2

Currently I am trying to create a table in which one column contains colored text. However, the color differs per cell. See the example code below, where the last column contains the (possibly) colored text.

    % Works as it should
    \begin{table}\centering\begin{tabular}{ccc}
    \toprule
       1 & 2 & {\color{red}3} \\
    \bottomrule
    \end{tabular}\end{table}

    % Does not work well...
    \begin{table}\centering\begin{tabular}{@{} p{2cm} p{2cm} p{2cm} p{2cm} p{2cm} @{}}
    \toprule
       Model & Parameter & BIC & $UD\text{max}F_T$ & Same?\\
    \midrule
       M1 & $\kappa_t^{(2)}$ & $\{43\}$ & $\{22, 50\}$ & {\color{red}No} \\
    \bottomrule
    \end{tabular}\end{table}

The problem with the second example is, in my case at least, that the text in the last column drops down, or seems to be put at a lower line. In the first example, the colored text is at the same line, though, as I would like it to be. Does anyone have an idea what is going wrong?

Packages that I use are:

    tabularx, amsmath, amsfonts, amsthm, color

Your help is greatly appreciated!

3

1 Answer 1

8

Fix it by changing {\color{red}No} to \textcolor{red}{No}.

The difference is that \textcolor inserts a \leavevmode before setting the color.

This is significant because in your second table you have made all the cells into vboxes with the p{} commands in the table preamble.

The extra space comes because TeX is in vertical mode when it processes the \color and there must be something in the expansion that causes TeX to add space to the vertical list. (But I can't see it).

You could also fix it by defining this particular column with a c instead of the p{}.

2
  • p is \parbox[t] so it aligns on the first node but in this case the first node is not the first line of text but the \special (or pdftex whatsit node) that inserts the colour so TeX aligns on that. If you start the paragraph before inserting the colour the whatsiit node is inside rather than before the first row which is a safer place for it to be. Commented Oct 10, 2012 at 20:21
  • Right. And "start the paragraph" is of course exactly what \leavevmode does.
    – Thruston
    Commented Oct 12, 2012 at 14:10

You must log in to answer this question.

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