Graphics, Figures & Tables\hhline resets \rowcolors

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Linguist
Posts: 43
Joined: Mon Nov 07, 2011 12:07 pm

\hhline resets \rowcolors

Post by Linguist »

Hello,

I've encountered a weird problem whereby use of \hhline causes \rowcolors to mess up. It is like the \hhline is itself treated as a row (which is then 'coloured')

So, if I have \rowcolors{1}{white}{gray} before a tabular, every second row should be grey. And this is indeed what happens without any \hhlines (first table in MWE). However, if I have an \hhline after the second row the following row is unexpectedly grey (2nd table in MWE) and if I have an \hhline after the third row the following row is unexpectedly white.

I've tested barious permutations of this with different combinations of \hhline and numbers of rows, and the best understanding of the problem I can come up with is that \hhline is somehow treated like its own row which is then "coloured". (But you see no effect because \hhline isn't actually a row, I think...) So if you use \hhline for every row, none will be coloured.

Not sure if there's anything obvious I'm missing.

Code: Select all

\documentclass[a4paper,12pt]{article}

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

\begin{document}

\begin{table}\rowcolors{1}{white}{gray} %coloured as I expect and want
	\begin{tabular}{llll}
		1&2&3&4\\ %white
		1&2&3&4\\ %grey
		1&2&3&4\\ %white
		1&2&3&4\\ %grey
	\end{tabular}
\end{table}

\begin{table}\rowcolors{1}{white}{gray} %third row unepextedly grey, final row white
	\begin{tabular}{llll}
		1&2&3&4\\ %white
		1&2&3&4\\ \hhline{----} %grey
		1&2&3&4\\ %grey
		1&2&3&4\\ %white
	\end{tabular}
\end{table}

\begin{table}\rowcolors{1}{white}{gray} %final row not grey as expected
	\begin{tabular}{llll}
		1&2&3&4\\ %gwhite
		1&2&3&4\\ %grey
		1&2&3&4\\ \hhline{----} %white
		1&2&3&4\\ %white
	\end{tabular}
\end{table}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

\hhline resets \rowcolors

Post by Stefan Kottwitz »

Very interesting! Following those thoughts, doubling the \hhline may look ok again:

Code: Select all

\documentclass[a4paper,12pt]{article}
\usepackage{hhline}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}\rowcolors{1}{white}{gray}
  \begin{tabular}{llll}
    1&2&3&4\\ %white
    1&2&3&4\\ \hhline{----}\hhline{----} %grey
    1&2&3&4\\ %white
    1&2&3&4\\ %grey
  \end{tabular}
\end{table}
\end{document}
Stefan
LaTeX.org admin
Linguist
Posts: 43
Joined: Mon Nov 07, 2011 12:07 pm

\hhline resets \rowcolors

Post by Linguist »

I’ve tried this, and indeed doubling \hhline does fix the row colouring issue, but it doubles the thickness of the line.

So we’re halfway there.

I should also note that exactly the same problem arises with \cline (though there are separate issues with \cline not rendering nicely with coloured table rows in many PDF readers). But this issue doesn’t arise with \hline.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

\hhline resets \rowcolors

Post by Stefan Kottwitz »

Then try adding \hhline{~} instead:

Code: Select all

\documentclass[a4paper,12pt]{article}
\usepackage{hhline}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}%\rowcolors{1}{white}{gray}
  \begin{tabular}{llll}
    1&2&3&4\\ %white
    1&2&3&4\\ \hhline{----}\hhline{~} %grey
    1&2&3&4\\ %white
    1&2&3&4\\ %grey
  \end{tabular}
\end{table}
\end{document}
Stefan
LaTeX.org admin
Linguist
Posts: 43
Joined: Mon Nov 07, 2011 12:07 pm

\hhline resets \rowcolors

Post by Linguist »

Perfect!

And as far as I can see, this doesn’t introduce any unwanted space/gaps either.
Post Reply