10

I want to color some cells of a table, some of which use \makecell. The problem I run into is that not the whole width of the \makecell cells is filled by the color.

MWE:

\documentclass{article}

\usepackage{makecell}
\usepackage{colortbl}

\begin{document}
  \begin{tabular}{|l|}
  Cell 1's text is very long \\
  \cellcolor{red} \makecell{Cell\\2}
  \end{tabular}
\end{document}

output

The desired behavior is of course to have the whole cell filled, so how can I do that?

Edit: I'm using \makecell to get a cell consisting of multiple lines in my original problem, so I've updated the MWE to do that as well. If there's any other method of doing that without using makecell I'm willing to do so as well.

Max
  • 185

2 Answers2

8

It is known that makecell has problems with coloured tables. Here are two work-arounds:

  • one plays with \rowcolor and its overhang optional arguments, the other replaces makecell with tabStackengine. It is worth noting that, as the column has the l specifier, the content of the \makecell command can be centred globally in the cell, playing with the placement parameters, but the two rows can't be centred w.r.t. each other.
  • the other solution uses package tabstackengine, which doesn't have the same problems with colouring cells, and \multicolumn to obtain a centred content.

    \documentclass{article}
    \usepackage{makecell}
    \usepackage{tabstackengine}
    \usepackage[table]{xcolor}
    
    \begin{document}
    
    \begin{tabular}{|l|}
    Cell 1's text is very long \\
    \rowcolor{red}[\tabcolsep][47pt]\makecell[r]{Cell 2\\ Text} \\
    \multicolumn{1}{|>{\columncolor{yellow}}c|}{\tabularCenterstack{c}{Cell 3\\ Text}}
    \end{tabular}
    
    \end{document} 
    

enter image description here

Bernard
  • 271,350
1

The environment {NiceTabular} of nicematrix has a built-in command \Block which provides the functionnalities of \makecell (and others). Concerning colors, you have directly the expected result.

\documentclass{article}
\usepackage{nicematrix}

\begin{document} \begin{NiceTabular}{|l|} Cell 1's text is very long \ \Block[fill=red]{}{Cell\2} \end{NiceTabular} \end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

F. Pantigny
  • 40,250