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:
- Row numbers of the table do not start with 1 and
- 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}
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)