From 5f0bfd41a80aaf4d05c37357a317a6694ec338ec Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Wed, 30 Mar 2022 10:07:09 -0700
Subject: [PATCH] LaTeX writer: add () after booktabs rules.

These commands take optional arguments with () and [],
which can lead to problems if the content of the table
cell begins with these characters.

Closes #8001.
---
 src/Text/Pandoc/Writers/LaTeX/Table.hs |  6 ++--
 test/command/2378.md                   | 10 +++---
 test/command/5367.md                   | 10 +++---
 test/command/7272.md                   |  4 +--
 test/tables.latex                      | 50 +++++++++++++-------------
 test/tables/nordics.latex              | 10 +++---
 test/tables/planets.latex              | 10 +++---
 test/tables/students.latex             | 10 +++---
 8 files changed, 55 insertions(+), 55 deletions(-)

diff --git a/src/Text/Pandoc/Writers/LaTeX/Table.hs b/src/Text/Pandoc/Writers/LaTeX/Table.hs
index ba9d2fc99..58fd3a251 100644
--- a/src/Text/Pandoc/Writers/LaTeX/Table.hs
+++ b/src/Text/Pandoc/Writers/LaTeX/Table.hs
@@ -52,7 +52,7 @@ tableToLaTeX inlnsToLaTeX blksToLaTeX tbl = do
                else ($$ text "\\endfirsthead") <$>
                     headToLaTeX blksToLaTeX thead
   head' <- if isEmptyHead thead
-           then return "\\toprule"
+           then return "\\toprule()"
            -- avoid duplicate notes in head and firsthead:
            else headToLaTeX blksToLaTeX
                 (if isEmpty firsthead
@@ -71,7 +71,7 @@ tableToLaTeX inlnsToLaTeX blksToLaTeX tbl = do
     $$ head'
     $$ "\\endhead"
     $$ vcat rows'
-    $$ "\\bottomrule"
+    $$ "\\bottomrule()"
     $$ "\\end{longtable}"
     $$ captNotes
     $$ notes
@@ -161,7 +161,7 @@ headToLaTeX :: PandocMonad m
 headToLaTeX blocksWriter (Ann.TableHead _attr headerRows) = do
   rowsContents <- mapM (rowToLaTeX blocksWriter HeaderCell . headerRowCells)
                        headerRows
-  return ("\\toprule" $$ vcat rowsContents $$ "\\midrule")
+  return ("\\toprule()" $$ vcat rowsContents $$ "\\midrule()")
 
 -- | Converts a row of table cells into a LaTeX row.
 rowToLaTeX :: PandocMonad m
diff --git a/test/command/2378.md b/test/command/2378.md
index 59359c653..7b6586366 100644
--- a/test/command/2378.md
+++ b/test/command/2378.md
@@ -13,15 +13,15 @@ is used.
 ^D
 \begin{longtable}[]{@{}ll@{}}
 \caption{a table}\tabularnewline
-\toprule
+\toprule()
 x & y\footnote{a footnote} \\
-\midrule
+\midrule()
 \endfirsthead
-\toprule
+\toprule()
 x & y{} \\
-\midrule
+\midrule()
 \endhead
 1 & 2 \\
-\bottomrule
+\bottomrule()
 \end{longtable}
 ```
diff --git a/test/command/5367.md b/test/command/5367.md
index 427aab913..591ee4b2e 100644
--- a/test/command/5367.md
+++ b/test/command/5367.md
@@ -23,20 +23,20 @@ hello\footnote{doc footnote}
 \begin{longtable}[]{@{}
   >{\centering\arraybackslash}p{(\columnwidth - 0\tabcolsep) * \real{0.1667}}@{}}
 \caption[Sample table.]{Sample table.\footnote{caption footnote}}\tabularnewline
-\toprule
+\toprule()
 \begin{minipage}[b]{\linewidth}\centering
 Fruit\footnote{header footnote}
 \end{minipage} \\
-\midrule
+\midrule()
 \endfirsthead
-\toprule
+\toprule()
 \begin{minipage}[b]{\linewidth}\centering
 Fruit{}
 \end{minipage} \\
-\midrule
+\midrule()
 \endhead
 Bans\footnote{table cell footnote} \\
-\bottomrule
+\bottomrule()
 \end{longtable}
 
 dolly\footnote{doc footnote}
diff --git a/test/command/7272.md b/test/command/7272.md
index cea282a10..bb184ad4b 100644
--- a/test/command/7272.md
+++ b/test/command/7272.md
@@ -16,12 +16,12 @@
 ^D
 \begin{longtable}[]{@{}
   >{\raggedright\arraybackslash}p{(\columnwidth - 0\tabcolsep) * \real{1.0000}}@{}}
-\toprule
+\toprule()
 \endhead
 \begin{minipage}[t]{\linewidth}\raggedright
 { text\\
 text2 }\strut
 \end{minipage} \\
-\bottomrule
+\bottomrule()
 \end{longtable}
 ```
diff --git a/test/tables.latex b/test/tables.latex
index eba77c7b9..0683ce15f 100644
--- a/test/tables.latex
+++ b/test/tables.latex
@@ -2,49 +2,49 @@ Simple table with caption:
 
 \begin{longtable}[]{@{}rlcl@{}}
 \caption{Demonstration of simple table syntax.}\tabularnewline
-\toprule
+\toprule()
 Right & Left & Center & Default \\
-\midrule
+\midrule()
 \endfirsthead
-\toprule
+\toprule()
 Right & Left & Center & Default \\
-\midrule
+\midrule()
 \endhead
 12 & 12 & 12 & 12 \\
 123 & 123 & 123 & 123 \\
 1 & 1 & 1 & 1 \\
-\bottomrule
+\bottomrule()
 \end{longtable}
 
 Simple table without caption:
 
 \begin{longtable}[]{@{}rlcl@{}}
-\toprule
+\toprule()
 Right & Left & Center & Default \\
-\midrule
+\midrule()
 \endhead
 12 & 12 & 12 & 12 \\
 123 & 123 & 123 & 123 \\
 1 & 1 & 1 & 1 \\
-\bottomrule
+\bottomrule()
 \end{longtable}
 
 Simple table indented two spaces:
 
 \begin{longtable}[]{@{}rlcl@{}}
 \caption{Demonstration of simple table syntax.}\tabularnewline
-\toprule
+\toprule()
 Right & Left & Center & Default \\
-\midrule
+\midrule()
 \endfirsthead
-\toprule
+\toprule()
 Right & Left & Center & Default \\
-\midrule
+\midrule()
 \endhead
 12 & 12 & 12 & 12 \\
 123 & 123 & 123 & 123 \\
 1 & 1 & 1 & 1 \\
-\bottomrule
+\bottomrule()
 \end{longtable}
 
 Multiline table with caption:
@@ -55,7 +55,7 @@ Multiline table with caption:
   >{\raggedleft\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.1625}}
   >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.3500}}@{}}
 \caption{Here's the caption. It may span multiple lines.}\tabularnewline
-\toprule
+\toprule()
 \begin{minipage}[b]{\linewidth}\centering
 Centered Header
 \end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
@@ -65,9 +65,9 @@ Right Aligned
 \end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
 Default aligned
 \end{minipage} \\
-\midrule
+\midrule()
 \endfirsthead
-\toprule
+\toprule()
 \begin{minipage}[b]{\linewidth}\centering
 Centered Header
 \end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
@@ -77,11 +77,11 @@ Right Aligned
 \end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
 Default aligned
 \end{minipage} \\
-\midrule
+\midrule()
 \endhead
 First & row & 12.0 & Example of a row that spans multiple lines. \\
 Second & row & 5.0 & Here's another one. Note the blank line between rows. \\
-\bottomrule
+\bottomrule()
 \end{longtable}
 
 Multiline table without caption:
@@ -91,7 +91,7 @@ Multiline table without caption:
   >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.1375}}
   >{\raggedleft\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.1625}}
   >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.3500}}@{}}
-\toprule
+\toprule()
 \begin{minipage}[b]{\linewidth}\centering
 Centered Header
 \end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
@@ -101,22 +101,22 @@ Right Aligned
 \end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
 Default aligned
 \end{minipage} \\
-\midrule
+\midrule()
 \endhead
 First & row & 12.0 & Example of a row that spans multiple lines. \\
 Second & row & 5.0 & Here's another one. Note the blank line between rows. \\
-\bottomrule
+\bottomrule()
 \end{longtable}
 
 Table without column headers:
 
 \begin{longtable}[]{@{}rlcr@{}}
-\toprule
+\toprule()
 \endhead
 12 & 12 & 12 & 12 \\
 123 & 123 & 123 & 123 \\
 1 & 1 & 1 & 1 \\
-\bottomrule
+\bottomrule()
 \end{longtable}
 
 Multiline table without column headers:
@@ -126,9 +126,9 @@ Multiline table without column headers:
   >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.1375}}
   >{\raggedleft\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.1625}}
   >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.3500}}@{}}
-\toprule
+\toprule()
 \endhead
 First & row & 12.0 & Example of a row that spans multiple lines. \\
 Second & row & 5.0 & Here's another one. Note the blank line between rows. \\
-\bottomrule
+\bottomrule()
 \end{longtable}
diff --git a/test/tables/nordics.latex b/test/tables/nordics.latex
index 4c10865dc..a5e7bc1be 100644
--- a/test/tables/nordics.latex
+++ b/test/tables/nordics.latex
@@ -4,7 +4,7 @@
   >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.2000}}
   >{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.2000}}@{}}
 \caption{States belonging to the \emph{Nordics.}}\tabularnewline
-\toprule
+\toprule()
 \begin{minipage}[b]{\linewidth}\centering
 Name
 \end{minipage} & \begin{minipage}[b]{\linewidth}\centering
@@ -16,9 +16,9 @@ Population\\
 Area\\
 (in km\textsuperscript{2})\strut
 \end{minipage} \\
-\midrule
+\midrule()
 \endfirsthead
-\toprule
+\toprule()
 \begin{minipage}[b]{\linewidth}\centering
 Name
 \end{minipage} & \begin{minipage}[b]{\linewidth}\centering
@@ -30,7 +30,7 @@ Population\\
 Area\\
 (in km\textsuperscript{2})\strut
 \end{minipage} \\
-\midrule
+\midrule()
 \endhead
 Denmark & Copenhagen & 5,809,502 & 43,094 \\
 Finland & Helsinki & 5,537,364 & 338,145 \\
@@ -38,5 +38,5 @@ Iceland & Reykjavik & 343,518 & 103,000 \\
 Norway & Oslo & 5,372,191 & 323,802 \\
 Sweden & Stockholm & 10,313,447 & 450,295 \\
 Total & & 27,376,022 & 1,258,336 \\
-\bottomrule
+\bottomrule()
 \end{longtable}
diff --git a/test/tables/planets.latex b/test/tables/planets.latex
index 1e703143d..c7fcfe1cf 100644
--- a/test/tables/planets.latex
+++ b/test/tables/planets.latex
@@ -1,16 +1,16 @@
 \begin{longtable}[]{@{}cclrrrrrrrrl@{}}
 \caption{Data about the planets of our solar system.}\tabularnewline
-\toprule
+\toprule()
 \multicolumn{2}{c}{} & Name & Mass (10\^{}24kg) & Diameter (km) & Density
 (kg/m\^{}3) & Gravity (m/s\^{}2) & Length of day (hours) & Distance from Sun
 (10\^{}6km) & Mean temperature (C) & Number of moons & Notes \\
-\midrule
+\midrule()
 \endfirsthead
-\toprule
+\toprule()
 \multicolumn{2}{c}{} & Name & Mass (10\^{}24kg) & Diameter (km) & Density
 (kg/m\^{}3) & Gravity (m/s\^{}2) & Length of day (hours) & Distance from Sun
 (10\^{}6km) & Mean temperature (C) & Number of moons & Notes \\
-\midrule
+\midrule()
 \endhead
 \multicolumn{2}{c}{\multirow{4}{*}{Terrestrial planets}} & Mercury & 0.330 &
 4,879 & 5427 & 3.7 & 4222.6 & 57.9 & 167 & 0 & Closest to the Sun \\
@@ -26,5 +26,5 @@ planet \\
 & & Neptune & 102 & 49,528 & 1638 & 11.0 & 16.1 & 4495.1 & -200 & 14 & \\
 \multicolumn{2}{c}{Dwarf planets} & Pluto & 0.0146 & 2,370 & 2095 & 0.7 & 153.3
 & 5906.4 & -225 & 5 & Declassified as a planet in 2006. \\
-\bottomrule
+\bottomrule()
 \end{longtable}
diff --git a/test/tables/students.latex b/test/tables/students.latex
index b1ea45ac8..c256c289c 100644
--- a/test/tables/students.latex
+++ b/test/tables/students.latex
@@ -2,21 +2,21 @@
   >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.5000}}
   >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.5000}}@{}}
 \caption{List of Students}\tabularnewline
-\toprule
+\toprule()
 \begin{minipage}[b]{\linewidth}\centering
 Student ID
 \end{minipage} & \begin{minipage}[b]{\linewidth}\centering
 Name
 \end{minipage} \\
-\midrule
+\midrule()
 \endfirsthead
-\toprule
+\toprule()
 \begin{minipage}[b]{\linewidth}\centering
 Student ID
 \end{minipage} & \begin{minipage}[b]{\linewidth}\centering
 Name
 \end{minipage} \\
-\midrule
+\midrule()
 \endhead
 \multicolumn{2}{l}{Computer Science} \\
 3741255 & Jones, Martha \\
@@ -27,5 +27,5 @@ Name
 \multicolumn{2}{l}{Astrophysics} \\
 4100332 & Petrov, Alexandra \\
 4100332 & Toyota, Hiroko \\
-\bottomrule
+\bottomrule()
 \end{longtable}