# Using color before flushleft adds vertical space

A \color command before a \begin{flushleft} adds spurious vertical space when compiled using pdflatex (see MWE). The documentation (texdoc color) mentions issues with spacing. Similar issues might apply to the verbatim environment.

Are there reliable solutions or workarounds for this problem that can be safely used for a LaTeX code generator? Or should one rather avoid, at all costs, switching color "between environments" -- say, by using the fancyvrb package?

\documentclass{article}
\usepackage{color}
\pagestyle{empty}
\begin{document}

The spacing between this
\begin{flushleft}
and the following paragraph is good.
\end{flushleft}%
\color{red}%
\begin{flushleft} But here comes trouble. Why is the red block two parskips away?
\end{flushleft}
\color{blue}%
Changing from flushleft to regular doesn't add space even when color is changed.
\begin{flushleft}
\color{green}%
Neither does changing color inside flushleft.
\end{flushleft}

\end{document}


-
The behaviour is not specific to pdfLateX -- it's the same for XeLaTeX. –  lockstep Jun 3 '12 at 20:17
The problem is that the \color instruction doesn't allow the mechanism for merging the space after and before the flushleft environments to act correctly. –  egreg Jun 3 '12 at 20:23
@egreg: Thank you. Just out of curiosity: Perhaps the "mechanism for merging the space" can be altered to support \color? How does this work anyway? Which layer implements it? –  krlmlr Jun 3 '12 at 20:34
I'm afraid that with the current LaTeX it's not possible. The flushleft environment emits an \addvspace{\topskip} command that examines what comes before it; if it's a skip, then the result will be, under normal conditions, the maximum of the two spaces. In this case, what comes before is a color declaration, which prevents the comparison. –  egreg Jun 3 '12 at 20:39
Would it be possible to patch or enhance \addvspace so that it overlooks any \color commands? –  krlmlr Jun 3 '12 at 20:41

Yes if at all possible you should generate

\begin{flushleft}
\leavevmode\color{red} But here comes trouble. Why is the red block two parskips away?
\end{flushleft}


So the colour whatsit is found inside the first line rather than before the paragraph, sorry that's just how it is.

-
Unless of course you load l3galley and add whatsits using its mechanism :-) –  Joseph Wright Jun 3 '12 at 20:21
Thank you for your answer and the explanation in the comments. –  krlmlr Jun 3 '12 at 21:32