5

I'm wondering why the \color command changes the vertical alignement in fixed-width tables.

\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}

\begin{tabular}{l | l} \hline \color{red} why is this & aligned with this? \ \hline \end{tabular}

\vspace{1cm}

\begin{tabular}{p{0.3\textwidth} | p{0.3\textwidth}} \hline \color{red} but this & not aligned with this? \ \hline \end{tabular} \end{document}

the color command changes vertical alignement in table

EDIT: and of course the follow-up: is there an easy way to fix this?

Kalissar
  • 339
  • 1
    Color changes cell height in tabular seems to be closely related (duplicate?). The following answer may also be interesting: https://tex.stackexchange.com/a/171903/134144 – leandriis May 29 '21 at 21:50
  • 2
    every other week for last 25+ years I make the same comment: see footnote on page 6 of texdoc grfguide which says basically: it's not my fault – David Carlisle May 29 '21 at 21:50
  • 3
    your second image does show the same alignment as the first. In both cases the top item of the cell box is aligned on the baseline of the row. In the first case the top item in the first column is a line of text, in the second one it is a whatsit node for the colour. If you warp your expectations to match those of TeX, you will see that this is the expected behaviour. – David Carlisle May 29 '21 at 21:53
  • 1
    Thank you @DavidCarlisle for your insights. Noone is pointing fingers, we're just trying to understand what's happening. – Kalissar May 29 '21 at 22:10

1 Answers1

4

My suggestion it is to use a correct syntax \textcolor{red}{but this} instead of \color{red} but this.

\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}

\begin{tabular}{l | l} \hline why is this & aligned with this? \ \hline \end{tabular}

\vspace{1cm}

\begin{tabular}{p{0.3\textwidth} | p{0.3\textwidth}} \hline \textcolor{red}{but this} & not aligned with this? \ \hline \end{tabular} \end{document}

enter image description here

Sebastiano
  • 54,118