Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)
)) 
share|improve this question

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
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 at 22:16
    
@Dan Yes, of course, \textcolor is better. –  egreg Jan 29 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.