In LaTeX there are commands that process arguments but do not produce visible output.
For example, the command \label
, or, if you use the package color or xcolor, the command \color
.
When the closing curly brace of the argument is tokenized, TeX's reading apparatus switches to state M (middle of line), thus a space that in the .tex-source-code comes after the closing curly brace is tokenized as an explicit space token.
If in the source code you have a space before the corresponding command, which becomes a space-token, and you also have a space after the closing curly brace of the argument of the command, you would get two explicit space tokens, whereof in horizontal mode each results in horizontal space if no linebreak occurs.
Often one does not like this.
Therefore these macros have mechanisms like the \@bsphack
..\@esphack
-mechanism: When the macro is called, the value of the register \lastskip
and the value of the parameter \spacefactor
is stored. Lastly, the space factor is restored and if the stored value of \lastskip
indicates that a space has been set before calling the macro, subsequent space-tokens are ignored, using \ignorespaces
.
This can lead to unexpected output if you define commands that output visible text but use commands in their definition that do not.
Suppose I intend to write a command that reads an argument and sets it in blue color.
I could write this:
\documentclass{article}
\usepackage{xcolor}
\newcommand\blue[1]{{\color{blue}#1}}%
\newcommand\ingroup[1]{{#1}}%
\begin{document}
\Huge
\par\noindent(\ingroup{ Text })
\par\noindent(\blue{ Text })
\par\noindent(\textcolor{blue}{ Text })
\end{document}
But the output contains a subtlety:
With the command \ingroup
the leading space in the argument was not ignored.
With the command \green
the leading space in the argument was ignored. The same with \textcolor
.
What is the best way to ensure that
- leading explicit space tokens in arguments are not ignored and that
- hyphenation is turned on even in case there are no leading spaces?