14

Seems like \rowcolors when used for a {tabularx} doesn’t work properly: The specific number of the first row to be colored is ignored. This is not the case for a normal {tabular}.

enter image description here

In both tables the coloring should start in the third row.

\documentclass{article}

\usepackage{tabularx}
\usepackage[table]{xcolor}

\begin{document}

\begin{minipage}{0.45\textwidth}
   \verb|{tabularx}| \\[2ex]
   \rowcolors{3}{blue!50}{yellow!60}
   \begin{tabularx}{40mm}{X}
      1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\
   \end{tabularx}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
   \verb|{tabular}| \\[2ex]
   \rowcolors{3}{blue!50}{yellow!60}
   \begin{tabular}{p{40mm}}
      1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\
   \end{tabular}
\end{minipage}

\end{document}

Is this a bug in xcolor/tabularx?


This question is kind of a follow-up question of my answer on Table cell with unwanted black background

1
  • 1
    In case of tabularx it is a political act: an Ukrainian flag is added if first two rows. ;-) Mar 4, 2016 at 19:04

2 Answers 2

12

Obviously tabularx doesn't have bugs, this is a documented feature.

enter image description here

TX sets the table several times, and restores latex counters but tex primitive counters can not be restored so easily. This defines a latex counter to shadow the \newcount allocated counter defined by xcolor.

\documentclass{article}

\usepackage{tabularx}
\usepackage[table]{xcolor}
\newcounter{tblerows}
\expandafter\let\csname c@tblerows\endcsname\rownum


\begin{document}

\begin{minipage}{0.45\textwidth}
   \verb|{tabularx}| \\[2ex]
   \rowcolors{3}{blue!50}{yellow!60}
   \begin{tabularx}{40mm}{X}
      1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\
   \end{tabularx}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
   \verb|{tabular}| \\[2ex]
   \rowcolors{3}{blue!50}{yellow!60}
   \begin{tabular}{p{40mm}}
      1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\
   \end{tabular}
\end{minipage}

\end{document}
6
  • Thanks for your answer. But what do you mean by “documented”? Niether a search for rowcolor nor color (or color) shows result in the manual (that’s what I checkt before asking this question …). And how can you beside there are no bug (just asking for curiosity …)
    – Tobi
    Mar 4, 2016 at 20:29
  • 1
    @Tobi mostly it was a joke, it's my code so clearly I believe it's bug free. Actually the fact that plain tex counters are not restored isn't as documented as i remembered. it is documented that latex counters are restored. tabularx was written long before rowcolor (or even color package) existed so you wouldn't see anything specifically about that, although it's not really related to color just that the counter increases by 1 every row but is not reset so by the final setting it is already much bigger than 3 before the table starts. Mar 4, 2016 at 20:40
  • 1
    I see :-) I wonder if the should be a “fix” for this non-bug in either of the packages since both are quite popular …
    – Tobi
    Mar 6, 2016 at 9:37
  • @David Carlisle Well this comment is not directly related to the question but rather to the code you have written. Why \makeatletter and \makeatother were not used when you defined the commands \expandafter\let\csname c@tblerows\endcsname\rownum as @ can be seen in that line? Are there special cases where \makeatletter and \makeatother are not necessary?
    – hesham
    Jan 28, 2019 at 12:33
  • 1
    @hesham I just used csname there as an alternative I could have used \makeatletter then used \c@tblerows as if @ is a letter that parses as a singe csname, but if @ is a non-letter then ` \c@tblerows parses as \c @tblerows so would put a cedilla accent on an @... but you can still construct the token via \csname even with non-letters. Jan 28, 2019 at 14:15
1

For information, with {NiceTabular} and {NiceTabularX} of nicematrix (and its built-in command \rowcolors available in the \CodeBefore), you have directly the expected result.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{xcolor}

\begin{document}

\begin{minipage}{0.45\textwidth}
   \verb|{NiceTabularX}| \\[2ex]
   \begin{NiceTabularX}{40mm}{X}
   \CodeBefore
      \rowcolors{3}{blue!50}{yellow!60}
   \Body
      1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\
   \end{NiceTabularX}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
   \verb|{NiceTabular}| \\[2ex]
   \begin{NiceTabular}{p{40mm}}
   \CodeBefore
      \rowcolors{3}{blue!50}{yellow!60}
   \Body
      1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\
   \end{NiceTabular}
\end{minipage}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .