pandoc
pandoc copied to clipboard
Problem removing table top or bottom rules with latex command in pandoc 2.19
Pandoc 2.19, macOS 12.5
With previous pandoc versions, table top/mid/bottom rules could be removed by including appropriate latex commands under header-includes. The following example with pandoc 2.19 removes top and bottom rules but adds two parentheses (), as shown in attached pdf. Same with grid tables.
---
header-includes:
- \let\toprule\relax
- \let\bottomrule\relax
---
| c1 | c2 | c3 |
|:---|:---|:---|
| r1 | a | b |
| r2 | c | d |
See #8001 for the reasons behind this change, but maybe we can make that work again: \midrule
is always followed by either \endhead
or \endfirsthead
, so the parens can be omitted. Same for \bottomrule
, which will always be followed by \end{longtable}
. This leaves \toprule
: replacing \toprule
with \toprule\relax
seems to have no adverse effects in my tests.
For the time being, you can use \renewcommand{\toprule}[2]{}
etc. to remove the rules.
In this connection we should also consider https://github.com/jgm/pandoc/issues/7421#issuecomment-882497287
Revisiting my decision to add the ()
instead of \relax
: based on tests with test/tables.native, it seems that the reason I did this was because with \midrule\relax
and \bottomrule\relax
we get extra vertical space beneath the midrule and the bottomrule. Does anyone understand why? It seems that using \relax
instead of ()
would perhaps solve both problems, but we don't want this vertical space.
replacing
\toprule
with\toprule\relax
seems to have no adverse effects in my tests.
When I tried this, I got extra white space in the headerless tables in test/tables.native. e.g.

With the current setup we don't get that blank line after the top rule.
Oh. I guess this issue should be closed then. I don't know why I missed the empty line, or why I got a different result.
I'd like to keep this open until I can understand why the \relax
doesn't work. Maybe a TeX guru can help.
I found my mistake: I was experimenting in a raw tex file and had removed the \endhead
command. I get the blank line when I add it back in.
See also #8242.
The reason \relax
does not work is that LaTeX interprets it as part of the table cell. Thus, the headerless table in test/tables.native
is roughly interpreted as
| \relax | | | |
| 12 | 12 | 12 | 12 |
| 123 | 123 | 123 | 123 |
| 1 | 1 | 1 | 1 |
And then \relax
is expanded to nothing, leaving empty first row in the final table. The simplest fix would be to replace it with \noalign{}
which inserts empty vertical box between table rows, thus not affecting the cell content. It is worth noting that \noalign
is a TeX primitive, so it's not idiomatic LaTeX.
\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\begin{document}
\begin{longtable}{@{}lll@{}}
\toprule\noalign{}
\endhead
(1) & 2 & 3 \\
a & b & c \\
\bottomrule\noalign{}
\end{longtable}
\begin{longtable}{@{}lll@{}}
\toprule\noalign{}
(1) & 2 & 3 \\
\endhead
a & b & c \\
\bottomrule\noalign{}
\end{longtable}
\end{document}
It seems like all questions around this issue have been answered. Closing.
I was thinking we should keep this open in order to explore the solution proposed here:
The simplest fix would be to replace it with
\noalign{}
which inserts empty vertical box between table rows, thus not affecting the cell content.
Unless I was misunderstanding and this isn't a potential solution?
Oh, I had missed that!
I've been trying to implement zebra shading in a longtable following the discussion here. I managed to get something that looked good by adding this before the table in my markdown file:
\rowcolors{2}{}{gray!25}
\setlength{\tabcolsep}{0pt}
\apptocmd{\toprule}{\hiderowcolors}{}{}
\apptocmd{\endhead}{\showrowcolors}{}{}
\apptocmd{\endfirsthead}{\showrowcolors}{}{}
The only problem was that two parentheses "()" were added into the top of the table. I then found my way to this thread. Following the discussion here, I discovered that if I used pandoc to compile my markdown file to LaTeX, I could get rid of the parentheses by replacing the instance of "\toprule()" to "\toprule\noalign{}" in the LaTeX file.
I guess my question is: what (and where) can I include in my markdown file so that Pandoc produces a PDF without the parentheses? I mean, I guess I can compile to LaTeX and make that change manually, but it'd be great if there were another way. Thanks!
There's no way to get this fix with pandoc itself. You could pipe output through sed or perl.
I think we should change pandoc to yield \toprule\noalign{}
instead of \toprule()
. That should be a simple fix.
@jpk3333 As a workaround you can also modify your code to look like this
\rowcolors{2}{}{gray!25}
\setlength{\tabcolsep}{0pt}
% \apptocmd{\toprule}{\hiderowcolors}{}{}
\makeatletter
\NewCommandCopy\@oldtoprule\toprule
\RenewExpandableDocumentCommand\toprule{d()m}{\@oldtoprule\hiderowcolors#2}
\makeatother
\apptocmd{\endhead}{\showrowcolors}{}{}
\apptocmd{\endfirsthead}{\showrowcolors}{}{}
though it's a bit hacky, so double check that it works as expected. (You may have to add \usepackage{xparse}
in the preamble if your LaTeX version is old.)
@marcin-serwin It worked! Thanks!