# Why does \color{blue} after \paragraph{…} color the paragraph title blue?

MWE:

\documentclass{article}

\usepackage{color}

\begin{document}

\paragraph{Why am I blue?}

{\color{blue} Only I am supposed to be blue!}

\end{document}


Output (plus my scribble):

Question: Why does \color{blue} after \paragraph{...} color the paragraph title blue?

There's a workaround by replacing \paragraph{...} with \paragraph{\color{black} ...}, but this does not seem like desirable behaviour.

• Add \leavevmode before the color command, or use \textcolor{blue}{text} instead of \color. – Ulrike Fischer Sep 30 '17 at 12:34

## 2 Answers

\paragraph is set at the start of the next paragraph. The next paragraph starts with the first letter O of Only after \color{blue}.

Fixes:

• \leavevmode to explicitly start the paragraph before the color change:

\paragraph{Why am I blue?}
\leavevmode
{\color{blue} Only I am supposed to be blue!}

• \textcolor to limit the scope of the color setting:

\paragraph{Why am I blue?}
\textcolor{blue}{Only I am supposed to be blue!}


The result in both cases with updated text:

To complement Heiko’s (as usual) fine answer, some more words about the workings of \paragraph.

The command is defined in terms of \@startsection with a certain set of parameters, one of which is taken as the given title; one of the parameters tells LaTeX that the heading should be typeset inline, so the title is momentarily stored away.

It will be reinserted when the following paragraph starts. This allows a more free syntax: the input

\paragraph{Title} Some text


produces the same result as

\paragraph{Title}

Some text


and adding a \label after \paragraph{Title} doesn't insert unwanted spaces.

In both cases, \paragraph{Title} doesn't start a (text) paragraph (the unfortunate name clash might be distracting). The next paragraph start is triggered by S (it could also be \indent, \noindent, \leavevmode or \hspace, essentially what the TeXbook calls a horizontal command).

When TeX sees this S it backs it up, inserts parskip glue and the indentation box. After this it inserts whatever has been stored away, which is not only the title, but also

1. instructions to remove the indentation box;
2. the stored title, possibly preceded or followed by the paragraph number;
3. instructions for correct spacing the title from the following text and for avoiding that a page break occurs immediately following the line with the title.

In particular, the title is inserted as part of the current (text) paragraph. After this the S that triggered the action is reinserted back and TeX continues reading the text for later break it into lines.

In your case the horizontal command is O, which is already under the scope of \color{blue}, so also the title is typeset blue.

As a general rule, avoid \color before starting a (text) paragraph.

Instead of the arcane \leavevmode you can also use \indent, \noindent or \hspace{0pt}.

\documentclass{article}
\usepackage{xcolor}

\begin{document}

\paragraph{Test}

{\color{blue}XXXX}

\paragraph{Test}

\indent{\color{blue}XXXX}

\paragraph{Test}

\noindent{\color{blue}XXXX}

\paragraph{Test}

\hspace{0pt}{\color{blue}XXXX}

\paragraph{Test}

\leavevmode{\color{blue}XXXX}

\paragraph{Test}

\textcolor{blue}{XXXX}

\end{document}


• +1. For the sake of the completeness, remembering my own confusions when I was starting, just add that the horizontal mode start also with several commands that could have invisible results as \mbox{}, \makebox{} , \rule{0pt}{0pt} or ${}$, and using xelatex/lualatex even a invisible character (also in the source code) as the zero width character, u+200b (one can go crazy when is not aware of this). But many others as \hphantom{} or \hbox{} have not this result (do not be fooled by the "h" of these commands). – Fran Sep 30 '17 at 15:28
• @Fran Yes, also \mbox{} would do, because it starts doing \leavevmode; on the other hand ${}$ is not necessarily invisible, because of \mathsurround. I generally avoid abusing math mode unless necessary. – egreg Sep 30 '17 at 15:34
• Same here. In fact, when prefer the arcane and longer \leavevmode over any short trick because nonetheless is self-explanatory (once you know what mean) while, for instance, I find harder a forensic analysis of what the hell I did yesterday if there a empty box somewhere in a macro. – Fran Sep 30 '17 at 16:44