# Getting extra space with this combination of \color and \label

If I type {\bfseries\color{red}Theorem 1. }{\itshape\label{two} This is a test.}, I get too much space between "1." and "This". Here's a file that shows what I mean:

\documentclass{article}

\usepackage{color}

\begin{document}

{\bfseries Theorem 1. }{\itshape This is a test.}

{\bfseries Theorem 1. }{\itshape\label{one} This is a test.}

{\bfseries\color{red}Theorem 1. }{\itshape This is a test.}

{\bfseries\color{red}Theorem 1. }{\itshape\label{two} This is a test.} %too much space between "1." and "This"

{\bfseries\color{red}Theorem 1. }{\itshape\label{three}This is a test.}

\end{document}


In every case except for the one with the comment after it, the space between "1." and "This" is the same (and I assume correct).

Is there a way to automatically stop this from happening? Here's what my .log file says when the color package is loaded:

("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\graphics\color.sty"
Package: color 2005/11/14 v1.0j Standard LaTeX Color (DPC)

("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\00miktex\color.cfg"
File: color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive
)
Package color Info: Driver file: dvips.def on input line 130.

("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\graphics\dvips.def"
File: dvips.def 1999/02/16 v3.0i Driver-dependant file (DPC,SPQR)
)
("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\graphics\dvipsnam.def"
File: dvipsnam.def 1999/02/16 v3.0i Driver-dependant file (DPC,SPQR)
))

-

\documentclass{article}

\usepackage{color}

\begin{document}

Theorem 1. \label{one} This is a test.

{\color{red}Theorem 1. }\label{two} This is a test.

\textcolor{red}{Theorem 1. }\label{three} This is a test.

\end{document}


What happens? The command \label is smart: it looks for a preceding space and, if it finds one, it removes the space, ignores the possible following spaces and reinserts the space removed at the beginning (after having done its specific job of setting the label).

This is what happens in the first line. In the second and third lines this doesn't happen, because between the space after 1. and \label there is the invisible item that changes back the color from red to black.

It's unfortunate, but there's nothing to do about it, apart from typing \label always attached to a word or in places where spaces are ignored: between paragraphs, after \item, after a \begin{theorem} (or similar environment defined with \newtheorem) or in math mode.

-
Another thing you can do about it is to arrange for color changes to end as soon as they are no longer needed. Using \textcolor for short streches of colored text encourages this somewhat. –  Dan Jan 29 '14 at 22:16
@Dan Yes, of course, \textcolor is better. –  egreg Jan 29 '14 at 22:22

The \label command tries to check if a space came before it, and when it decides there was such a space, it arranges for any space after it to be ignored. This work out in the second example, but not the fourth one. What happens there is that the \color command places a special node at the end of the current group that sets the current color back to its previous value. That is, the line in question looks something like this to TeX:

[group_start][font_change][color_change]Theorem 1.[space][group_end][color_change]


The \label{two} command that follows this can't see the [space] through the [color_change] node (grouping doesn't produce this problem nor do font changes). Thus \label detects no previous space and so allows the following space to stay. You need to end the scope of the color command before the space that follows "Theorem 1.":

\textbf{\textcolor{red}{Theorem 1.} }{\itshape\label{two} This is a test.}


The \textcolor command is perfect for this. Or you can just omit spaces after any \label.

The reason color changes behave this way while font changes do not is because color was grafted onto LaTeX years after it was created and this was the only method generally available.

-