git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: Improved calculation of table column widths.

We now have LaTeX do the calculation, using `\tabcolsep`. So we should now have accurate relative column widths no matter what the text width. The default template has been modified to load the calc package if tables are used.

commitbd7c9eb32be7c5499f4a50cb4b5f138f9eb919e8 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 29 Oct 2020, 12:10 (6 years ago) parent517c55d
4 files changed+79−49
data/templates/default.latex+1 src/Text/Pandoc/Writers/LaTeX.hs+12−10 test/command/5367.md+3−3 test/tables.latex+63−36
data/templates/default.latex+1
@@ -257,6 +257,7 @@ $highlighting-macros$
257257$endif$
258258$if(tables)$
259259\usepackage{longtable,booktabs}
260+\usepackage{calc} % for calculating minipage widths
260261$if(beamer)$
261262\usepackage{caption}
262263% Make caption package work with longtable
src/Text/Pandoc/Writers/LaTeX.hs+12−10
@@ -851,9 +851,6 @@ tableRowToLaTeX :: PandocMonad m
851851 -> [[Block]]
852852 -> LW m (Doc Text)
853853tableRowToLaTeX header aligns widths cols = do
854− -- scale factor compensates for extra space between columns
855− -- so the whole table isn't larger than columnwidth
856− let scaleFactor = 0.97 ** fromIntegral (length aligns)
857854 let isSimple [Plain _] = True
858855 isSimple [Para _] = True
859856 isSimple [] = True
@@ -861,9 +858,10 @@ tableRowToLaTeX header aligns widths cols = do
861858 -- simple tables have to have simple cells:
862859 let widths' = if all (== 0) widths && not (all isSimple cols)
863860 then replicate (length aligns)
864− (scaleFactor / fromIntegral (length aligns))
865− else map (scaleFactor *) widths
866− cells <- mapM (tableCellToLaTeX header) $ zip3 widths' aligns cols
861+ (1 / fromIntegral (length aligns))
862+ else widths
863+ let numcols = length widths'
864+ cells <- mapM (tableCellToLaTeX header numcols) $ zip3 widths' aligns cols
867865 return $ hsep (intersperse "&" cells) <> "\\tabularnewline"
868866
869867-- For simple latex tables (without minipages or parboxes),
@@ -890,11 +888,12 @@ displayMathToInline :: Inline -> Inline
890888displayMathToInline (Math DisplayMath x) = Math InlineMath x
891889displayMathToInline x = x
892890
893−tableCellToLaTeX :: PandocMonad m => Bool -> (Double, Alignment, [Block])
891+tableCellToLaTeX :: PandocMonad m
892+ => Bool -> Int -> (Double, Alignment, [Block])
894893 -> LW m (Doc Text)
895−tableCellToLaTeX _ (0, _, blocks) =
894+tableCellToLaTeX _ _ (0, _, blocks) =
896895 blockListToLaTeX $ walk fixLineBreaks $ walk displayMathToInline blocks
897−tableCellToLaTeX header (width, align, blocks) = do
896+tableCellToLaTeX header numcols (width, align, blocks) = do
898897 beamer <- gets stBeamer
899898 externalNotes <- gets stExternalNotes
900899 inMinipage <- gets stInMinipage
@@ -912,9 +911,12 @@ tableCellToLaTeX header (width, align, blocks) = do
912911 AlignCenter -> "\\centering"
913912 AlignDefault -> "\\raggedright"
914913 return $ "\\begin{minipage}" <> valign <>
915− braces (text (printf "%.2f\\columnwidth" width)) <>
914+ braces (text (printf
915+ "(\\columnwidth - %d\\tabcolsep) * \\real{%.2f}"
916+ (numcols - 1) width)) <>
916917 halign <> cr <> cellContents <> "\\strut" <> cr <>
917918 "\\end{minipage}"
919+-- (\columnwidth - 8\tabcolsep) * \real{0.15}
918920
919921notesToLaTeX :: [Doc Text] -> Doc Text
920922notesToLaTeX [] = empty
test/command/5367.md+3−3
@@ -23,18 +23,18 @@ hello\footnote{doc footnote}
2323\begin{longtable}[]{@{}c@{}}
2424\caption[Sample table.]{Sample table.\footnote{caption footnote}}\tabularnewline
2525\toprule
26−\begin{minipage}[b]{0.16\columnwidth}\centering
26+\begin{minipage}[b]{(\columnwidth - 0\tabcolsep) * \real{0.17}}\centering
2727Fruit\footnote{header footnote}\strut
2828\end{minipage}\tabularnewline
2929\midrule
3030\endfirsthead
3131\toprule
32−\begin{minipage}[b]{0.16\columnwidth}\centering
32+\begin{minipage}[b]{(\columnwidth - 0\tabcolsep) * \real{0.17}}\centering
3333Fruit{}\strut
3434\end{minipage}\tabularnewline
3535\midrule
3636\endhead
37−\begin{minipage}[t]{0.16\columnwidth}\centering
37+\begin{minipage}[t]{(\columnwidth - 0\tabcolsep) * \real{0.17}}\centering
3838Bans\footnote{table cell footnote}\strut
3939\end{minipage}\tabularnewline
4040\bottomrule
test/tables.latex+63−36
@@ -52,45 +52,57 @@ Multiline table with caption:
5252\begin{longtable}[]{@{}clrl@{}}
5353\caption{Here's the caption. It may span multiple lines.}\tabularnewline
5454\toprule
55−\begin{minipage}[b]{0.13\columnwidth}\centering
55+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.15}}\centering
5656Centered Header\strut
57−\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\raggedright
57+\end{minipage} &
58+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.14}}\raggedright
5859Left Aligned\strut
59−\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\raggedleft
60+\end{minipage} &
61+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.16}}\raggedleft
6062Right Aligned\strut
61−\end{minipage} & \begin{minipage}[b]{0.31\columnwidth}\raggedright
63+\end{minipage} &
64+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.35}}\raggedright
6265Default aligned\strut
6366\end{minipage}\tabularnewline
6467\midrule
6568\endfirsthead
6669\toprule
67−\begin{minipage}[b]{0.13\columnwidth}\centering
70+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.15}}\centering
6871Centered Header\strut
69−\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\raggedright
72+\end{minipage} &
73+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.14}}\raggedright
7074Left Aligned\strut
71−\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\raggedleft
75+\end{minipage} &
76+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.16}}\raggedleft
7277Right Aligned\strut
73−\end{minipage} & \begin{minipage}[b]{0.31\columnwidth}\raggedright
78+\end{minipage} &
79+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.35}}\raggedright
7480Default aligned\strut
7581\end{minipage}\tabularnewline
7682\midrule
7783\endhead
78−\begin{minipage}[t]{0.13\columnwidth}\centering
84+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.15}}\centering
7985First\strut
80−\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright
86+\end{minipage} &
87+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.14}}\raggedright
8188row\strut
82−\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft
89+\end{minipage} &
90+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.16}}\raggedleft
839112.0\strut
84−\end{minipage} & \begin{minipage}[t]{0.31\columnwidth}\raggedright
92+\end{minipage} &
93+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.35}}\raggedright
8594Example of a row that spans multiple lines.\strut
8695\end{minipage}\tabularnewline
87−\begin{minipage}[t]{0.13\columnwidth}\centering
96+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.15}}\centering
8897Second\strut
89−\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright
98+\end{minipage} &
99+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.14}}\raggedright
90100row\strut
91−\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft
101+\end{minipage} &
102+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.16}}\raggedleft
921035.0\strut
93−\end{minipage} & \begin{minipage}[t]{0.31\columnwidth}\raggedright
104+\end{minipage} &
105+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.35}}\raggedright
94106Here's another one. Note the blank line between rows.\strut
95107\end{minipage}\tabularnewline
96108\bottomrule
@@ -100,33 +112,42 @@ Multiline table without caption:
100112
101113\begin{longtable}[]{@{}clrl@{}}
102114\toprule
103−\begin{minipage}[b]{0.13\columnwidth}\centering
115+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.15}}\centering
104116Centered Header\strut
105−\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\raggedright
117+\end{minipage} &
118+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.14}}\raggedright
106119Left Aligned\strut
107−\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\raggedleft
120+\end{minipage} &
121+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.16}}\raggedleft
108122Right Aligned\strut
109−\end{minipage} & \begin{minipage}[b]{0.31\columnwidth}\raggedright
123+\end{minipage} &
124+\begin{minipage}[b]{(\columnwidth - 3\tabcolsep) * \real{0.35}}\raggedright
110125Default aligned\strut
111126\end{minipage}\tabularnewline
112127\midrule
113128\endhead
114−\begin{minipage}[t]{0.13\columnwidth}\centering
129+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.15}}\centering
115130First\strut
116−\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright
131+\end{minipage} &
132+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.14}}\raggedright
117133row\strut
118−\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft
134+\end{minipage} &
135+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.16}}\raggedleft
11913612.0\strut
120−\end{minipage} & \begin{minipage}[t]{0.31\columnwidth}\raggedright
137+\end{minipage} &
138+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.35}}\raggedright
121139Example of a row that spans multiple lines.\strut
122140\end{minipage}\tabularnewline
123−\begin{minipage}[t]{0.13\columnwidth}\centering
141+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.15}}\centering
124142Second\strut
125−\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright
143+\end{minipage} &
144+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.14}}\raggedright
126145row\strut
127−\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft
146+\end{minipage} &
147+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.16}}\raggedleft
1281485.0\strut
129−\end{minipage} & \begin{minipage}[t]{0.31\columnwidth}\raggedright
149+\end{minipage} &
150+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.35}}\raggedright
130151Here's another one. Note the blank line between rows.\strut
131152\end{minipage}\tabularnewline
132153\bottomrule
@@ -148,22 +169,28 @@ Multiline table without column headers:
148169\begin{longtable}[]{@{}clrl@{}}
149170\toprule
150171\endhead
151−\begin{minipage}[t]{0.13\columnwidth}\centering
172+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.15}}\centering
152173First\strut
153−\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright
174+\end{minipage} &
175+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.14}}\raggedright
154176row\strut
155−\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft
177+\end{minipage} &
178+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.16}}\raggedleft
15617912.0\strut
157−\end{minipage} & \begin{minipage}[t]{0.31\columnwidth}\raggedright
180+\end{minipage} &
181+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.35}}\raggedright
158182Example of a row that spans multiple lines.\strut
159183\end{minipage}\tabularnewline
160−\begin{minipage}[t]{0.13\columnwidth}\centering
184+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.15}}\centering
161185Second\strut
162−\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright
186+\end{minipage} &
187+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.14}}\raggedright
163188row\strut
164−\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft
189+\end{minipage} &
190+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.16}}\raggedleft
1651915.0\strut
166−\end{minipage} & \begin{minipage}[t]{0.31\columnwidth}\raggedright
192+\end{minipage} &
193+\begin{minipage}[t]{(\columnwidth - 3\tabcolsep) * \real{0.35}}\raggedright
167194Here's another one. Note the blank line between rows.\strut
168195\end{minipage}\tabularnewline
169196\bottomrule