10

This issue occurs while using xcolor to alternate the table color, while also using makecell to put multiple lines in a table cell. In this specific scenario, where a larger cell is in a colored line, the coloring will stop just before the end of the table.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{makecell}

\rowcolors{2}{gray!20}{white}

\begin{document}
    \begin{tabular}{ |c|c| } 
    \rowcolor{gray!50}
    \hline
    \textbf{One} & \textbf{Two}\\
    \hline
    Text & More Text\\
    Even More & \makecell{Multiple \\ Lines}\\
    \hline
    \end{tabular}
\end{document}

enter image description here

4 Answers4

6

You could use \Gape.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{makecell}

\rowcolors{2}{gray!20}{white}

\begin{document}
    \begin{tabular}{ |c|c| } 
    \rowcolor{gray!50}
    \hline
    \textbf{One} & \textbf{Two}\\
    \hline
    Text & More Text\\
    Even More & \Gape[0pt][2pt]{\makecell{Multiple \\ Lines}}\\
    \hline
    \end{tabular}
\end{document}

enter image description here

4

I would not use makecell to do such things.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array}

\rowcolors{2}{gray!20}{white}
\newcolumntype{C}{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{2cm}}
% https://tex.stackexchange.com/a/12712/156344
\begin{document}
\begin{tabular}{|C|C|} 
    \rowcolor{gray!50}
    \hline
    \textbf{One} & \textbf{Two}\\
    \hline
    Text & More Text\\
    Even More & Multiple Lines\\
    \hline
\end{tabular}
\end{document}

enter image description here

3

For completeness. The (relative new) package tabularray has own macro for break text in cells into multiline text:

{first line\\ second line}

Using it at any column width, the row coloring works fine. Also the code is shorter. So, the OP table (extended with two more rows) can be written as:

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

\begin{document} \begin{center} \begin{tblr}{colspec = {Q[c,m] Q[c,m]}, row{1} = {font=\bfseries, bg=gray!50}, row{odd[2]} = {bg=gray!25} } \toprule \SetRow{} One & Two \ \midrule Text & More Text \ Even More & {Multiple\ Lines} \ Text & More Text \ Even More & {Multiple\ Lines} \ \bottomrule \end{tblr} \end{center} \end{document}

enter image description here

Zarko
  • 296,517
2

The environment {NiceTabular} of nicematrix has a built-in command \rowcolors (which must be used in the so-called \CodeBefore).

With that command, you have directly the expected output.

\documentclass{article}
\usepackage{xcolor}
\usepackage{makecell}
\usepackage{nicematrix}

\begin{document} \begin{NiceTabular}{ |c|c| } \CodeBefore \rowcolor{gray!50}{1} \rowcolors{2}{gray!20}{} \Body \hline \textbf{One} & \textbf{Two}\ \hline Text & More Text\ Even More & \makecell{Multiple \ Lines}\ \hline \end{NiceTabular} \end{document}

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

Output of the above code

However, nicematrix has also a built-in command \Block which may be used here instead of makecell.

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

\begin{document} \begin{NiceTabular}{ |c|c| } \CodeBefore \rowcolor{gray!50}{1} \rowcolors{2}{gray!20}{} \Body \hline \textbf{One} & \textbf{Two}\ \hline Text & More Text\ Even More & \Block{}{Multiple \ Lines}\ \hline \end{NiceTabular} \end{document}

The output is the same.

F. Pantigny
  • 40,250