diff options
-rw-r--r-- | src/Text/Pandoc/Writers/LaTeX.hs | 18 | ||||
-rw-r--r-- | tests/tables.latex | 188 |
2 files changed, 108 insertions, 98 deletions
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index d200ecee1..b2db7377d 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -471,12 +471,18 @@ blockToLaTeX (Header level (id',classes,_) lst) = blockToLaTeX (Table caption aligns widths heads rows) = do headers <- if all null heads then return empty - else ($$ "\\midrule\\endhead") `fmap` + else ($$ "\\midrule\n") `fmap` (tableRowToLaTeX True aligns widths) heads + let endhead = if all null heads + then empty + else text "\\endhead" captionText <- inlineListToLaTeX caption let capt = if isEmpty captionText then empty - else text "\\caption" <> braces captionText <> "\\\\" + else text "\\caption" <> braces captionText + <> "\\tabularnewline\n\\toprule\n" + <> headers + <> "\\endfirsthead" rows' <- mapM (tableRowToLaTeX False aligns widths) rows let colDescriptors = text $ concat $ map toColDescriptor aligns modify $ \s -> s{ stTable = True } @@ -484,8 +490,9 @@ blockToLaTeX (Table caption aligns widths heads rows) = do braces ("@{}" <> colDescriptors <> "@{}") -- the @{} removes extra space at beginning and end $$ capt - $$ "\\toprule\\addlinespace" + $$ "\\toprule" $$ headers + $$ endhead $$ vcat rows' $$ "\\bottomrule" $$ "\\end{longtable}" @@ -512,7 +519,7 @@ tableRowToLaTeX header aligns widths cols = do let scaleFactor = 0.97 ** fromIntegral (length aligns) let widths' = map (scaleFactor *) widths cells <- mapM (tableCellToLaTeX header) $ zip3 widths' aligns cols - return $ hsep (intersperse "&" cells) $$ "\\\\\\addlinespace" + return $ hsep (intersperse "&" cells) <> "\\tabularnewline" -- For simple latex tables (without minipages or parboxes), -- we need to go to some lengths to get line breaks working: @@ -549,7 +556,8 @@ tableCellToLaTeX header (width, align, blocks) = do AlignDefault -> "\\raggedright" return $ ("\\begin{minipage}" <> valign <> braces (text (printf "%.2f\\columnwidth" width)) <> - (halign <> cr <> cellContents <> cr) <> "\\end{minipage}") + (halign <> "\\strut" <> cr <> cellContents <> cr) <> + "\\strut\\end{minipage}") $$ case notes of [] -> empty ns -> (case length ns of diff --git a/tests/tables.latex b/tests/tables.latex index eb665204d..850629499 100644 --- a/tests/tables.latex +++ b/tests/tables.latex @@ -1,166 +1,168 @@ Simple table with caption: \begin{longtable}[c]{@{}rlcl@{}} -\caption{Demonstration of simple table syntax.}\\ -\toprule\addlinespace -Right & Left & Center & Default -\\\addlinespace -\midrule\endhead -12 & 12 & 12 & 12 -\\\addlinespace -123 & 123 & 123 & 123 -\\\addlinespace -1 & 1 & 1 & 1 -\\\addlinespace +\caption{Demonstration of simple table syntax.}\tabularnewline +\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 +1 & 1 & 1 & 1\tabularnewline \bottomrule \end{longtable} Simple table without caption: \begin{longtable}[c]{@{}rlcl@{}} -\toprule\addlinespace -Right & Left & Center & Default -\\\addlinespace -\midrule\endhead -12 & 12 & 12 & 12 -\\\addlinespace -123 & 123 & 123 & 123 -\\\addlinespace -1 & 1 & 1 & 1 -\\\addlinespace +\toprule +Right & Left & Center & Default\tabularnewline +\midrule +\endhead +12 & 12 & 12 & 12\tabularnewline +123 & 123 & 123 & 123\tabularnewline +1 & 1 & 1 & 1\tabularnewline \bottomrule \end{longtable} Simple table indented two spaces: \begin{longtable}[c]{@{}rlcl@{}} -\caption{Demonstration of simple table syntax.}\\ -\toprule\addlinespace -Right & Left & Center & Default -\\\addlinespace -\midrule\endhead -12 & 12 & 12 & 12 -\\\addlinespace -123 & 123 & 123 & 123 -\\\addlinespace -1 & 1 & 1 & 1 -\\\addlinespace +\caption{Demonstration of simple table syntax.}\tabularnewline +\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 +1 & 1 & 1 & 1\tabularnewline \bottomrule \end{longtable} Multiline table with caption: \begin{longtable}[c]{@{}clrl@{}} -\caption{Here's the caption. It may span multiple lines.}\\ -\toprule\addlinespace -\begin{minipage}[b]{0.13\columnwidth}\centering +\caption{Here's the caption. It may span multiple lines.}\tabularnewline +\toprule +\begin{minipage}[b]{0.13\columnwidth}\centering\strut Centered Header -\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\raggedright\strut Left Aligned -\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\raggedleft +\strut\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\raggedleft\strut Right Aligned -\end{minipage} & \begin{minipage}[b]{0.30\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[b]{0.30\columnwidth}\raggedright\strut Default aligned -\end{minipage} -\\\addlinespace -\midrule\endhead -\begin{minipage}[t]{0.13\columnwidth}\centering +\strut\end{minipage}\tabularnewline +\midrule +\endfirsthead +\toprule +\begin{minipage}[b]{0.13\columnwidth}\centering\strut +Centered Header +\strut\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\raggedright\strut +Left Aligned +\strut\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\raggedleft\strut +Right Aligned +\strut\end{minipage} & \begin{minipage}[b]{0.30\columnwidth}\raggedright\strut +Default aligned +\strut\end{minipage}\tabularnewline +\midrule +\endhead +\begin{minipage}[t]{0.13\columnwidth}\centering\strut First -\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright\strut row -\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft +\strut\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft\strut 12.0 -\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright\strut Example of a row that spans multiple lines. -\end{minipage} -\\\addlinespace -\begin{minipage}[t]{0.13\columnwidth}\centering +\strut\end{minipage}\tabularnewline +\begin{minipage}[t]{0.13\columnwidth}\centering\strut Second -\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright\strut row -\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft +\strut\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft\strut 5.0 -\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright\strut Here's another one. Note the blank line between rows. -\end{minipage} -\\\addlinespace +\strut\end{minipage}\tabularnewline \bottomrule \end{longtable} Multiline table without caption: \begin{longtable}[c]{@{}clrl@{}} -\toprule\addlinespace -\begin{minipage}[b]{0.13\columnwidth}\centering +\toprule +\begin{minipage}[b]{0.13\columnwidth}\centering\strut Centered Header -\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\raggedright\strut Left Aligned -\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\raggedleft +\strut\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\raggedleft\strut Right Aligned -\end{minipage} & \begin{minipage}[b]{0.30\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[b]{0.30\columnwidth}\raggedright\strut Default aligned -\end{minipage} -\\\addlinespace -\midrule\endhead -\begin{minipage}[t]{0.13\columnwidth}\centering +\strut\end{minipage}\tabularnewline +\midrule +\endhead +\begin{minipage}[t]{0.13\columnwidth}\centering\strut First -\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright\strut row -\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft +\strut\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft\strut 12.0 -\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright\strut Example of a row that spans multiple lines. -\end{minipage} -\\\addlinespace -\begin{minipage}[t]{0.13\columnwidth}\centering +\strut\end{minipage}\tabularnewline +\begin{minipage}[t]{0.13\columnwidth}\centering\strut Second -\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright\strut row -\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft +\strut\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft\strut 5.0 -\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright\strut Here's another one. Note the blank line between rows. -\end{minipage} -\\\addlinespace +\strut\end{minipage}\tabularnewline \bottomrule \end{longtable} Table without column headers: \begin{longtable}[c]{@{}rlcr@{}} -\toprule\addlinespace -12 & 12 & 12 & 12 -\\\addlinespace -123 & 123 & 123 & 123 -\\\addlinespace -1 & 1 & 1 & 1 -\\\addlinespace +\toprule +12 & 12 & 12 & 12\tabularnewline +123 & 123 & 123 & 123\tabularnewline +1 & 1 & 1 & 1\tabularnewline \bottomrule \end{longtable} Multiline table without column headers: \begin{longtable}[c]{@{}clrl@{}} -\toprule\addlinespace -\begin{minipage}[t]{0.13\columnwidth}\centering +\toprule +\begin{minipage}[t]{0.13\columnwidth}\centering\strut First -\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright\strut row -\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft +\strut\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft\strut 12.0 -\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright\strut Example of a row that spans multiple lines. -\end{minipage} -\\\addlinespace -\begin{minipage}[t]{0.13\columnwidth}\centering +\strut\end{minipage}\tabularnewline +\begin{minipage}[t]{0.13\columnwidth}\centering\strut Second -\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\raggedright\strut row -\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft +\strut\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\raggedleft\strut 5.0 -\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright +\strut\end{minipage} & \begin{minipage}[t]{0.30\columnwidth}\raggedright\strut Here's another one. Note the blank line between rows. -\end{minipage} -\\\addlinespace +\strut\end{minipage}\tabularnewline \bottomrule \end{longtable} |