# Xcolor breaks tabular cell alignment when using the p option

It appears that when using xcolor along with tabular, coloring the text of a cell inside a paragraph columns breaks its alignment:

\documentclass{article}
\usepackage{array}
\usepackage{xcolor}

\definecolor{darktext}{HTML}{414141}

\begin{document}

\begin{tabular}{c p{5cm}}
test1 & {\color{darktext} test2} \\
test3 & test4 \\
\end{tabular}

\end{document}


Is this a bug or intended? Is there some alternative to doing this?

• {\leavevmode\color{darktext} test2}. A number of LaTeX commands are not intended for use in vertical mode. Jan 5 '17 at 19:14
• It is a documented feature, use \textcolor{darktext}{test2} Jan 5 '17 at 19:14

See the footnote on page 6 of grfguide (texdoc color).
A whatsit node such as a \color special if used in vertical mode will affect the alignment of the surrounding box.
You can avoid this by using \leavevmode\color{darktext} test2 or perhaps more naturally \textcolor{darktext}{test2} (\textcolor adds the \leavevmode internally for exactly this issue). In either case these versions cause the colour to start in the first line of the paragraph not before the paragraph, which means that the first item in the box for the table cell is the first line of the paragraph, so the alignment point for the box is not affected, and is still the baseline of that line of text.