Pandoc Filter to Insert Arbitrary Complex Tables

Outputs: Web Page / LaTeX / PDF / Overleaf

Dependencies

Make sure you have Pandoc and pandoc-crossref installed (callable from cmd).

Usage

Write your complex tables in HTML in tables.html and in LaTeX in tables.tex. https://tablesgenerator.com is a good resource for constructing complex tables. To insert tables into the output HTML/LaTeX document, use the syntax <COMMENT> tbl:table-id <COMMENT> to mark the beginning and <COMMENT> END <COMMENT> to mark the end of a table definition in tables.html and tables.tex. <COMMENT> corresponds to % in LaTeX and <!-- and --> in HTML. tbl:table-id is the identifier of the table used for cross-referencing in the markdown source. Refer to pandoc-crossref for details of cross referencing tables.

To compile the documents, apply the filter custom-table.py AFTER pandoc-crossref in the command line.

pandoc -F pandoc-crossref --lua-filter insertTables.lua README.md -o README.tex
pandoc -F pandoc-crossref --lua-filter insertTables.lua README.md -o README.html

Example

Column Span
Row Span a b d f
c d e g
Table 1: This is a complex table, written in tables.tex and tables.html.

See Table 1.

Table 2: This is a normal table written in markdown, which will not be replaced.
Column A Column B
A1 B1
A2 B2

Custom Caption Positions

By default, insertTables.lua looks for the string \begin{tabular and inserts the caption before it. In circumstances where \begin{tabular} or \begin{tabularx} are not present in the table’s code, this filter will fail. To deal with these cases, you have to tell insertTables.lua where to insert the caption by placing the anchor %caption% in your table’s code. This may also be useful when you want to place the caption below the table body. This can be achieved by placing the anchor %caption% after the tabular environment:

\begin{table}[!htb]
    \centering
    \begin{tabular}{lllll}
        \hline
        \textbf{} & \multicolumn{4}{l}{Column Span} \\ \hline
        \multirow{2}{*}{Row Span} & a & b & d & f \\
         & c & d & e & g \\ \hline
        \end{tabular}
    %caption%
\end{table}

which results in Table 3 (this could only be seen in tex or PDF output).

Table 3: For LaTeX tables, you can define the position of the caption with the string %caption% in tables.tex.
Placeholder
Table