7

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):

---
header-includes:
  - \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?

Edit:
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:

\begin{longtable}[]{@{}ll@{}}
\end{longtable}

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):

\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\PassOptionsToPackage{hyphens}{url} % url is loaded by hyperref
\usepackage[unicode=true]{hyperref}
\hypersetup{
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{longtable,booktabs}
% Fix footnotes in tables (requires footnote package)
\IfFileExists{footnote.sty}{\usepackage{footnote}\makesavenoteenv{long table}}{}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

% set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother

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

\date{}

\begin{document}

\begin{longtable}[]{@{}rllc@{}}
\caption{Demonstration of pipe table syntax.}\tabularnewline
\toprule
Right & Left & Default & Center\tabularnewline
\midrule
\endfirsthead
\toprule
Right & Left & Default & Center\tabularnewline
\midrule
\endhead
12 & 12 & 12 & 12\tabularnewline
123 & 123 & 123 & 123\tabularnewline
1 & 1 & 1 & 1\tabularnewline
\bottomrule
\end{longtable}

\end{document}
  • 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
6

like this:

enter image description here

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

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs, longtable}

\begin{document}
\begin{longtable}{@{}   >{\columncolor{white}[0pt][\tabcolsep]}r % <---
                        l
                        l
                        >{\columncolor{white}[\tabcolsep][0pt]}c % <---
                  @{}   }
\caption{Demonstration of pipe table syntax.}\\
    \toprule
Right & Left & Default & Center             \\
    \midrule
\endfirsthead
    \toprule
Right   & Left  & Default   & Center        \\
    \midrule
    \endhead
\rowcolor{gray!10}
12      & 12    & 12        & 12            \\
123     & 123   & 123       & 123           \\
1       & 1     & 1         & 1             \\
\bottomrule
\end{longtable}

\end{document}
  • 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
  • I appreciate your effort very much, no doubt about that! And I just realized that your solution works with any combination of row colors, not only when the first one is white! But my problem is that your solution forces me to break my whole document creation process because I can't directly convert from Markdown to PDF but instead have to compile an intermediary Latex file which I have to adjust for every single zebra-striped table before I can finally convert it to PDF. So it's not a complete solution to my original question... – Salim B Nov 6 '17 at 0:38
  • 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

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.