Skip to content

Commit 4f6ce96

Browse files
authored
restyle tables (#54)
* restyle tables * update colors and weights * try disabling row colors * disable some old workarounds * comment out the new hack * try more of the workaround * add new filter
1 parent 623b460 commit 4f6ce96

File tree

2 files changed

+146
-13
lines changed

2 files changed

+146
-13
lines changed

filter/table-rules.lua

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
--[[
2+
MIT License
3+
4+
Copyright (c) 2017-2021 pandoc Lua filters contributors
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
24+
tables-vrules - adds vertical rules to tables for latex output
25+
26+
Copyright: © 2021 Christophe Agathon <[email protected]>
27+
28+
License: MIT – see license for details
29+
30+
Credits: marijnschraagen for the original Latex hack
31+
32+
Output: latex, pdf.
33+
34+
Usage: See README.md for details
35+
36+
--]]
37+
local List = require 'pandoc.List'
38+
39+
local vars = {}
40+
41+
function get_vars (meta)
42+
vars.vrules = meta['tables-vrules']
43+
vars.hrules = meta['tables-hrules']
44+
end
45+
46+
function repl_midrules(m1, m2)
47+
if m2:match('^\\[%w]+rule') then
48+
-- don't double the rule
49+
return m1 .. m2
50+
else
51+
return m1 .. '\n\\midrule\n' .. m2
52+
end
53+
end
54+
55+
function Table(table)
56+
local returned_list
57+
local latex_code = ''
58+
local coldef =''
59+
local envdef =''
60+
local new_coldef =''
61+
local end_line = ''
62+
63+
if not vars.vrules and not vars.hrules then return nil end
64+
65+
if FORMAT:match 'latex' then
66+
67+
-- Get latex code for the whole table
68+
latex_code = pandoc.write ( pandoc.Pandoc({table}),'latex' )
69+
70+
-- Rewrite column definition to add vertical rules if needed
71+
if vars.vrules then
72+
envdef, begdef, coldef, enddef =
73+
latex_code:match("((\\begin{longtable}%[[^%]]*%]{@{})(.*)(@{}}))")
74+
75+
if coldef then
76+
if coldef:match('^[lrc]+$') then
77+
-- old style
78+
new_coldef = coldef:gsub('(.)', '|%1') .. '|'
79+
else
80+
-- asuming new style
81+
new_coldef = coldef:gsub('(>)', '|%1') .. '|'
82+
end
83+
latex_code = latex_code:sub(envdef:len() + 1)
84+
end
85+
end
86+
87+
-- Add \midrules after each row if needed
88+
if vars.hrules then
89+
latex_code = latex_code:gsub('(\\\\\n)([\\%w]+)', repl_midrules)
90+
end
91+
92+
-- Return modified latex code as a raw block
93+
if vars.vrules then
94+
returned_list = List:new{pandoc.RawBlock('tex',
95+
begdef .. new_coldef .. enddef ..
96+
latex_code)}
97+
else
98+
returned_list = List:new{pandoc.RawBlock('tex', latex_code)}
99+
end
100+
end
101+
return returned_list
102+
end
103+
104+
function Meta(meta)
105+
-- We have to add this since Pandoc doesn't because there are no
106+
-- table anymore in the AST. We converted them in RawBlocks
107+
108+
if not vars.vrules and not vars.hrules then return nil end
109+
includes = [[
110+
%begin tables-vrules.lua
111+
\usepackage{longtable,booktabs,array}
112+
\usepackage{calc} % for calculating minipage widths
113+
% Correct order of tables after \paragraph or \subparagraph
114+
\usepackage{etoolbox}
115+
\makeatletter
116+
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
117+
\makeatother
118+
% Allow footnotes in longtable head/foot
119+
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
120+
\makesavenoteenv{longtable}
121+
\setlength{\aboverulesep}{0pt}
122+
\setlength{\belowrulesep}{0pt}
123+
\renewcommand{\arraystretch}{1.3}
124+
%end tables-vrules.lua
125+
]]
126+
127+
if meta['header-includes'] then
128+
table.insert(meta['header-includes'], pandoc.RawBlock('tex', includes))
129+
else
130+
meta['header-includes'] = List:new{pandoc.RawBlock('tex', includes)}
131+
end
132+
133+
return meta
134+
end
135+
136+
return {{Meta = get_vars}, {Table = Table}, {Meta = Meta}}

template/eisvogel.latex

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,21 @@ $endif$
193193
\sloppy
194194

195195
% Customizations to the behavior of tables
196-
$if(tables)$
197196
\usepackage{longtable,booktabs,array}
198197
$if(multirow)$
199198
\usepackage{multirow}
200199
$endif$
201200
\usepackage{calc} % for calculating minipage widths
202-
% Correct order of tables after \paragraph or \subparagraph
201+
202+
% % Correct order of tables after \paragraph or \subparagraph
203203
\usepackage{etoolbox}
204204
\makeatletter
205205
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
206206
\makeatother
207-
% Allow footnotes in longtable head/foot
207+
208+
% % Allow footnotes in longtable head/foot
208209
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
209210
\makesavenoteenv{longtable}
210-
$endif$
211211

212212
\usepackage{footnotebackref}
213213
$if(graphics)$
@@ -413,16 +413,14 @@ $endif$
413413
%
414414
% tables
415415
%
416-
$if(tables)$
417-
418-
\definecolor{table-row-color}{HTML}{C0C0C0}
419-
\definecolor{table-alt-color}{HTML}{DADADA}
420-
\definecolor{table-rule-color}{HTML}{FFFFFF}
416+
\definecolor{table-row-color}{HTML}{E0E0E0}
417+
\definecolor{table-alt-color}{HTML}{E0E0E0}
418+
\definecolor{table-rule-color}{HTML}{4D4D4D}
421419

422420
\arrayrulecolor{table-rule-color} % color of \toprule, \midrule, \bottomrule
423-
\setlength\heavyrulewidth{0em} % thickness of \toprule, \bottomrule
424-
\setlength\lightrulewidth{0em} % thickness of \midrule
425-
\setlength\arrayrulewidth{0em} % thickness of array rules
421+
\setlength\heavyrulewidth{1pt} % thickness of \toprule, \bottomrule
422+
\setlength\lightrulewidth{1pt} % thickness of \midrule
423+
\setlength\arrayrulewidth{1pt} % thickness of array rules
426424
\renewcommand{\arraystretch}{1.3} % spacing (padding)
427425

428426
$if(table-use-row-colors)$
@@ -432,7 +430,6 @@ $if(table-use-row-colors)$
432430
\appto{\endhead}{\showrowcolors}{}{}
433431
\appto{\endfirsthead}{\showrowcolors}{}{}
434432
$endif$
435-
$endif$
436433

437434
%
438435
% remove paragraph indentation

0 commit comments

Comments
 (0)