When modifying my answer to this question I came across some odd behaviour.
If \color{<color>}
is used at the start of a top-aligned \parbox
, then the entire box is moved downwards, like this:
\documentclass{article}
\usepackage{xcolor}
\begin{document}
Word
\framebox{\parbox[t]{1cm}{word}} %% <- this is fine
\framebox{\parbox[t]{1cm}{\textcolor{blue}{word}}} %% <- this is fine
\framebox{\parbox[t]{1cm}{\color{blue}word}} %% <- ??
\framebox{\parbox[t]{1cm}{\hskip0pt\color{blue}word}} %% <- this is fine
\framebox{\parbox[t]{1cm}{\leavevmode\color{blue}word}} %% <- this is fine
\framebox{\parbox[t]{1cm}{$\displaystyle\int$}} %% <- this is fine
\framebox{\parbox[t]{1cm}{\color{blue}$\displaystyle\int$}} %% <- ??
\end{document}
(The \framebox
es are just there to show the locations/sizes of the \parbox
es.)
Can someone tell me why this happens (and how I can best avoid it)?
Best Answer
\color
is usually implemented via "whatsits", i.e.\special
or\pdfcolorstack
in case of pdfTeX. It becomes the top element at the start of the vertical box (\parbox[t]
). The following paragraph starts below. Putting it after\leavevmode
at the baseline of the starting paragraph is the right fix.For debugging, a box can be inspected by
\showbox
:The console (with
\tracingonline=1
) and the.log
file reports: