3

I can not simply align the text by using p{xx\linewidth} and \color in the same tabular.

The result is very awful... a conflicting package problem ?

anyone who has an idea is welcome ;)

sample:

why B below A and C ?

source:

\documentclass[a4paper,12pt]{book}

\usepackage[utf8]{inputenc}   
\usepackage[T1]{fontenc}      
\usepackage{color}            


\begin{document}

\begin{table}
\begin{tabular}{p{0.2\linewidth}p{0.2\linewidth}p{0.2\linewidth}}

A &   \color{red}{B}   &  C \\

\end{tabular}
\end{table}

\end{document} 

1 Answers1

3

When you invoke a p column, you are in a \parbox (or is it minipage?) that enters that column in vertical mode. Some commands act differently in vertical (page) mode than in horizontal (paragraph) mode. \color is one of them. The way to remedy the issue is to either use \textcolor which will leave vertical mode itself, or to explicitly leave vertical mode with a \leavevmode. I show both methods below.

\documentclass[a4paper,12pt]{book}

\usepackage[utf8]{inputenc}   
\usepackage[T1]{fontenc}      
\usepackage{color}            


\begin{document}

\begin{table}
\begin{tabular}{p{0.2\linewidth}p{0.2\linewidth}p{0.2\linewidth}}

A &   \leavevmode\color{red}B   &  C \\

A &   \textcolor{red}{B}   &  C \\

\end{tabular}
\end{table}

\end{document} 

enter image description here

Other macros that will suffer the same fate, and thus require a \leavevmode to behave in the "expected" way, include \llap, \rlap, and (outside of tabular environments) \marginpar.

  • 1
    @Jc Crivello I had the same thought, and found this related question that may be helpful. – sk8forether May 23 '17 at 14:42
  • 1
    @sk8forether Vertical alignment of the \parbox is certainly an issue too, but it is unique from the vertical/horizontal mode issue in which you found yourself with this question. – Steven B. Segletes May 23 '17 at 14:43
  • @JcCrivello Note that \color{red}{B} of your MWE, while not wrong, gives an improper impression. \color{} is a declaration, taking no additional arguments such as {B}. All text that follows is affected by the color change until the end of the current group. Thus, the syntax should be \color{} B. On the other hand \textcolor{}{} is the macro that not only defines the color, but also the text to which to apply it. – Steven B. Segletes May 23 '17 at 14:56