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

Your example can be simplified:

\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}

enter image description here

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.


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.

Tags:

Spacing

Color