11

According to Guide to LaTeX, 4th edition, when creating a \label{name},

The keyword name may be any combination of letters, numbers, or symbols.

Is the name of a label case-sensitive? Can the name contain spaces?

I'm asking because when using Vim's snipmate TeX snippet, if I type sec and hit tab, I automatically get:

\section{section name}
\label{sec:section name}

Then when I enter the section name in the \section, Vim automatically puts the same text after the sec: portion of the \label. Since my sections are often multiple, capitalized words, the label name ends up containing spaces and are capitalized. For instance, I might get:

\section{Planned Testing}
\label{sec:Planned Testing}

Prior to allowing Snipmate in Vim automatically enter the label name based on the section name, I would have manually typed:

\section{Planned Testing}
\label{sec:planned_testing}

Spaces not allowed

Syntastic in Vim shows an error if a label name contains spaces.

  • As far as i know, they are case-sensitive. You shouldn't use whitespace, or underscores, or Umlauts (ä, ü) in your labels. TeX will convert them to something funny. Using XeTeX or LuaTeX you can use Umlauts in labels. – Johannes_B Dec 12 '13 at 18:11
  • I use \label{chap:chapter-name},\label{sec:section-name},\label{subsec:subsection-name},\label{fig:figure-name},\label{tab:table-name},\label{eq:equation-name},\label{lst:code-name} and \label{itm:important-note} as shown in http://en.wikibooks.org/wiki/LaTeX/Labels_and_Cross-referencing and http://tex.stackexchange.com/questions/12174/naming-latex-files-best-practice – texenthusiast Dec 12 '13 at 19:16
  • @Johannes_B Why do you recommend not using underscores? – Matthew Rankin Dec 12 '13 at 22:29
  • @texenthusiast Is there a reason you use hyphens instead of underscores? The two examples of multi-word labels in the first link you provided—LaTeX/Labels and Cross-referencing—used underscores as opposed to hyphens—\label{fig:my_figure} and \label{the_label}. – Matthew Rankin Dec 12 '13 at 22:34
  • 1
    @MatthewRankin you can use them as http://tex.stackexchange.com/questions/121416/putting-an-underscore-in-a-label/121438#121438, but sometimes one might get into troubles, see more http://tex.stackexchange.com/questions/18311/what-are-the-valid-names-as-labels/18312#18312. To eliminate all conflicts(to be safe all times), I gave my choice :). – texenthusiast Dec 12 '13 at 22:43
  • @MatthewRankin As texenthusiast linked: to be safe, don't use those things that might get you in trouble. – Johannes_B Dec 13 '13 at 07:26

1 Answers1

17

Is the name of a label case-sensitive? Can the name contain spaces?

Yes and yes.

References are stored as a macro using \csname r@<name>\endcsname, so anything allowed within \csname...\endcsname is valid (as reference, see Understanding how references and labels work and What exactly do \csname and \endcsname do?). This includes a distinction between case (since, by nature \macro is different from \Macro, \MaCRo, \MACRO, ...).

As a small example consider:

enter image description here

\documentclass{article}
\expandafter\def\csname some bizarre Name 123\endcsname{Stuff}
\begin{document}
\csname some bizarre Name 123\endcsname

\section{A section}\label{sec:A section}
See~\ref{sec:A section} and~\ref{sec:a section}.
\end{document}
Werner
  • 603,163
  • 11
    although spaces are permitted in the name of a label, it's generally recommended that you not use them. they have been known to cause problems in journal production, so if you are submitting an article for publication, it's best to stick to the simplest option that will get the job done reliably. – barbara beeton Dec 12 '13 at 18:57
  • @barbarabeeton IIRC, what a label may contain has been dramatically relaxed some years ago. Is your recommendation still valid? – Denis Bitouzé Jun 07 '23 at 18:16
  • 1
    @DenisBitouzé -- I personally wouldn't/don't use spaces in labels. Although I'm pretty sure the basic mechanisms accept them, I don't know the behavior of all packages, or of "external" scripts that may be applied. And debugging really unusual situations that exceed my familiarity isn't my favorite form of recreation. YMMV. – barbara beeton Jun 07 '23 at 18:57