20

The following code shows that a \color{red} command increases the height of a tabular cell. I want to use a switch, not a command as \textcolor{red}{text}.

\documentclass{article}

\usepackage{xcolor}
\usepackage{float}

\begin{document}

\begin{table}[H]
\begin{tabular}{*{2}{p{0.45\textwidth}}}
\hline
\color{red} header &
header \tabularnewline
\hline
 %
 content  & content \tabularnewline
 content  & content \tabularnewline
 content  & content \tabularnewline
\hline
\end{tabular}
\end{table} 
\end{document}

enter image description here

How can I work around this problem?

2 Answers 2

29

The cell height will be correct if you insert \leavevmode before \color for changing into horizontal mode. \textcolor does the same internally, it calls \leavevmode and then \color, additionally it does grouping. So use this as a switch:

\leavevmode\color{red} header &

If necessary in other places (maybe in moving arguments), consider protecting \leavevmode.

\protect\leavevmode\color{red}
2
  • 6
    The \color command doesn't switch to LR-mode, since it's useful also between paragraphs; however in a p column it defeats the boxing mechanism because when the paragraph starts, as soon as TeX scans the h in "header", the vertical list is not empty. Issuing \leavevmode cures the problem by starting a paragraph itself.
    – egreg
    Oct 14, 2011 at 10:22
  • @egreg: thanks for the additional explanation!
    – Stefan Kottwitz
    Oct 14, 2011 at 10:28
1

With {NiceTabular} of nicematrix, you have directly the expected output.

\documentclass{article}

\usepackage{xcolor}
\usepackage{float}
\usepackage{nicematrix}

\begin{document}

\begin{table}[H]
\begin{NiceTabular}{*{2}{p{0.45\textwidth}}}
\hline
\color{red} header &
header \tabularnewline
\hline
 %
 content  & content \tabularnewline
 content  & content \tabularnewline
 content  & content \tabularnewline
\hline
\end{NiceTabular}
\end{table} 
\end{document}

Output of the above code

You must log in to answer this question.

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