Skip to content

restyle tables #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions filter/table-rules.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
--[[
MIT License

Copyright (c) 2017-2021 pandoc Lua filters contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

tables-vrules - adds vertical rules to tables for latex output

Copyright: © 2021 Christophe Agathon <[email protected]>

License: MIT – see license for details

Credits: marijnschraagen for the original Latex hack

Output: latex, pdf.

Usage: See README.md for details

--]]
local List = require 'pandoc.List'

local vars = {}

function get_vars (meta)
vars.vrules = meta['tables-vrules']
vars.hrules = meta['tables-hrules']
end

function repl_midrules(m1, m2)
if m2:match('^\\[%w]+rule') then
-- don't double the rule
return m1 .. m2
else
return m1 .. '\n\\midrule\n' .. m2
end
end

function Table(table)
local returned_list
local latex_code = ''
local coldef =''
local envdef =''
local new_coldef =''
local end_line = ''

if not vars.vrules and not vars.hrules then return nil end

if FORMAT:match 'latex' then

-- Get latex code for the whole table
latex_code = pandoc.write ( pandoc.Pandoc({table}),'latex' )

-- Rewrite column definition to add vertical rules if needed
if vars.vrules then
envdef, begdef, coldef, enddef =
latex_code:match("((\\begin{longtable}%[[^%]]*%]{@{})(.*)(@{}}))")

if coldef then
if coldef:match('^[lrc]+$') then
-- old style
new_coldef = coldef:gsub('(.)', '|%1') .. '|'
else
-- asuming new style
new_coldef = coldef:gsub('(>)', '|%1') .. '|'
end
latex_code = latex_code:sub(envdef:len() + 1)
end
end

-- Add \midrules after each row if needed
if vars.hrules then
latex_code = latex_code:gsub('(\\\\\n)([\\%w]+)', repl_midrules)
end

-- Return modified latex code as a raw block
if vars.vrules then
returned_list = List:new{pandoc.RawBlock('tex',
begdef .. new_coldef .. enddef ..
latex_code)}
else
returned_list = List:new{pandoc.RawBlock('tex', latex_code)}
end
end
return returned_list
end

function Meta(meta)
-- We have to add this since Pandoc doesn't because there are no
-- table anymore in the AST. We converted them in RawBlocks

if not vars.vrules and not vars.hrules then return nil end
includes = [[
%begin tables-vrules.lua
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\renewcommand{\arraystretch}{1.3}
%end tables-vrules.lua
]]

if meta['header-includes'] then
table.insert(meta['header-includes'], pandoc.RawBlock('tex', includes))
else
meta['header-includes'] = List:new{pandoc.RawBlock('tex', includes)}
end

return meta
end

return {{Meta = get_vars}, {Table = Table}, {Meta = Meta}}
23 changes: 10 additions & 13 deletions template/eisvogel.latex
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,21 @@ $endif$
\sloppy

% Customizations to the behavior of tables
$if(tables)$
\usepackage{longtable,booktabs,array}
$if(multirow)$
\usepackage{multirow}
$endif$
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph

% % Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot

% % Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
$endif$

\usepackage{footnotebackref}
$if(graphics)$
Expand Down Expand Up @@ -413,16 +413,14 @@ $endif$
%
% tables
%
$if(tables)$

\definecolor{table-row-color}{HTML}{C0C0C0}
\definecolor{table-alt-color}{HTML}{DADADA}
\definecolor{table-rule-color}{HTML}{FFFFFF}
\definecolor{table-row-color}{HTML}{E0E0E0}
\definecolor{table-alt-color}{HTML}{E0E0E0}
\definecolor{table-rule-color}{HTML}{4D4D4D}

\arrayrulecolor{table-rule-color} % color of \toprule, \midrule, \bottomrule
\setlength\heavyrulewidth{0em} % thickness of \toprule, \bottomrule
\setlength\lightrulewidth{0em} % thickness of \midrule
\setlength\arrayrulewidth{0em} % thickness of array rules
\setlength\heavyrulewidth{1pt} % thickness of \toprule, \bottomrule
\setlength\lightrulewidth{1pt} % thickness of \midrule
\setlength\arrayrulewidth{1pt} % thickness of array rules
\renewcommand{\arraystretch}{1.3} % spacing (padding)

$if(table-use-row-colors)$
Expand All @@ -432,7 +430,6 @@ $if(table-use-row-colors)$
\appto{\endhead}{\showrowcolors}{}{}
\appto{\endfirsthead}{\showrowcolors}{}{}
$endif$
$endif$

%
% remove paragraph indentation
Expand Down