0

I've noticed that LyX 2.4 does not like whitespaces inside the label field: it turns them into =000020 when converting into tex.

It's easy to reproduce. Insert a new label with a name A B and look at LaTeX Preview: \label{A=000020B}.

What are the actual, official, contemporary latex rules or problems regarding whitespaces inside \label and, if permitted, how to use them inside LyX?

Previously, Are LaTeX's label names case-sensitive? Can label names contain spaces? and What to consider when naming labels to prevent errors? suggested that whitespaces are allowed. I wonder then why LyX 2.4 is stricter.

Update. The accepted Answer has completely addressed the problem of the whitespaces in the latex label names, particularly when used by refstyle as it turns out, and how it's handled in the current and future versions of LyX. Therefore, I don't mind this Question to be "Closed" now. Thank you!

6
  • Possible is different from convenient. For computers, spaces in code generally mean "the previous argument, command, option, or whatever is over," and making exceptions to this rule makes creating robust programming more challenging. You never know for sure what mechanism will be fooled by superfluous spaces in labels, file names, variables, etc. Lyx is just cautious avoiding this issue internally.
    – Fran
    Commented Sep 3 at 16:06
  • Why LyX works like it works is nothing the TeX community could change or solve. IMHO questions about development decisions are better asked to the developers → lyx.org/MailingLists. The questions about using spaces in \label in LaTeX are already answered in the questions you've linked. And please ask only one question per question.
    – cabohah
    Commented Sep 4 at 15:01
  • mail-archive.com/[email protected]/msg222793.html The closely related question of what exactly, officially is currently allowed inside latex label name stands.
    – rych
    Commented Sep 5 at 3:40
  • 1
    Since LyX is sort of a TeX editor, aren't question about it still on-topic here? Commented Sep 22 at 7:10
  • Closing this perfectly legit question for those reasons is weird. "This question needs to be more focused" -- there is no way to make it more focused. It just requires an effort to understand. It describes the problem if whitespaces inside latex label names as revealed in the interplay of refstyle and LyX. "It is not currently accepting answers" -- fine, but it's rather because a complete answer is given and accepted. In short, we can use whitespaces as long as other packages respect them. Here we've discovered an undocumented limitation of refstyle, found a workaround, and patched LyX.
    – rych
    Commented Sep 23 at 3:49

1 Answer 1

1

It turned out that LyX 2.4 escaped label names for compatibility with refstyle's formatted references:

\label{sec:A B}
 ...
\secref{A B} % can't find the label sec:AB -- refstyle has gobbled the whitespace!

1. LaTeX, refstyle restriction

I received an answer from the refstyle maintainer (Thank you, Daniel!) as follows (emphasis is mine):

"The main command in refstyle that cycles through a list of references (and a single reference) is:

\def\RS@ref#1[#2]#3{%
    \begingroup
        \RS@setkeys{RS@#1}{#2}%
        \@safe@activestrue%
        \edef\RS@tmpa{\zap@space#3 \@empty}%
        \@safe@activesfalse%
        \edef\RS@tmpa{\noexpand\RS@@ref{#1} \RS@tmpa,\relax\noexpand\@eolst}%
        \RS@tmpa%
    \endgroup}

It removes spaces and was intended for the following scenario, and that is how many people write a list of references with spaces in between

\secref(aaa, bbb, ccc) -> \ref{sec:aaa}, \ref{sec:bbb}, \ref{sec:ccc}

If the \zap@space part is removed then we have the wrong reference:

\secref(aaa, bbb, ccc) -> \ref{sec:aaa}, \ref{sec: bbb}, \ref{sec:
ccc}

The refstyle package cannot distinguish between a space in a list and a space in a reference. If I change that it will break a lot of old documents. The second point is that in standard latex, spaces in labels is a newish thing. It was not always there. Also note even if you put 10 spaces in a row in a label, latex only passes one through to the definition - see the .aux file. I think it is better to use an underscore _ or include your label in brackets {aaa}".

-- Danie Els

2. LyX, workaround

The extra brackets solution is committed into main by a LyX developer. Thank you, Jürgen!

\label{sec:A B}
 ...
\secref{A B} % can't find the label sec:AB -- refstyle has gobbled the whitespace!
 
\secref{{A B}} % works!

"Actually, considering all eventualities, this might be the best solution, as the problem only concerns refstyle's formatted ref commands. All other solutions that I could think of add unnecessary complication... Good, I committed to master". -- Jürgen

4
  • 1
    I wonder why the label in source bother you if you use LyX. The label in the LyX editor always will be shown as a gray box A B, the cross-reference menu show the label "A B" and the references in text will .Ref: A B or so. Yes, the label in latex code is changes and it is ugly, but functional and the main purpose of Lyx is not having to took thatx code (except seldomly in case of troubles or to learn LaTeX). So, who care?
    – Fran
    Commented Sep 22 at 10:38
  • I may use the label name itself for reference, e.g., by redefining \renewcommand*{\thmref}[1]{\hyperref[thm:#1]{#1}} underneath. I'm glad the next release of LyX will leave my labels as is. IMHO, LyX shouldn't even take such liberties with LaTeX it generates. See also mail-archive.com/[email protected]/msg222812.html
    – rych
    Commented Sep 22 at 11:37
  • The suggested workaround was implemented as a patch and is released with LyX 2.4.2
    – rych
    Commented Oct 5 at 12:51
  • 1
    +1 very nice answer. Thanks for writing up those details!
    – scottkosty
    Commented Oct 22 at 9:53

Not the answer you're looking for? Browse other questions tagged .