TransWikia.com

Problem with colortbl and makecell

TeX - LaTeX Asked on April 28, 2021

Here is an simple example.

documentclass{article}
usepackage[table]{xcolor}
usepackage{colortbl}
usepackage{multirow, makecell}

begin{document}
begin{tabular}{c}
  longer text
  cellcolor{teal}
  cellcolor{teal}multirowcell{-2}[0ex][r]{shortshort}
%   cellcolor{teal}multirow{-2}*{short} % is ok but can't break line or set alignment
end{tabular}
end{document}

enter image description here

When the text in the multirowcell is shorter than than the text of the above cell, the cell will not be colored well. multirow is ok, but I want to break line inside the cell and set the alignment.

Cauze the code will be generated by python and the width of the column is impossible to be accessed in program nor be setted to a just suitable value, options such as p, w in array or X in tabularx is not considered.

4 Answers

Makecell may have problems with coloured cells in tables. As a workaround, I suggest to use Centerstack from stackengine in a plain multirow. Unrelated: loading xcolor with option [table], you don't have to load colortbl since the former does it for you.

documentclass{article}
usepackage[table]{xcolor}
usepackage{multirow, makecell}
usepackage[usestackEOL]{stackengine}

begin{document}

begin{tabular}{c}
  longer text
  cellcolor{teal!75}
  cellcolor{teal!75}multirowcell{-2}[0ex][r]{shorter  short}
% cellcolor{teal}multirow{-2}*{short} % is ok but can't break line or set alignment
end{tabular}
qquad
begin{tabular}{c}
  longer text
  cellcolor{teal!75}
  cellcolor{teal!75}multirow{-2}{*}{Centerstack[r]{shorter  short}}
end{tabular}

end{document} 

enter image description here

Answered by Bernard on April 28, 2021

With use the w{c}{...} column type is simple:

documentclass{article}
usepackage[table]{xcolor}  % it load colortbl
usepackage{makecell}

begin{document}
begin{tabular}{w{c}{5em}}
  longer text       
rowcolor{teal}
makecell{shortshort}
end{tabular}
end{document}

enter image description here

Answered by Zarko on April 28, 2021

Solution by David

use a nested tabular

newcommand{minitab}[2][l]{begin{tabular}{@{}#1@{}}#2end{tabular}}
cellcolor{teal}multirow{-2}*{minitab[r]{shortshort}

Answered by ZhiyuanLck on April 28, 2021

The package nicematrix (≥ 4.0) provides tools to color cells, rows and columns in a way compatible with multirow and makecell:

documentclass{article}
usepackage{xcolor}
usepackage{multirow, makecell}
usepackage{nicematrix}

begin{document}

begin{NiceTabular}{c}[code-before = rowcolor{teal!75}{2,3}]
 longer text
 
 multirowcell{-2}[0ex][r]{shorter  short}
end{NiceTabular}
end{document} 

You won't have the thin white lines you see in some PDF viewers at some levels of zoom (see for instance Bernard's answer).

Result of above code

Answered by F. Pantigny on April 28, 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