# \color after inline section colors the section title

In the following document, the subsection title gets colored red. Why does this happen?

\documentclass{article}
\usepackage{color}
\newcommand{\comment}[1]{{\color{red}#1}}
\begin{document}
\paragraph{This should not be red!}
\comment{Only this!}
\end{document}


How can I fix it? If I replace {\color{red}#1} with \textcolor{red}{#1} I get a "runaway argument" error.

-
Odd behaviour, good question. FWIW, when I write \newcommand{\comment}[1]{\textcolor{red}{#1}} I don't get an error and the colouring is correct. –  Will Robertson Sep 26 '10 at 19:45

The reason this happens is because the color changing command does not cause TeX to enter horizontal mode. Thus, the color changes to red, then when the first character of the argument to \comment is processed, TeX switches from vertical mode to horizontal mode to start a new paragraph. \everypar inserts the \paragraph title which, as Philipp notes, is missing \normalcolor so it is typeset in the color you chose.

To remedy this, you need to get TeX to start a new paragraph before you change color.

\newcommand{\comment}[1]{\leavevmode{\color{red}#1}}


That said, as Will notes,

\newcommand{\comment}[1]{\textcolor{red}{#1}}


works too.

-

This is a bug in the standard classes: the style argument of \@startsection must include \normalcolor. Fix for the moment:

\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
{3.25ex \@plus1ex \@minus.2ex}%
{-1em}%
{\normalfont\normalsize\normalcolor\bfseries}}


Other classes such as KOMA-Script include the required \normalcolor. Should probably be reported at http://www.latex-project.org/bugs-upload.html.

-
The standard classes will not be modified and all requests to modify them are apparently ignored. At least, according to clsguide. –  TH. Sep 26 '10 at 23:06
The paragraph that you refer to refers to design decisions, not necessarily to bugs. It's up to the LaTeX team to decide whether this behavior is a bug or a design decision… –  Philipp Sep 27 '10 at 8:35
It's a design decision, see latex-project.org/cgi-bin/ltxbugs2html?pr=latex/4128 –  Philipp Oct 3 '10 at 18:38