git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: Fix regression in table header position.

In recent versions the table headers were no longer bottom-aligned (if more than one line). This patch fixes that by using minipages for table headers in non-simple tables. Closes #7347.

commit21cc52abe33997ea2f2c539f10d26684b7633bc0 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 5 Jun 2021, 14:13 (5 years ago) parentc550bf8
6 files changed+79−18
src/Text/Pandoc/Writers/LaTeX/Table.hs+10−3 test/command/5367.md+6−2 test/tables.latex+27−3 test/tables/nordics.latex+22−4 test/tables/planets.latex+4−4 test/tables/students.latex+10−2
src/Text/Pandoc/Writers/LaTeX/Table.hs+10−3
@@ -16,6 +16,7 @@ module Text.Pandoc.Writers.LaTeX.Table
1616 ) where
1717import Control.Monad.State.Strict
1818import Data.List (intersperse)
19+import qualified Data.List.NonEmpty as NonEmpty
1920import Data.List.NonEmpty (NonEmpty ((:|)))
2021import Data.Text (Text)
2122import qualified Data.Text as T
@@ -243,8 +244,13 @@ cellToLaTeX :: PandocMonad m
243244 -> Ann.Cell
244245 -> LW m (Doc Text)
245246cellToLaTeX blockListToLaTeX celltype annotatedCell = do
246− let (Ann.Cell _specs _colnum cell) = annotatedCell
247− let (Cell _attr align rowspan colspan blocks) = cell
247+ let (Ann.Cell specs _colnum cell) = annotatedCell
248+ let hasWidths = snd (NonEmpty.head specs) /= ColWidthDefault
249+ let specAlign = fst (NonEmpty.head specs)
250+ let (Cell _attr align' rowspan colspan blocks) = cell
251+ let align = case align' of
252+ AlignDefault -> specAlign
253+ _ -> align'
248254 beamer <- gets stBeamer
249255 externalNotes <- gets stExternalNotes
250256 inMinipage <- gets stInMinipage
@@ -256,7 +262,7 @@ cellToLaTeX blockListToLaTeX celltype annotatedCell = do
256262 Plain{} -> True
257263 _ -> False
258264 result <-
259− if all isPlainOrPara blocks
265+ if not hasWidths || (celltype /= HeaderCell && all isPlainOrPara blocks)
260266 then
261267 blockListToLaTeX $ walk fixLineBreaks $ walk displayMathToInline blocks
262268 else do
@@ -290,3 +296,4 @@ cellToLaTeX blockListToLaTeX celltype annotatedCell = do
290296data CellType
291297 = HeaderCell
292298 | BodyCell
299+ deriving Eq
test/command/5367.md+6−2
@@ -24,11 +24,15 @@ hello\footnote{doc footnote}
2424 >{\centering\arraybackslash}p{(\columnwidth - 0\tabcolsep) * \real{0.17}}@{}}
2525\caption[Sample table.]{Sample table.\footnote{caption footnote}}\tabularnewline
2626\toprule
27−Fruit\footnote{header footnote} \\
27+\begin{minipage}[b]{\linewidth}\centering
28+Fruit\footnote{header footnote}
29+\end{minipage} \\
2830\midrule
2931\endfirsthead
3032\toprule
31−Fruit{} \\
33+\begin{minipage}[b]{\linewidth}\centering
34+Fruit{}
35+\end{minipage} \\
3236\midrule
3337\endhead
3438Bans\footnote{table cell footnote} \\
test/tables.latex+27−3
@@ -56,11 +56,27 @@ Multiline table with caption:
5656 >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.35}}@{}}
5757\caption{Here's the caption. It may span multiple lines.}\tabularnewline
5858\toprule
59−Centered Header & Left Aligned & Right Aligned & Default aligned \\
59+\begin{minipage}[b]{\linewidth}\centering
60+Centered Header
61+\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
62+Left Aligned
63+\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedleft
64+Right Aligned
65+\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
66+Default aligned
67+\end{minipage} \\
6068\midrule
6169\endfirsthead
6270\toprule
63−Centered Header & Left Aligned & Right Aligned & Default aligned \\
71+\begin{minipage}[b]{\linewidth}\centering
72+Centered Header
73+\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
74+Left Aligned
75+\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedleft
76+Right Aligned
77+\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
78+Default aligned
79+\end{minipage} \\
6480\midrule
6581\endhead
6682First & row & 12.0 & Example of a row that spans multiple lines. \\
@@ -76,7 +92,15 @@ Multiline table without caption:
7692 >{\raggedleft\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.16}}
7793 >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.35}}@{}}
7894\toprule
79−Centered Header & Left Aligned & Right Aligned & Default aligned \\
95+\begin{minipage}[b]{\linewidth}\centering
96+Centered Header
97+\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
98+Left Aligned
99+\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedleft
100+Right Aligned
101+\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
102+Default aligned
103+\end{minipage} \\
80104\midrule
81105\endhead
82106First & row & 12.0 & Example of a row that spans multiple lines. \\
test/tables/nordics.latex+22−4
@@ -5,13 +5,31 @@
55 >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.20}}@{}}
66\caption{States belonging to the \emph{Nordics.}}\tabularnewline
77\toprule
8−Name & Capital & \vtop{\hbox{\strut Population}\hbox{\strut (in 2018)}} &
9−\vtop{\hbox{\strut Area}\hbox{\strut (in km\textsuperscript{2})}} \\
8+\begin{minipage}[b]{\linewidth}\centering
9+Name
10+\end{minipage} & \begin{minipage}[b]{\linewidth}\centering
11+Capital
12+\end{minipage} & \begin{minipage}[b]{\linewidth}\centering
13+Population\\
14+(in 2018)
15+\end{minipage} & \begin{minipage}[b]{\linewidth}\centering
16+Area\\
17+(in km\textsuperscript{2})
18+\end{minipage} \\
1019\midrule
1120\endfirsthead
1221\toprule
13−Name & Capital & \vtop{\hbox{\strut Population}\hbox{\strut (in 2018)}} &
14−\vtop{\hbox{\strut Area}\hbox{\strut (in km\textsuperscript{2})}} \\
22+\begin{minipage}[b]{\linewidth}\centering
23+Name
24+\end{minipage} & \begin{minipage}[b]{\linewidth}\centering
25+Capital
26+\end{minipage} & \begin{minipage}[b]{\linewidth}\centering
27+Population\\
28+(in 2018)
29+\end{minipage} & \begin{minipage}[b]{\linewidth}\centering
30+Area\\
31+(in km\textsuperscript{2})
32+\end{minipage} \\
1533\midrule
1634\endhead
1735Denmark & Copenhagen & 5,809,502 & 43,094 \\
test/tables/planets.latex+4−4
@@ -1,18 +1,18 @@
11\begin{longtable}[]{@{}cclrrrrrrrrl@{}}
22\caption{Data about the planets of our solar system.}\tabularnewline
33\toprule
4−\multicolumn{2}{l}{} & Name & Mass (10\^{}24kg) & Diameter (km) & Density
4+\multicolumn{2}{c}{} & Name & Mass (10\^{}24kg) & Diameter (km) & Density
55(kg/m\^{}3) & Gravity (m/s\^{}2) & Length of day (hours) & Distance from Sun
66(10\^{}6km) & Mean temperature (C) & Number of moons & Notes \\
77\midrule
88\endfirsthead
99\toprule
10−\multicolumn{2}{l}{} & Name & Mass (10\^{}24kg) & Diameter (km) & Density
10+\multicolumn{2}{c}{} & Name & Mass (10\^{}24kg) & Diameter (km) & Density
1111(kg/m\^{}3) & Gravity (m/s\^{}2) & Length of day (hours) & Distance from Sun
1212(10\^{}6km) & Mean temperature (C) & Number of moons & Notes \\
1313\midrule
1414\endhead
15−\multicolumn{2}{l}{\multirow{4}{*}{Terrestrial planets}} & Mercury & 0.330 &
15+\multicolumn{2}{c}{\multirow{4}{*}{Terrestrial planets}} & Mercury & 0.330 &
16164,879 & 5427 & 3.7 & 4222.6 & 57.9 & 167 & 0 & Closest to the Sun \\
1717& & Venus & 4.87 & 12,104 & 5243 & 8.9 & 2802.0 & 108.2 & 464 & 0 & \\
1818& & Earth & 5.97 & 12,756 & 5514 & 9.8 & 24.0 & 149.6 & 15 & 1 & Our world \\
@@ -24,7 +24,7 @@ planet \\
2424& \multirow{2}{*}{Ice giants} & Uranus & 86.8 & 51,118 & 1271 & 8.7 & 17.2 &
25252872.5 & -195 & 27 & \\
2626& & Neptune & 102 & 49,528 & 1638 & 11.0 & 16.1 & 4495.1 & -200 & 14 & \\
27−\multicolumn{2}{l}{Dwarf planets} & Pluto & 0.0146 & 2,370 & 2095 & 0.7 &
27+\multicolumn{2}{c}{Dwarf planets} & Pluto & 0.0146 & 2,370 & 2095 & 0.7 &
2828153.3 & 5906.4 & -225 & 5 & Declassified as a planet in 2006. \\
2929\bottomrule
3030\end{longtable}
test/tables/students.latex+10−2
@@ -3,11 +3,19 @@
33 >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.50}}@{}}
44\caption{List of Students}\tabularnewline
55\toprule
6−Student ID & Name \\
6+\begin{minipage}[b]{\linewidth}\centering
7+Student ID
8+\end{minipage} & \begin{minipage}[b]{\linewidth}\centering
9+Name
10+\end{minipage} \\
711\midrule
812\endfirsthead
913\toprule
10−Student ID & Name \\
14+\begin{minipage}[b]{\linewidth}\centering
15+Student ID
16+\end{minipage} & \begin{minipage}[b]{\linewidth}\centering
17+Name
18+\end{minipage} \\
1119\midrule
1220\endhead
1321\multicolumn{2}{l}{Computer Science} \\