Skip to content

Commit e6976fc

Browse files
committed
First working filter with test file for the 4 kinds of tables supported
by pandoc
1 parent 1abec29 commit e6976fc

File tree

5 files changed

+261
-4
lines changed

5 files changed

+261
-4
lines changed

tables-vrules/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ DIFF ?= diff --strip-trailing-cr -u
55
test: test_latex
66

77
test_latex: sample.md expected.tex tables-vrules.lua
8-
@pandoc --lua-filter tables-vrules.lua --to=latex $< \
8+
@pandoc -s --lua-filter tables-vrules.lua --to=latex $< \
99
| $(DIFF) expected.tex -
1010

1111
expected.tex: sample.md tables-vrules.lua
12-
pandoc --lua-filter tables-vrules.lua --output $@ $<
12+
pandoc -s --lua-filter tables-vrules.lua --output $@ $<

tables-vrules/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ For more information you can refer to :
2424

2525
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.
2626

27-
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" whay.
27+
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.
28+
29+
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).
2830

2931

3032
Usage

tables-vrules/expected.tex

+196
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
% Options for packages loaded elsewhere
2+
\PassOptionsToPackage{unicode}{hyperref}
3+
\PassOptionsToPackage{hyphens}{url}
4+
%
5+
\documentclass[
6+
]{article}
7+
\usepackage{amsmath,amssymb}
8+
\usepackage{lmodern}
9+
\usepackage{iftex}
10+
\ifPDFTeX
11+
\usepackage[T1]{fontenc}
12+
\usepackage[utf8]{inputenc}
13+
\usepackage{textcomp} % provide euro and other symbols
14+
\else % if luatex or xetex
15+
\usepackage{unicode-math}
16+
\defaultfontfeatures{Scale=MatchLowercase}
17+
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
18+
\fi
19+
% Use upquote if available, for straight quotes in verbatim environments
20+
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
21+
\IfFileExists{microtype.sty}{% use microtype if available
22+
\usepackage[]{microtype}
23+
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
24+
}{}
25+
\makeatletter
26+
\@ifundefined{KOMAClassName}{% if non-KOMA class
27+
\IfFileExists{parskip.sty}{%
28+
\usepackage{parskip}
29+
}{% else
30+
\setlength{\parindent}{0pt}
31+
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
32+
}{% if KOMA class
33+
\KOMAoptions{parskip=half}}
34+
\makeatother
35+
\usepackage{xcolor}
36+
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
37+
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
38+
\hypersetup{
39+
pdftitle={table-vrules lua filter test file},
40+
pdfauthor={Christophe Agathon},
41+
hidelinks,
42+
pdfcreator={LaTeX via pandoc}}
43+
\urlstyle{same} % disable monospaced font for URLs
44+
\setlength{\emergencystretch}{3em} % prevent overfull lines
45+
\providecommand{\tightlist}{%
46+
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
47+
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
48+
\usepackage{longtable,booktabs,array}
49+
\usepackage{calc} % for calculating minipage widths
50+
% Correct order of tables after \paragraph or \subparagraph
51+
\usepackage{etoolbox}
52+
\makeatletter
53+
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
54+
\makeatother
55+
% Allow footnotes in longtable head/foot
56+
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
57+
\makesavenoteenv{longtable}
58+
\ifLuaTeX
59+
\usepackage{selnolig} % disable illegal ligatures
60+
\fi
61+
62+
\title{table-vrules lua filter test file}
63+
\author{Christophe Agathon}
64+
\date{}
65+
66+
\begin{document}
67+
\maketitle
68+
69+
\hypertarget{simple-table}{%
70+
\section{Simple Table}\label{simple-table}}
71+
72+
\begin{longtable}[]{@{}|r|l|c|l|@{}}
73+
\caption{Demonstration of simple table syntax.}\tabularnewline
74+
\toprule
75+
Right & Left & Center & Default \\
76+
\midrule
77+
\endfirsthead
78+
\toprule
79+
Right & Left & Center & Default \\
80+
\midrule
81+
\endhead
82+
12 & 12 & 12 & 12 \\
83+
123 & 123 & 123 & 123 \\
84+
1 & 1 & 1 & 1 \\
85+
\bottomrule
86+
\end{longtable}
87+
88+
\hypertarget{multiline-tables}{%
89+
\section{Multiline Tables}\label{multiline-tables}}
90+
91+
\begin{longtable}[]{@{}
92+
|>{\centering\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.1667}}
93+
|>{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.1111}}
94+
|>{\raggedleft\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.2222}}
95+
|>{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.3611}}|@{}}
96+
\caption{Here's the caption. It, too, may span multiple
97+
lines.}\tabularnewline
98+
\toprule
99+
\begin{minipage}[b]{\linewidth}\centering
100+
Centered Header
101+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
102+
Default Aligned
103+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedleft
104+
Right Aligned
105+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
106+
Left Aligned
107+
\end{minipage} \\
108+
\midrule
109+
\endfirsthead
110+
\toprule
111+
\begin{minipage}[b]{\linewidth}\centering
112+
Centered Header
113+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
114+
Default Aligned
115+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedleft
116+
Right Aligned
117+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
118+
Left Aligned
119+
\end{minipage} \\
120+
\midrule
121+
\endhead
122+
First & row & 12.0 & Example of a row that spans multiple lines. \\
123+
Second & row & 5.0 & Here's another one. Note the blank line between
124+
rows. \\
125+
\bottomrule
126+
\end{longtable}
127+
128+
\hypertarget{grid-tables}{%
129+
\section{Grid Tables}\label{grid-tables}}
130+
131+
\begin{longtable}[]{@{}
132+
|>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.2222}}
133+
|>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.2222}}
134+
|>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.2917}}|@{}}
135+
\caption{Sample grid table.}\tabularnewline
136+
\toprule
137+
\begin{minipage}[b]{\linewidth}\raggedright
138+
Fruit
139+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
140+
Price
141+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
142+
Advantages
143+
\end{minipage} \\
144+
\midrule
145+
\endfirsthead
146+
\toprule
147+
\begin{minipage}[b]{\linewidth}\raggedright
148+
Fruit
149+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
150+
Price
151+
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
152+
Advantages
153+
\end{minipage} \\
154+
\midrule
155+
\endhead
156+
Bananas & \$1.34 & \begin{minipage}[t]{\linewidth}\raggedright
157+
\begin{itemize}
158+
\tightlist
159+
\item
160+
built-in wrapper
161+
\item
162+
bright color
163+
\end{itemize}
164+
\end{minipage} \\
165+
Oranges & \$2.10 & \begin{minipage}[t]{\linewidth}\raggedright
166+
\begin{itemize}
167+
\tightlist
168+
\item
169+
cures scurvy
170+
\item
171+
tasty
172+
\end{itemize}
173+
\end{minipage} \\
174+
\bottomrule
175+
\end{longtable}
176+
177+
\hypertarget{pipes-tables}{%
178+
\section{Pipes Tables}\label{pipes-tables}}
179+
180+
\begin{longtable}[]{@{}|r|l|l|c|@{}}
181+
\caption{Demonstration of pipe table syntax.}\tabularnewline
182+
\toprule
183+
Right & Left & Default & Center \\
184+
\midrule
185+
\endfirsthead
186+
\toprule
187+
Right & Left & Default & Center \\
188+
\midrule
189+
\endhead
190+
12 & 12 & 12 & 12 \\
191+
123 & 123 & 123 & 123 \\
192+
1 & 1 & 1 & 1 \\
193+
\bottomrule
194+
\end{longtable}
195+
196+
\end{document}

tables-vrules/sample.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
---
2+
title: table-vrules lua filter test file
3+
author: Christophe Agathon
4+
5+
---
16

27
# Simple Table
38

tables-vrules/tables-vrules.lua

+55-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,60 @@ local List = require 'pandoc.List'
1515

1616

1717
function Table(table)
18-
local returned-list
18+
local returned_list
19+
local latex_code = ''
20+
local coldef =''
21+
local envdef =''
22+
local new_coldef =''
1923

24+
if FORMAT:match 'latex' then
25+
26+
--print(pandoc.utils.stringify(table.caption))
27+
28+
latex_code = pandoc.write ( pandoc.Pandoc({table}),'latex' )
29+
envdef, begdef, coldef, enddef = latex_code:match("((\\begin{longtable}%[[^%]]*%]{@{})(.*)(@{}}))")
30+
31+
if not coldef then return nil end
32+
33+
if coldef:match('^[lrc]+$') then
34+
-- old style
35+
new_coldef = coldef:gsub('(.)','|%1') .. '|'
36+
else
37+
-- asuming new style
38+
new_coldef = coldef:gsub('(>)','|%1') .. '|'
39+
end
40+
--print ('>', coldef)
41+
--print ('>', new_coldef)
42+
--print ('>', envdef)
43+
--print ('>', begdef .. new_coldef .. enddef ..
44+
-- latex_code:sub(envdef:len() + 1))
45+
returned_list = List:new{pandoc.RawBlock('tex',
46+
begdef .. new_coldef .. enddef ..
47+
latex_code:sub(envdef:len() + 1))}
48+
end
49+
return returned_list
50+
end
51+
52+
function Meta(meta)
53+
-- We have to add this since Pandoc doesn't do it when a filter is
54+
-- processing tables (is it a bug or a feature ???)
55+
--
56+
includes = [[\usepackage{longtable,booktabs,array}
57+
\usepackage{calc} % for calculating minipage widths
58+
% Correct order of tables after \paragraph or \subparagraph
59+
\usepackage{etoolbox}
60+
\makeatletter
61+
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
62+
\makeatother
63+
% Allow footnotes in longtable head/foot
64+
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
65+
\makesavenoteenv{longtable} ]]
66+
67+
if meta['header-includes'] then
68+
table.insert(meta['header-includes'], pandoc.RawBlock('tex', includes))
69+
else
70+
meta['header-includes'] = List:new{pandoc.RawBlock('tex', includes)}
71+
end
72+
73+
return meta
2074
end

0 commit comments

Comments
 (0)