Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This should be a simple question. I am using \rowcolors{1}{}{lightgray} to alternate coloring of rows in a table.

Is there a way to "restart" the re-coloring after a certain point so that I ensure that regardless of how many rows are above, I can guarantee that the alternating behavior resumes starting with a given, pre-set row?

MWE:

\documentclass[11pt]{article}
\usepackage{threeparttable}             % tables with footnotes, capions all the same width
\usepackage{dcolumn}                    % decimal-aligned tabular math columns
\usepackage{multirow}                   % Allow table cells to span multiple rows
\usepackage{booktabs}                   % Formatting options for publication-quality tables
\usepackage{ltxtable}                   % long tabularx
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\begin{document}
\rowcolors{1}{}{lightgray}
\LTXtable{\textwidth}{testtable}
\end{document}

testtable.tex:

\begin{longtable}{p{3cm} X}
    \toprule
    Heading row & This should be lightgrey \\
    \hline % I use \hline because \midrule counts as a row and screws with the colors
    A data row & This row should be uncolored \\
    A data row & This row should be lightgrey \\
    A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
    \hline
    A new heading row & This should be lightgrey regardless of whether the previous row exists.
\end{longtable}
share|improve this question
    
You can place \rowcolors{1}{}{lightgray} inside the table, just before the point in which the coloring should "restart". –  Gonzalo Medina Apr 9 at 21:50
    
@GonzaloMedina That's not compiling, unfortunately. –  Arkamis Apr 9 at 21:53
    
sure it is in the following case: \documentclass{article} \usepackage[table]{xcolor} \begin{document} \rowcolors{1}{}{lightgray} \noindent\begin{tabular}{cc} textc1 & textc2 \\ textc1 & textc2 \\ textc1 & textc2 \\ textc1 & textc2 \\ \rowcolors{1}{}{lightgray} textc1 & textc2 \\ textc1 & textc2 \\ textc1 & textc2 \\ textc1 & textc2 \\ \end{tabular} \end{document} See how important a MWE can be? –  Gonzalo Medina Apr 9 at 21:56
    
I should say "it's not working". It's 6 PM. Brain also not working. –  Arkamis Apr 9 at 21:56
    
I suppose it could be something with the ltxtable package. Your MWE works. That intervention in my own does not. –  Arkamis Apr 9 at 21:58

2 Answers 2

up vote 5 down vote accepted

Package xcolor remembers the state of the colored row via count register \rownum. This can be reset to the initial value via:

\global\rownum=0\relax

It is too late to change the count register in the current row. Thus it needs to be done in a cell of the previous row.

The following example adds a LaTeX interface for \rownum to get macros \setcounter, \addtocounter, and \therownum working. Then the counter is reset in the row right before the lines, marked with restart:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array}

% LaTeX counter interface for \rownum
% ---
\makeatletter
\@ifundefined{c@rownum}{%
  \let\c@rownum\rownum
}{}
\@ifundefined{therownum}{%
  \def\therownum{\@arabic\rownum}%
}{}
\makeatother

\begin{document}
\rowcolors{1}{yellow}{red}
\begin{tabular}{>{\the\rownum}ll}
\hline
&\\
&\\
&\\
&\setcounter{rownum}{0}\\
\hline
\hline
& restart\\
&\\
&\setcounter{rownum}{0}\\
\hline
\hline
& restart\\
&\setcounter{rownum}{0}\\
\hline
\hline
& restart\\
&\\
&\\
\hline
\end{tabular}

\end{document}

Result

Remark:

  • \rowcolors{1}{yellow}{red} means, start with the first row with the altering row colors. the first color is used for odd numbered rows, the second color for even numbered rows.

Package longtable

Much more "fun" is package longtable. It takes LTchunksize rows for measuring the table cell widths. Therefore a row can be processed several times. This affect \rownum. The previous example with \usepackage{longtable} and longtable instead of tabular:

Result

Or with \setcounter{LTchunksize}{1} (and several LaTeX runs):

Result LTchunksize=1


Already longtable causes trouble with the row numbering, therefore it will not become better with ltxtable, because it is based on longtable. In the example below, the first row gets number 2, thus the first argument of \rowcolors will not work reliable unless it is set to 1.

To some degree the "resetting" is working, if it is done in the previous row. However the first row should be white not lightgray according to \rowcolors and the row numbering is not working correctly.

\RequirePackage{filecontents}
\begin{filecontents}{testtable1.tex}
\begin{longtable}{>{\the\rownum.}r p{3.5cm} X}
    \toprule
    & Heading row & This should be lightgrey \\
    \hline
    & A data row & This row should be uncolored \\
    & A data row & This row should be lightgrey \\
    & A data row & This row sometimes doesn't exist.
                   It would be uncolored if it did
    \restartrowcolors\\
    \midrule
    & A new heading row & This should be lightgrey regardless
      of whether the previous row exists.\\
    & A data row & This should be uncolored \\
    & A data row & This should be lightgrey
\end{longtable}
\end{filecontents}

\begin{filecontents}{testtable2.tex}
\begin{longtable}{>{\the\rownum.}r p{3.5cm} X >{\the\rownum}l}
    \toprule
    & Heading row & This should be lightgrey \\
    \hline
    & A data row & This row should be uncolored \\
    & A data row & This row should be lightgrey 
    \restartrowcolors\\
    \midrule
    & A new heading row & This should be lightgrey regardless
      of whether the previous row exists.\\
    & A data row & This should be uncolored \\
    & A data row & This should be lightgrey
\end{longtable}
\end{filecontents}

\documentclass[11pt]{article}
\usepackage{booktabs}                   % Formatting options for publication-quality tables
\usepackage{ltxtable}                   % long tabularx
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}

\newcommand*{\restartrowcolors}{%
  \ifhmode\unskip\fi
  \vadjust{%
    \global\rownum=0 %
  }%
}

\begin{document}
\rowcolors{1}{}{lightgray}
\LTXtable{\textwidth}{testtable1}
\LTXtable{\textwidth}{testtable2}
\end{document}

Result

share|improve this answer
    
I can get this working, but not when I convert the hlines to midrules. –  Arkamis Apr 10 at 15:37
    
Ah, I got it to work! I simply used %A data row & This row sometimes doesn't exist. It would be uncolored if it did\\ \noalign{\global\rownum=1}% \midrule instead of hline –  Arkamis Apr 10 at 15:44
    
@Arkamis: The problem is not \hline vs. \midrule, but the row numbering, if package longtable is involved. I have updated and fixed the answer. –  Heiko Oberdiek Apr 10 at 18:57

You cam simply say rowcolor{lightgray} just before the row that you want to be colored regardless of your row color definition earlier.

If previous row doesn't exist:

\documentclass[11pt]{article}
\usepackage{threeparttable}             % tables with footnotes, capions all the same width
\usepackage{dcolumn}                    % decimal-aligned tabular math columns
\usepackage{multirow}                   % Allow table cells to span multiple rows
\usepackage{booktabs}                   % Formatting options for publication-quality tables
\usepackage{ltxtable}                   % long tabularx
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\usepackage{filecontents}
\begin{filecontents*}{testtable.tex}
    \rowcolors{1}{white}{lightgray}
  \begin{longtable}{p{3cm} X}
    \toprule
    Heading row & This should be lightgrey \\
    \hline % I use \hline because \midrule counts as a row and screws with the colors
    A data row & This row should be uncolored \\
    A data row & This row should be lightgrey \\
%    A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
%    \hline
    \rowcolor{lightgray}
        \rowcolors{5}{white}{lightgray}
    A new heading row & This should be lightgrey regardless of whether the previous row exists.\\
    A data row & This row should be uncolored \\
    A data row & This row should be lightgrey \\
    A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
\end{longtable}
\end{filecontents*}
\begin{document}
\rowcolors{1}{}{lightgray}
\LTXtable{\textwidth}{testtable}
\end{document}

enter image description here

Here from fourth row colors are alternate.

If previous row exists:

\documentclass[11pt]{article}
\usepackage{threeparttable}             % tables with footnotes, capions all the same width
\usepackage{dcolumn}                    % decimal-aligned tabular math columns
\usepackage{multirow}                   % Allow table cells to span multiple rows
\usepackage{booktabs}                   % Formatting options for publication-quality tables
\usepackage{ltxtable}                   % long tabularx
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\usepackage{filecontents}
\begin{filecontents*}{testtable.tex}
    \rowcolors{1}{white}{lightgray}
  \begin{longtable}{p{3cm} X}
    \toprule
    Heading row & This should be lightgrey \\
    \hline % I use \hline because \midrule counts as a row and screws with the colors
    A data row & This row should be uncolored \\
    A data row & This row should be lightgrey \\
    A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
    \hline
    %\rowcolor{lightgray}
    %\rowcolors{4}{white}{lightgray}     
    A new heading row & This should be lightgrey regardless of whether the previous row exists.\\
    A data row & This row should be uncolored \\
    A data row & This row should be lightgrey \\
    A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
\end{longtable}
\end{filecontents*}
\begin{document}
\rowcolors{1}{}{lightgray}
\LTXtable{\textwidth}{testtable}
\end{document}

enter image description here

share|improve this answer
    
This turns the heading row gray, but it doesn't set the row under it to white. It doesn't "restart" the alternating. –  Arkamis Apr 10 at 0:34
    
@Arkamis See the update. Is it OK? –  Harish Kumar Apr 10 at 0:44
    
Harish, I will test when I get into the office. It looks like either this solution or the other will work. We shall see! –  Arkamis Apr 10 at 14:17
    
@Arkamis You can rely on Heiko's macros and remedies always :). It was a good decision to use his trick. Have a nice time. :) –  Harish Kumar Apr 10 at 23:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.