4

Note: I know there were other people asking the same question here and here, but none of there examples were minimal, and I'm still confuzzled. Sorry for the inconvience.


I know I can do this:

%rowcolor-multirow-test.tex

\documentclass{report}
\usepackage[table]{xcolor}
\usepackage{multirow}

\definecolor{slg}{gray}{0.95}
% ^^ "super-light gray" -- the builtin 'lightgray' isn't light enough.
\begin{document}

Wrong: \\
\rowcolors{1}{white}{slg}
\begin{tabular}{c|c|c|c}
  A & B & C & D \\
  \multirow{2}{*}{Hello!} & foo & bar & 42 \\
  & fooo & baar & 13 \\
\end{tabular}

\bigskip

Right:\\
\begin{tabular}{c|c|c|c}
  A & B & C & D \\
  & foo & bar & 42 \\
  \multirow{-2}{*}{Hello!} & fooo & baar & 13 \\
  % ^^ Put it on the odd rows and give it a negative number to span up.
\end{tabular}

\end{document}

To get this:
Obligatory xkcd: http://xkcd.com/1341


But how can I get this, preferably with the vertical lines intact?
What did you expect, something funny?
(I cheated and colored the cells manually with \cellcolor[gray]{0.95})

pdfLaTeX 3.14159265-2.6-1.40.17 (MiKTeX 2.9)

2

A possible solution is by using the \cellcolor command inside the cell that you want to have a different color. Note that this is not the cell containing the \multirow command but the one above it that you need to place the command in.

\documentclass{report}
\usepackage[table]{xcolor}
\usepackage{multirow}

\definecolor{slg}{gray}{0.95}
\begin{document}

\rowcolors{1}{white}{slg}

Fixed:\\
\begin{tabular}{c|c|c|c}
    A & B & C & D \\
    \cellcolor{white} & foo & bar & 42 \\
    \multirow{-2}{*}{Hello!} & fooo & baar & 13 \\
\end{tabular}
\end{document}

enter image description here

5
  • What you wrote doesn't seem to match the image you put; I get all rows white. Changing white to lightgray Doesn't work either. – SIGSTACKFAULT Nov 23 '16 at 14:48
  • The code is meant to work further on your example, i.e. it implies that you use the \rowcolors{1}{white}{slg} command. I have updated my answer for clarity – Herthog Nov 23 '16 at 14:50
  • Ah. Thanks, works perfectly. Could this be made to be a run-once thing? – SIGSTACKFAULT Nov 23 '16 at 14:59
  • What do you mean be a 'run-once thing'? – Herthog Nov 23 '16 at 15:00
  • i.e., Do \xyzzy at the start of the table, does everything else automagically. Or would that be too shenanigans-y? – SIGSTACKFAULT Nov 23 '16 at 15:04
1

The latest version of nicematrix (v 5.8 of 2021-01-01) provides an easy way to do that.

\documentclass{report}
\usepackage{nicematrix}
\usepackage{xcolor}

\begin{document}

\begin{NiceTabular}{c|c|c|c}
\CodeBefore 
  \rowcolors[gray]{1}{}{0.95}[cols=2-*]
\Body
  A & B & C & D \\
  \Block{2-1}{Hello!} & foo & bar & 42 \\
  & fooo & baar & 13 \\
\end{NiceTabular}

\end{document}

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

Ouput of the above code

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.