2

Somehow a control space \ used in \label doesn't expand. \foo gives the following newlabel in the aux file: Anonymous\ 1. Mind the backslash. The log gives: "! Missing \endcsname inserted." \baz does give the desired result in the aux file: Anonymous 2.

I guess this is related to the question Dynamically generated label in which mr Oberdiek replies: "Also \_ is problematic. Therefore the interface has to be changed." (Does this mean it cannot be done?)

Why doesn't the control space expand in \label? I think I can also phrase this question differently: why doesn't a control space expand when used (in combination with some other characters) between \csname \endcsname? I can't figure it out.

Is there any way to get \foo i.e. \name\ working as well? Thanks in advance!

\documentclass{article}
\newcommand{\name}{Anonymous}
\newcommand{\foo}{\label{person:\name\ 1}\name}
\newcommand{\baz}{\label{person:\name{} 2}\name}
\begin{document}
    \foo
    \baz
\end{document}
  • Why not stick to your definition of \baz, rather than a control space in \foo? – Werner Sep 5 '18 at 21:44
  • 1
    The \<space> primitive is not expandable. So it doesn't expand in a label and it's illegal inside the \csname construct that associates a ref to a label. What's the purpose of having \<space> in a label anyway? – egreg Sep 5 '18 at 21:56
  • The code is a minimal example. I'd like to generelize the input of names and using the input as a label. By using shorthands for certain names, I try to make it a bit easier. But if it cannot be done this way, I'll just find some other one. :) – Abe Sep 6 '18 at 5:58
4

Let's tackle the problem more generally.

If you say

\section{Title}\label{foo}

and the section happens to be number 2.4, falling on page 35, LaTeX will write

\newlabel{foo}{{2.4}{35}}

When the .aux file is read in at the next run, the \newlabel command will essentially perform

\expandafter\gdef\csname r@foo\endcsname{{2.4}{35}}

so that \ref{foo} and \pageref will be able to extract the relevant information.

The definition of \label is

% latex.ltx, line 4195:
\def\label#1{\@bsphack
  \protected@write\@auxout{}%
         {\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
  \@esphack}

which has the consequence that the argument to \label is subject to full expansion.

Now let's consider \label{foo\ 1}. What gets written would be

\newlabel{foo\ 1}{{2.4}{35}}

because the primitive \<space> is not expandable, so a write operation leaves it unchanged.

Now when the .aux file is read in, LaTeX tries to do

\expandafter\gdef\csname r@foo\ 1\endcsname{{2.4}{35}}

and here the error pops up: the token \<space> is illegal inside \csname. Only character tokens and macros expanding to character tokens are legal.

Changing the code for the \label-\ref mechanism in order to allow \<space> is out of the question. Besides, what's the purpose of having \<space> in a \label?

2

Try this:

\documentclass{article}
\newcommand{\name}{Anonymous}
\newcommand{\foo}{\label{person:\name\space1}\name}% or \name\space 1, same result
\begin{document}
\section{Test}

\foo

See also \ref{person:Anonymous 1} which is same as \ref{person:\name\space1}.

\end{document}

i.e. you want a space in your label and as far as I understand your problem is that it disappeared when using \name<space>. The \name{} approach works too, but then {} must be used when using the label in a \ref or similar macros.

Output:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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