2

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]{short\\short}
%   \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.

ZhiyuanLck
  • 4,516
  • 1
    there is no reason at all to use multrow here as you can not span any rows of the table as there are no other columns. – David Carlisle Apr 24 '20 at 09:12
  • @DavidCarlisle I removed other unnecessary columns and rows. Just suppose I must do it in this way. I know the best way is not to write in this way, but I wonder if there is a solution without specifying the width of the column no matter how complicated and unnecessary the solution is. Cause I'm writing a conversion script... – ZhiyuanLck Apr 24 '20 at 09:28
  • 2
    you can use a nested \begin{tabular}{@{}l@{}} in the entry, but getting multiple lines within a cell is completely unrelated to making that cell span rows or columns. – David Carlisle Apr 24 '20 at 09:41
  • It works, thanks you anyway – ZhiyuanLck Apr 24 '20 at 09:45
  • please post solutions as answers not by editing the question (even if it's using my comment) otherwise the question-answer format of the site is messed up for future readers. – David Carlisle Apr 24 '20 at 12:24

4 Answers4

2

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{short\short} \end{tabular} \end{document}

enter image description here

Addendum: Also is simple with use new tabularray package:

\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}  % with tabularray the colortbl is not needed

\begin{document} \begin{tblr}{c} longer text \ \SetRow{cyan!30} {short\short} \end{tblr} \end{document}

or

\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}

\begin{document} \begin{tblr}{row{2}={cyan!30,c}, c} longer text \ {short\short} \end{tblr} \end{document}

In the both cases the result is the same:

enter image description here

L.J.R.
  • 10,932
Zarko
  • 296,517
1

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

F. Pantigny
  • 40,250
0

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

Bernard
  • 271,350
  • stackengine have some trouble with ehhline package (or I use it in a wrong way), thank you anyway – ZhiyuanLck Apr 24 '20 at 10:07
  • Could you give an example code of your problem(s)? – Bernard Apr 24 '20 at 10:09
  • I did not record it in detail, so I forgot why I use stackengine, all I remember is that I just forgot to remove this package, then there is a strange problem and when I remove it, everything gets normal. But I will try to reproduce it when I finish my work. Since only loading the package will cause the problem, I believe I will produce it. Then I will ask another question. – ZhiyuanLck Apr 24 '20 at 10:15
  • I've just tried loading ehhline with this code, and I had no error nor warning, whatever the loading order. – Bernard Apr 24 '20 at 10:31
  • Not this code, I draw all the horizon line of the table by leaders and boxes with \hhline{!{}}, I will ask another question soon(in 24h) – ZhiyuanLck Apr 24 '20 at 10:34
  • Sorry for late, go to https://tex.stackexchange.com/q/540700/201158 – ZhiyuanLck Apr 26 '20 at 03:27
0

Solution by David

use a nested tabular

\newcommand{\minitab}[2][l]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}
\cellcolor{teal}\multirow{-2}*{\minitab[r]{short\\short}
ZhiyuanLck
  • 4,516