TransWikia.com

rowcolor for a multirow

TeX - LaTeX Asked on March 27, 2021

The multirow text is covered by the background color. Does someone know why and can fix it?

enter image description here

begin{table}[h]
    caption{caption} 
    label{tab} 
    centering
    begin{tabular}{cccccc}
        toprule
        & & A & B & C & D 
        midrule
        rowcolor{gray!10}
            multirow{2}{*}{Rosso}  & Pruning & - & - & 6-C-Pr & - 
        rowcolor{gray!10}
                                & Stalks & - & - & 6-C-S & - 
            multirow{2}{*}{Verde}  & Pruning & - & - & 6-M-Pr & - 
                                & Stalks & - & - & 6-M-S & - 
        rowcolor{gray!10}
            multirow{2}{*}{Blu}    & Pruning & 4-Pa-Pr & 5-Pa-Pr & 6-Pa-Pr & 7-Pa-Pr 
        rowcolor{gray!10}
                                & Stalks & 4-Pa-S & 5-Pa-S & 6-Pa-S & 7-Pa-S 
            multirow{2}{*}{Bianco}         & Pruning & - & - & 6-W-Pr & - 
                                & Stalks & - & - & 6-W-Pr & - 
            bottomrule
        end{tabular}
end {table}

3 Answers

If the multirow command is placed on the upper of the two rows, then its text is placed and the next table line with rowcolor will overprint the lower part. Therefore, multirow is better moved to the lower of the two rows. Then the background color of the two rows is already set, when the text of multirow is placed:

documentclass{article}
usepackage{booktabs}
usepackage{caption}
usepackage{colortbl}
usepackage{xcolor}
usepackage{multirow}

begin{document}
begin{table}[h]
  caption{caption}
  label{tab}
  centering
  begin{tabular}{cccccc}
    toprule
    & & A & B & C & D 
    midrule
    rowcolor{gray!10}
    & Pruning & -- & -- & 6-C-Pr & -- 
    rowcolor{gray!10}
    multirow{-2}{*}{Rosso}
    & Stalks & -- & -- & 6-C-S & -- 
    & Pruning & -- & -- & 6-M-Pr & -- 
    multirow{-2}{*}{Verde}
    & Stalks & -- & -- & 6-M-S & -- 
    rowcolor{gray!10}
    & Pruning & 4-Pa-Pr & 5-Pa-Pr & 6-Pa-Pr & 7-Pa-Pr 
    rowcolor{gray!10}
    multirow{-2}{*}{Blu}
    & Stalks & 4-Pa-S & 5-Pa-S & 6-Pa-S & 7-Pa-S 
    & Pruning & -- & -- & 6-W-Pr & -- 
    multirow{-2}{*}{Bianco}
    & Stalks & -- & -- & 6-W-Pr & -- 
    bottomrule
  end{tabular}
end {table}
end{document}

Result

Correct answer by Heiko Oberdiek on March 27, 2021

I propose the following improvement to the table: as booktabs introduces vertical spacing around rules, which results in white strips in coloured rows, I replace the rule commands from booktabs with variable width rule commands from the small boldline package, from the shipunov bundle, and add vertical padding above and below row cells with cellspace package, which ensures a minimal vertical spacing at the top and the bottom of every cell of columns prefixed by the letter S.

documentclass{article}

usepackage{caption}
usepackage[table]{xcolor}
usepackage{multirow}
usepackage{boldline}
usepackage{cellspace}
setlengthcellspacetoplimit{4pt}
setlengthcellspacebottomlimit{4pt}

begin{document}

begin{table}[h]
  caption{caption}
  label{tab}
  centering
  begin{tabular}{*{6}{Sc}}
    hlineB{2}
    & & A & B & C & D 
   hlineB{1.25}
    rowcolor{gray!10}
    & Pruning & -- & -- & 6-C-Pr & -- 
    rowcolor{gray!10}
    multirow{-2}{*}{Rosso}
    & Stalks & -- & -- & 6-C-S & -- 
    & Pruning & -- & -- & 6-M-Pr & -- 
    multirow{-2}{*}{Verde}
    & Stalks & -- & -- & 6-M-S & -- 
    rowcolor{gray!10}
    & Pruning & 4-Pa-Pr & 5-Pa-Pr & 6-Pa-Pr & 7-Pa-Pr 
    rowcolor{gray!10}
    multirow{-2}{*}{Blu}
    & Stalks & 4-Pa-S & 5-Pa-S & 6-Pa-S & 7-Pa-S 
    & Pruning & -- & -- & 6-W-Pr & -- 
    multirow{-2}{*}{Bianco}
    & Stalks & -- & -- & 6-W-Pr & -- 
    hlineB{2}
  end{tabular}
end {table}

end{document} 

enter image description here

Answered by Bernard on March 27, 2021

You can construct that table with {NiceTabular} of nicematrix with the following avantages:

  • The colored rows will automatically respect the rules of booktabs.
  • You don't have to put multicolumn with a negative argument: you use Block with a positive argument.
  • You can color alternatively the logical rows with a command rowcolors in the CodeBefore.
  • You will have a good output in all the PDF viewers at all levels of zoom (better than with colortbl).

There is a drawback: you need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

documentclass{article}
usepackage{caption}
usepackage{nicematrix}
usepackage{booktabs}

begin{document}


begin{table}[h]
caption{caption}
label{tab}
centering
begin{NiceTabular}{*{6}{c}}[cell-space-limits=4pt]
CodeBefore
  rowcolors{1}{}{gray!10}[respect-blocks]
Body
  toprule
  & & A & B & C & D 
 midrule
  Block{2-1}{Rosso}
  & Pruning & -- & -- & 6-C-Pr & -- 
  & Stalks & -- & -- & 6-C-S & -- 
  Block{2-1}{Verde}
  & Pruning & -- & -- & 6-M-Pr & -- 
  & Stalks & -- & -- & 6-M-S & -- 
  Block{2-1}{Blu}
  & Pruning & 4-Pa-Pr & 5-Pa-Pr & 6-Pa-Pr & 7-Pa-Pr 
  & Stalks & 4-Pa-S & 5-Pa-S & 6-Pa-S & 7-Pa-S 
  Block{2-1}{Bianco}
  & Pruning & -- & -- & 6-W-Pr & -- 
  & Stalks & -- & -- & 6-W-Pr & -- 
  bottomrule
end{NiceTabular}
end {table}

end{document} 

Output of the above code

Answered by F. Pantigny on March 27, 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