TransWikia.com

rowcolors not working with multirow

TeX - LaTeX Asked on July 30, 2021

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 Answers

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

Correct answer by Herthog on July 30, 2021

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

Answered by F. Pantigny on July 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP