TransWikia.com

Multicolumn overrules rowcolors

TeX - LaTeX Asked by Martijn Kriek on February 5, 2021

I am trying to create a table with alternating rowcolors.
This works fine in itself with a command like:

rowcolors{1}{gray!20}{white}

However, from here it gets a bit more complex.
Since the last line of my table need 6 columns,
I need to use the multicolumn comment,
which causes all multicolumn parts to not inherit
the color as specified by the rowcolors command.

Code example:

begin{table}[!ht]
rowcolors{1}{gray!20}{white}
begin{tabular}{|p{0.184linewidth}|p{0.452linewidth}*{4}{|c}|}hline
bf{Item} & multicolumn{5}{l|}{bf{Description}} hline
bf{Goal} & multicolumn{5}{l|}{Goal of this} hline
bf{Procedure} & multicolumn{5}{l|}{How to do it} hline
bf{Specification} & multicolumn{5}{l|}{Think of this and that while doing this}  hline
bf{Comments} & & bf{Pass} & bf{Fail} & bf{Pass} & bf{Fail} cline{3-6}
& & $circ$ & $circ$ & $circ$ & $circ$ hline
end{tabular}
end{table}

Specifying the multicolumn color like:

multicolumn{5}{>{columncolor{gray!20}{some text here}}l|}{}

would force the multicolumn color, however I require the
{gray!20} part to be inherited from the rowcolors command for that row.

2 Answers

One way to do this is to modify the multicolumn command to pick up the row colour on the fly. Row colours are kept in two macros:

  • @oddrowcolor - for odd rows
  • @evenrowcolor - for even rows

while the counter rownum keeps track of the current row number. Here's a minimal working example showing the result of the modification, activated by the newly-created mcinherit:

enter image description here

documentclass{article}
usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
letoldmcmulticolumn
makeatletter
newcommand{mcinherit}{% Activate multicolumn inheritance
  renewcommand{multicolumn}[3]{%
    oldmc{##1}{##2}{ifoddrownum @oddrowcolorelse@evenrowcolorfi ##3}%
  }}
makeatother
begin{document}
begin{table}[!ht]
  rowcolors{1}{gray!20}{white} mcinherit
  begin{tabular}{|>{bfseries}p{0.184linewidth}|p{0.452linewidth}*{4}{|c}|}hline
    Item          & multicolumn{5}{l|}{textbf{Description}} hline
    Goal          & multicolumn{5}{l|}{Goal of this} hline
    Procedure     & multicolumn{5}{l|}{How to do it} hline
    Specification & multicolumn{5}{l|}{cellcolor{green}Think of this and that while doing this}  hline
    Comments      & & bf{Pass} & bf{Fail} & bf{Pass} & bf{Fail} cline{3-6}
                  & & $circ$ & $circ$ & $circ$ & $circ$ hline
  end{tabular}
end{table}
end{document}

I took the liberty of modifying your column formatting as well. For example, with column 1 containing only boldface entries, this can be formatted with the column specification using >{bfseries} - this inserts bfseries before every entry (support provided by the array package).

Note that this redefinition of multicolumn still allows you to change the cell colour to something else, since the third argument is inserted after the colour change. So, a local change will override the inherited colour. With a little work, it would be possible to automate the inheritance based on whether or not you use row colours. However, the extent of your usage is not known at the moment, so the switch* mcinherit will suffice.

Finally, use textbf instead of bf. For more on this, see Will two-letter font style commands (bf , it , …) ever be resurrected in LaTeX?

* Note that the redefinition should be made local if you're intermixing some coloured and uncoloured tables. This is "automatic" if you're calling mcinherit from within a table environment. Again, with some work the redefinition could be made more accommodating.

Answered by Werner on February 5, 2021

You can create this table easily with {NiceTabular} of nicematrix.

  • In {NiceTabular}, you merge cells both vertically and horizontally with the command Block.

  • The key hvlines will draw all the rules, excepted in the blocks.

  • A built-in command rowcolors will color alternatively the rows (as does rowcolors of colortbl) and, when the key respect-blocks is used, the blocks will be respected.

  • The result will be perfect in all the PDF viewers, at all levels of zoom (the rules won't seem to vanish).

However, you need several compilations (because nicematrix uses PGF/Tikz nodes).

documentclass{article}
usepackage{nicematrix}
usepackage{xcolor}

begin{document}
begin{NiceTabular}{>{bfseries}p{0.184linewidth}p{0.452linewidth}*{4}{c}}%
    [ 
      hvlines ,
      code-before = rowcolors{1}{gray!20}{}[respect-blocks]
    ]
  Item          & Block[l]{1-5}{textbf{Description}} 
  Goal          & Block[l]{1-5}{Goal of this}
  Procedure     & Block[l]{1-5}{How to do it}
  Specification & Block[l]{1-5}{Think of this and that while doing this} 
  Block{2-1}{}%
  Comments      & Block{2-1}{} & textbf{Pass} & textbf{Fail} & textbf{Pass} & textbf{Fail}
                & & $circ$ & $circ$ & $circ$ & $circ$ 
end{NiceTabular}

end{document}

Output of the above code

Answered by F. Pantigny on February 5, 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