Skip to content

LaTeX: Repeat caption of a table consistently. #4623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/Text/Pandoc/Writers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -719,19 +719,23 @@ blockToLaTeX (Table caption aligns widths heads rows) = do
let removeNote (Note _) = Span ("", [], []) []
removeNote x = x
captionText <- inlineListToLaTeX caption
firsthead <- if isEmpty captionText || all null heads
then return empty
else ($$ text "\\endfirsthead") <$> toHeaders heads
let capt = if isEmpty captionText
then empty
else text "\\caption" <>
braces captionText <> "\\tabularnewline"
firsthead <- if all null heads
then return "\\toprule"
else toHeaders heads
let captRepeated = if isEmpty captionText
then empty
else text "\\caption[]" <>
braces captionText <> "\\tabularnewline"
head' <- if all null heads
then return "\\toprule"
-- avoid duplicate notes in head and firsthead:
else toHeaders (if isEmpty firsthead
then heads
else walk removeNote heads)
let capt = if isEmpty captionText
then empty
else text "\\caption" <>
braces captionText <> "\\tabularnewline"
rows' <- mapM (tableRowToLaTeX False aligns widths) rows
let colDescriptors = text $ concatMap toColDescriptor aligns
modify $ \s -> s{ stTable = True }
Expand All @@ -740,6 +744,8 @@ blockToLaTeX (Table caption aligns widths heads rows) = do
-- the @{} removes extra space at beginning and end
$$ capt
$$ firsthead
$$ "\\endfirsthead"
$$ captRepeated
$$ head'
$$ "\\endhead"
$$ vcat rows'
Expand Down
1 change: 1 addition & 0 deletions test/command/2378.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ is used.
x & y\footnote{a footnote}\tabularnewline
\midrule
\endfirsthead
\caption[]{a table}\tabularnewline
\toprule
x & y{}\tabularnewline
\midrule
Expand Down
23 changes: 23 additions & 0 deletions test/tables.latex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Simple table with caption:
Right & Left & Center & Default\tabularnewline
\midrule
\endfirsthead
\caption[]{Demonstration of simple table syntax.}\tabularnewline
\toprule
Right & Left & Center & Default\tabularnewline
\midrule
Expand All @@ -22,6 +23,10 @@ Simple table without caption:
\toprule
Right & Left & Center & Default\tabularnewline
\midrule
\endfirsthead
\toprule
Right & Left & Center & Default\tabularnewline
\midrule
\endhead
12 & 12 & 12 & 12\tabularnewline
123 & 123 & 123 & 123\tabularnewline
Expand All @@ -37,6 +42,7 @@ Simple table indented two spaces:
Right & Left & Center & Default\tabularnewline
\midrule
\endfirsthead
\caption[]{Demonstration of simple table syntax.}\tabularnewline
\toprule
Right & Left & Center & Default\tabularnewline
\midrule
Expand All @@ -63,6 +69,7 @@ Default aligned\strut
\end{minipage}\tabularnewline
\midrule
\endfirsthead
\caption[]{Here's the caption. It may span multiple lines.}\tabularnewline
\toprule
\begin{minipage}[b]{0.13\columnwidth}\centering
Centered Header\strut
Expand Down Expand Up @@ -110,6 +117,18 @@ Right Aligned\strut
Default aligned\strut
\end{minipage}\tabularnewline
\midrule
\endfirsthead
\toprule
\begin{minipage}[b]{0.13\columnwidth}\centering
Centered Header\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\raggedright
Left Aligned\strut
\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\raggedleft
Right Aligned\strut
\end{minipage} & \begin{minipage}[b]{0.31\columnwidth}\raggedright
Default aligned\strut
\end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.13\columnwidth}\centering
First\strut
Expand All @@ -136,6 +155,8 @@ Table without column headers:

\begin{longtable}[]{@{}rlcr@{}}
\toprule
\endfirsthead
\toprule
\endhead
12 & 12 & 12 & 12\tabularnewline
123 & 123 & 123 & 123\tabularnewline
Expand All @@ -147,6 +168,8 @@ Multiline table without column headers:

\begin{longtable}[]{@{}clrl@{}}
\toprule
\endfirsthead
\toprule
\endhead
\begin{minipage}[t]{0.13\columnwidth}\centering
First\strut
Expand Down