title | author |
---|---|
tables-rules - Pandoc filter to add rules to tables in LaTeX |
Christophe Agathon |
Add vertical and horizontal rules to tables.
v1.0. Copyright: © 2021 Christophe Agathon [email protected] License: MIT - see LICENSE file for details.
This filter manages vertical and horizontal rules in latex generated by Pandoc.
Two boolean metadata tables-vrules
and tables-hrules
control if tables will have vertical or horizontal rules, or both.
Since pandoc has a strong policy against vertical rules in tables, people have been looking for solutions to get those, especially when rendering PDF files via Latex.
For more information you can refer to :
- This Pandoc issue jgm/pandoc#922
- This discussion on StackExchange https://tex.stackexchange.com/questions/595615/how-can-i-reformat-a-table-using-markdown-pandoc-pdf/596005
marjinshraagen proposed a solution based on a patch of \LT@array
in Latex. It used to work pretty well. It doesn't anymore for Multiline Tables and Pipes Tables since Pandoc changed the Latex code it generates for those kind of tables. Don't know exactly when it changed but sometime between Pandoc version 2.9.2.1 and version 2.16.
Since patching in Latex is tricky and I am not a Latex guru, I didn't manage to make it work again, so I made this filter which change the call to longtable
to add vertical rules in a more "natural" way.
The filter adds some code in the Latex preamble because Pandoc adds it only if there are tables in the document (which is OK) and only if the Tables blocks are not processed by a filter (which might be considered as bug).
You would probably call this filter after your other filters since it generates raw LaTeX code for every table in the document. Pandoc blocs and inlines are no longer available for subsequent filters.
This might be better to do this in a writer than in a filter unfortunately writers are not modular like filters and one would have to implement the whole LaTeX writer to do this.
UPDATE : custom writers in Pandoc have been redesigned so it's now an option.
Since the processing is not based on a LaTeX \patchcmd, compatibility is high.
Its compatible with colortbl
and array
packages.
Rules are not doubled below the table first row (header) and last row as it happens with others hacks.
With a Markdown document, simply use one of the table synthax allowed by Pandoc (details in Pandoc's manual.
To get rules on all the tables of the document, you can either :
- Set
tables-vrules: true
and/ortables-hrules: true
in the YAML preamble of your Marldown document - Add
--metadata=tables-vrules
and/or--metadata=tables-hrules
to your Pandoc command (especially if your source document is not Markdown, but HTML or something else)
Copy tables-rules.lua
in your document folder or in your pandoc
data directory (details in
Pandoc's manual).
Run it on your document with a --luafilter
option:
pandoc --luafilter tables-rules.lua SOURCE.md -o OUTPUT.pdf
or specify it in a defaults file (details in Pandoc's manual).
This will generate Tables with vertical and/or horizontal rules in Latex and PDF documents from Pandoc markdown source files.
This filter is active only for Latex and PDF output.
Rules in HTML documents should be handled via css styling.
PRs welcome.