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.

5
  • 2
    You might be interested in the following question, where the problem of coloring makecells was already discussed: How to color column heads determined width makecell Using a combination of \cellcolor and \colorbox should work for your case as well.
    – leandriis
    Jun 24, 2018 at 10:38
  • I tried placing a \colorbox inside my \makecell but that didn't look any different.
    – Max
    Jun 24, 2018 at 10:57
  • 5
    Placing the \colorbox command around the \makecell command will yield a completely colored cell: \cellcolor{red} \colorbox{red}{\makecell{Cell 2}}
    – leandriis
    Jun 24, 2018 at 11:01
  • It works! I thought I had tried that one earlier... Could you add your last comment as anwser so I can mark it as solution?
    – Max
    Jun 24, 2018 at 11:06
  • I'm sorry, but it isn't a duplicate of the mentioned link: this one does not have fixed width columns.
    – Bernard
    Jun 24, 2018 at 11:35

2 Answers 2

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .