2

Why does the following MWE not work as expected to make the row color alternate?

\documentclass{article}

\usepackage{tabularx,booktabs,tabu,amsmath,colortbl}
\usepackage[table]{xcolor}

\begin{document}

\begin{tabu} to \linewidth {@{}*3{X[c]}@{}}
    \taburowcolors [1] 2{gray!10 .. white}
    \toprule
    5 & 6 & 7\\
    5 & 6 & 7\\
    \bottomrule
\end{tabu}

\end{document}

enter image description here

  • 1
    Hm, try https://github.com/tabu-fixed/tabu/issues. Seems that the link in the comment before returns a broken issues as is%C2%ADsues. That is indeed wrong. – Mensch May 2 at 2:19
3

The manual for the tabu package starts with the warning:

The tabu package patches internal commands of many packages and is liable to stop working if those internal interfaces change. Unfortunately the original author appears to be out of contact and the package had not been updated for some years.

It seems that this warning should be taken seriously when it comes to comes to adding background colour to the rows because the example code for \taburowcolors, which is on page 19 of the manual, does not compile for me.

You can achieve something something similar to what you want using a tabularx environment:

enter image description here

Note, however, that the booktabs package does not work particularly well with \rowcolors as the line heights do not match properly, as is already evident from the image above.

Here is the code:

\documentclass{article}

\usepackage{tabularx,amsmath,booktabs}
\usepackage[table]{xcolor}

\begin{document}

\rowcolors{2}{gray!10}{white}
\begin{tabularx}{\linewidth}{@{}*3{X}@{}}
    \toprule
    5 & 6 & 7\\
    5 & 6 & 7\\
    \bottomrule
\end{tabularx}

\end{document}
  • Thanks for clarifying this to me. I have an off-topic question: why doesn't the grey background stick with exact row width instead of spilling from both sides? – Diaa May 2 at 2:14
  • 2
    @Diaa, because rules from booktabs have some vertical space around them. in answer you have warning, that booktabs' rules doesnt work well with table coloring. as possible solution see Bernard answer on the question tex.stackexchange.com/questions/269547 – Zarko May 2 at 3:04

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.