3

Using xcolor.\rowcolors together with booktabs.\midrule I have encountered some weird behaviour. To make it plain: the point is not whether or why the tables look so utterly ugly ...

In the MWE there are 5 equal tables displaying two major problems:

  1. Row numbers of the table do not start with 1 and
  2. Row numbers are sometimes influenced by the \midrule command (not, however, by \hline)
  • Table 1, using tabularx, does the coloring correctly, but the rows start at 17. I suspect, this is twice the rowcount + columncount, but I can't prove that. No idea what's happening here. Normally you would not even realise there's something wrong, unless you display the row number. (Although I had been able to create a table starting at 22 and thus just inverting the coloring scheme, but I don't remember what I did for that.)
  • Table 2, same as tab 1, but placing \global\rownum=-1 at the end of the last row. Now the row numbering is as expected.
  • Table 3, using longtable. Does not need the rownum correction at the end, but without the code after row 3 \global\advance\rownum by -1 you get the same result as shown in tab 4.
  • Table 4, using xltabular, shows what caused this question: \midrule influences the rowcount, or probably more accurate, the table algortithm misinterprets the line as a row. So the rows are 1,2,3,5,6... Also xltabular needs, contrary to longtable, the correction at the end of the last row to get correct row numbers.
  • Table 5, same as tab 4, except adding the correction of rownum before midrule at the end of row 3.

Though I actually "solved" the problems I have a rather bad feeling. Am I doing something dreadfully wrong? If so, what? Or are there bugs kreeping around? If so, who does the mischief? I'm not TeXically enough to figure that out.
I have also played with the order of \usepackage, to no effect, though. (And by the way I'm working with texlive 2018 and Win7pro or 10pro. Compilation with pdflatex and lualatex produce the same output.)

Thanks for any hints.

Following the MWE and the image of its output.

Changes marked in the code, new output picture added.

Final version solving the problem. Input: Ulrike Fischer, Apr. 2017: \noalign{\global\rownum=1}

\documentclass [b5paper]{article} 
\listfiles
\usepackage {booktabs}
\usepackage {xltabular}
\usepackage [table]{xcolor}
% patch according to Zarko
%%%% was good input, but did not really help in real live -> removed again
%%%%\newcounter{tabrows}
%%%%\expandafter\let\csname c@tabrows\endcsname\rownum
% end patch

%%%% still valid
% also removed all `\global\rownum=-1` commands and 
%%%% no longer needed, as long as same coloring scheme for all tables
%%%%      added `\rowcolors` in front all each table

\begin {document}
\vspace*{-7 \baselineskip} % to place all on one page
\rowcolors {1}{gray!20}{}  % odds grey, evens white

% --- using tabularx -----------------------------------------------------------
\centering
\begin {tabularx}{0.6\textwidth}{@{}ccX@{}}
   %%%% this table does NOT start row numbers at 1
   % code added
   \toprule  
   % end added
   \rowcolor {red!10!white}
   Table 1 &  \textbf{tabularx} & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   \midrule
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
\end {tabularx}

\vspace{\baselineskip}
%%%%\rowcolors {1}{gray!20}{}  % odds grey, evens white
\begin {tabularx}{0.6\textwidth}{@{}ccX@{}}
   \noalign{\global\rownum=1} % could also be 2 or any arbitrary value
   \rowcolor {red!10!white}
   Table 2 &  \textbf{tabularx} & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   \midrule
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
\end {tabularx}

% --- using longtable ----------------------------------------------------------
%%%%\rowcolors {1}{gray!20}{}  % odds grey, evens white
\begin {longtable}{@{}ccp{3.5cm}@{}}
   % code added
   \toprule\endhead   % \endhead because of underfull \vbox waring 
   % end added
   \noalign{\global\rownum=1}
   \rowcolor {red!10!white}
   Table 3 &  \textbf{longtable} & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum \global\advance\rownum by -1\\
   \midrule
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
\end {longtable}

% --- using xltabular ----------------------------------------------------------
\rowcolors {1}{gray!20}{}  % odds grey, evens white
\begin {xltabular}{0.6\textwidth}{@{}ccX@{}}
   % code added
   \toprule\endhead
   % end added
   \noalign{\global\rownum=1}
   \rowcolor {red!10!white}
   Table 4 &  \textbf{xltabular} & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   \midrule
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
\end {xltabular}

\rowcolors {1}{gray!20}{}  % odds grey, evens white
\begin {xltabular}{0.6\textwidth}{@{}ccX@{}}
   \noalign{\global\rownum=1}
   \rowcolor {red!10!white}
   Table 5 &  \textbf{xltabular} & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum \global\advance\rownum by -1\\
   \midrule
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
   S1 & S2 & Row \# \the\rownum\\
\end {xltabular}
\end {document}

Result of the MWE

Result after changes

2
  • Welcome to TeX.SX
    – Elad Den
    Mar 22 at 15:01
  • Try the following fix. After loading xcolor add \newcounter{tblerows}% see https://tex.stackexchange.com/questions/297345/ % why-is-the-start-row-of-rowcolors-ignored-in-tabularx \expandafter\let\csname c@tblerows\endcsname\rownum . (I'm not author of fix)
    – Zarko
    Mar 22 at 15:28
1

Actually, this is not a answer to the question but maybe you will be interested to see that with the environment {NiceTabular} of nicematrix, you have also a command \rowcolors (available in the \CodeBefore) and a counter for the rows (the LaTeX counter iRow) and you have directly the expected result when using in conjonction with booktabs.

\documentclass{article} 
\usepackage{booktabs}
\usepackage{nicematrix}

\begin{document}
\centering
\begin{NiceTabular}{@{}ccc@{}}
\CodeBefore
   \rowcolors{1}{gray!20}{}
\Body
   Table 1 &  \textbf{NiceTabular} & Row \# \arabic{iRow}\\
   S1 & S2 & Row \# \arabic{iRow}\\
   S1 & S2 & Row \# \arabic{iRow}\\
   \midrule
   S1 & S2 & Row \# \arabic{iRow}\\
   S1 & S2 & Row \# \arabic{iRow}\\
   S1 & S2 & Row \# \arabic{iRow}\\
   S1 & S2 & Row \# \arabic{iRow}\\
\end{NiceTabular}
\end{document}

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

Output of the above code

3
  • Thanks for the hint. I have gone through the (excellent!) documentation of nicematrix and seen lots of goodies. Unfortunately it does not compile with my 2018 texlive installation, although I have installed the newest version of nicematix. — As drawbacks I conceive that I cannot create p-columns (only wx with x=lrc) and the package obviously does not support long tables (tikz, as far as I know, cannot produce multipage pictures).
    – wasadu
    Mar 23 at 15:03
  • 1
    You are right: nicematrix has no feature similar to longtable in order to break long tables... (maybe in a future version...). Mar 23 at 15:36
  • @wasadu: You can use p-columns in {NiceTabular}... Mar 24 at 11:26
0

In the meantime I have stumbled over an input from Ulrike Fischer, resetting the \rownum after headers/footers in longtable or xltabular. I have edited my MWE accordingly, adding the code \noalign{\global\rownum=1} at the appropriate location.

This solution seems to work in real tables with any arbitrary header, in contrast to the patch which did not. Not entered in my MWE are the commands \hiderowcolors (right after the preamble of the table) and \showrowcolors (after the final end of header/footer) which most likely are desirable in real applications.

Not really solved is the second part of my question. But as the botch with \global\advance\rownum by -1 at least corrects the visible aspect of the problem (not alternating the colour), I leave it at that.

0

You may try the new LaTeX3 package tabularray: everything works well as expected.

\documentclass[b5paper]{article} 

\usepackage[a6paper,margin=1.5cm]{geometry}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{longtblr}[
  caption = {Long Table},
  label = {tblr:test},
]{
  width = 0.7\textwidth,
  colspec = {@{}ccX@{}},
  row{odd} = {gray9},
  row{1} = {red9},
  rowhead = 1,
}
  \toprule
  Head & Head & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
  \midrule
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
  \midrule
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
  \midrule
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
  \midrule
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
  \midrule
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
  \midrule
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
  \midrule
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
  \midrule
    S1 & S2   & Row \# \arabic{rownum} \\
    S1 & S2   & Row \# \arabic{rownum} \\
  \bottomrule
\end{longtblr}

\end{document}

enter image description here

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.