I'd like to have zebra striped table rows in the PDF output converted from Markdown using Pandoc. To create Latex tables Pandoc relies on the packages booktabs and longtable. Therefore I tried to redefine the longtable environment. Here's a MWE (Markdown file content):

  - \usepackage[table]{xcolor}
  - \definecolor{lightgray}{gray}{0.95}
  - \let\OldLongtable\longtable
  - \let\OldEndLongtable\endlongtable
  - \renewenvironment{longtable}{\rowcolors{1}{white}{lightgray}\OldLongtable}{\OldEndLongtable}

| Right | Left | Default | Center |
|   12  |  12  |    12   |    12  |
|  123  |  123 |   123   |   123  |
|    1  |    1 |     1   |     1  |

  : Demonstration of pipe table syntax.

The problem now is that the row coloring is wider than the actual table:

enter image description here

How can I fix this?

I've stumbled upon some information concerning this issue. The author of a Pandoc Latex Template called Eisvogel states:

Unfortunately the colored cells extend beyond the edge of the table because pandoc uses @-expressions (@{}) like so:


Additionally he links to the Latex Wikibook section about @-expressions. But to the Latex novice I am this is all as clear as mud...

Edit 2:
As requested by Zarko here's the same as a complete Latex document (output from Pandoc's Markdown-to-Latex conversion):

\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\else % if luatex or xelatex
% use upquote if available, for straight quotes in verbatim environments
% use microtype if available
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
\PassOptionsToPackage{hyphens}{url} % url is loaded by hyperref
            pdfborder={0 0 0},
\urlstyle{same}  % don't use monospace font for urls
% Fix footnotes in tables (requires footnote package)
\IfFileExists{footnote.sty}{\usepackage{footnote}\makesavenoteenv{long table}}{}
}{% else
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
% Redefines (sub)paragraphs to behave more like sections

% set default figure placement to htbp




\caption{Demonstration of pipe table syntax.}\tabularnewline
Right & Left & Default & Center\tabularnewline
Right & Left & Default & Center\tabularnewline
12 & 12 & 12 & 12\tabularnewline
123 & 123 & 123 & 123\tabularnewline
1 & 1 & 1 & 1\tabularnewline

  • we can't help you if you not provide complete small document beginning with \documentclass{...} and ending with \end{document}, which will reproduce your problem. welcome to tex.se!
    – Zarko
    Sep 30 '17 at 22:53
  • @Zarko I've added Latex output from Pandoc, hope that helps :)
    – Salim B
    Sep 30 '17 at 23:46
  • a litle bit :-). see if replacing \begin{longtable}[]{@{}rllc@{}} with \begin{longtable}[]{@{} >{\columncolor{white}[0pt][\tabcolsep]r ll >{\columncolor{white}[0pt][\tabcolsep] c@{}} helps. for testing i temporary haven't time :-(
    – Zarko
    Nov 5 '17 at 15:43
  • @ Zarko Thanks for your answer! But it seems there's a } missing somewhere. What you've proposed throws the error: unexpected "{" expecting letter or lf new-line \end{longtable} ^
    – Salim B
    Nov 5 '17 at 21:23
  • indeed, they are missing :-(. before columns type: \begin{longtable}[]{@{} >{\columncolor{white}[0pt][\tabcolsep]}r ll >{\columncolor{white}[0pt][\tabcolsep]} c@{}}. now should work
    – Zarko
    Nov 5 '17 at 21:26

like this:

enter image description here

i extract from yours code minimal working example, where i add code which should solve your problem:

\usepackage{booktabs, longtable}

\begin{longtable}{@{}   >{\columncolor{white}[0pt][\tabcolsep]}r % <---
                        >{\columncolor{white}[\tabcolsep][0pt]}c % <---
                  @{}   }
\caption{Demonstration of pipe table syntax.}\\
Right & Left & Default & Center             \\
Right   & Left  & Default   & Center        \\
12      & 12    & 12        & 12            \\
123     & 123   & 123       & 123           \\
1       & 1     & 1         & 1             \\

  • Thanks a lot, this looks promising! But am I right on the assumption that this solution is tailored to the first one of the zebra colors (white) I've specified? It would be awesome if a generic solution could be found that ideally could be incorporated into Pandoc (or it's default Latex template) :)
    – Salim B
    Nov 5 '17 at 23:49
  • i only demonstrate principle (on one row) how to solve problem of coloring tables when you in specification of columns use on beginning and on the end @{}. this you can simply extend coloring each second row as is enabled by xcolor package. i newer use pandoc, so i haven't any clue, if integration require something special. from your example i can conclude, that you only need to add code lines indicated by % <---. lets say, that problem with \rowcolor{...} is solved, and left integration this solution to \pandoc` left to new question (if it is not straightforward).
    – Zarko
    Nov 6 '17 at 0:05
  • 1
    if it can't be incorporated, than the simple solution is : not use @{} on beginning and end of column specification :-)
    – Zarko
    Nov 6 '17 at 0:47
  • 2
    @SalimB, @{} on beginning and on the end remove \tabcolsep distance of the cell content from start of the first column and end of the last column.
    – Zarko
    Nov 6 '17 at 1:58
  • 1
    @ Zarko Yes and no. Your answer is helpful, no doubt, therefore I accepted it. But my original problem is still not solved, i.e. I can't produce zebra colored tables with correct coloring width directly out of Pandoc. Since the issue is known to the Pandoc devs, I hope there will be a solution eventually...
    – Salim B
    Nov 27 '17 at 0:34

As of today this is still an issue and @Zarko 's answer cannot be solved through a pandoc filter because it is a writer issue. The script below is a (crude) solution to fix the table colors that can be integrated into a pipeline:

#!/usr/bin/env python

fix-table-color-bleed.py: take a file as argument or input from stdin,
remove @{} from table headers and print the result on stdout 

import sys
from re import sub

def fix_table_color_bleed(infile):

   Remove @{} from latex table entries and print on stdout

   fixedfile = sub("(\\\\begin\{longtable\}\[\]\{)\@\{\}(.*?)\@\{\}",
        "\\1\\2", infile)

if __name__ == "__main__":
  if len(sys.argv) > 1:
     infile = open(sys.argv[1])
     filecontens = infile.read()
     filecontens = sys.stdin.read()


The (\\\\begin\{longtable\}\[\]\{)\@\{\}(.*?)\@\{\} part is the regex pattern to capture and remove the @{} sequence. You can use this script by supplying a tex file as argument. To use it in a pipeline you can use for example:

pandoc -s --from markdown --to latex <inputfile>.md | fix-table-color-bleed.py <outputfile>.tex
  • Welcome to tex.sx. Feb 16 '21 at 14:22
  • thank you :) I'm 4 years late to the party but I hope my answer might help someone in the future Feb 16 '21 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.