11

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.

Samuel
  • 2,484
  • 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

2 Answers2

4

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.

Philipp
  • 17,641
  • 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 08:35
  • 1
    It's a design decision, see http://www.latex-project.org/cgi-bin/ltxbugs2html?pr=latex/4128 – Philipp Oct 03 '10 at 18:38
4

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.

TH.
  • 62,639