From ebfdb37084a080c3754bfef001cbb094c5492699 Mon Sep 17 00:00:00 2001 From: Michael Schramm Date: Tue, 14 Mar 2023 18:46:53 -0400 Subject: [PATCH] add table-rules.lua --- _extensions/quarto-yaac/table-rules.lua | 121 ++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 _extensions/quarto-yaac/table-rules.lua diff --git a/_extensions/quarto-yaac/table-rules.lua b/_extensions/quarto-yaac/table-rules.lua new file mode 100644 index 0000000..86a0d5d --- /dev/null +++ b/_extensions/quarto-yaac/table-rules.lua @@ -0,0 +1,121 @@ +--[[ +tables-vrules - adds vertical rules to tables for latex output +Copyright: © 2021 Christophe Agathon +License: MIT – see LICENSE file 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 repl_multicol(m1, m2) + return m1 .. '{|' .. m2 .. '|}' +end + +function repl_multicol_r(m1, m2) + return m1 .. '{' .. m2 .. '|}' +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 + -- fix multicolumn if needed + -- right rule on every multicol + latex_code = latex_code:gsub('(&%s*\\multicolumn{%d+}){([^}]+)}', repl_multicol_r) + -- left rule if it begins the row + latex_code = latex_code:gsub('([^&]\n\\multicolumn{%d+}){([^}]+)}', repl_multicol) + 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}} \ No newline at end of file