52

What are the valid characters to use in labels? I thought that only alphabetic characters were valid, but I tested ' and ! and they work. What are the restrictions to these names?

mjsr
  • 3,425

1 Answers1

44

ASCII characters in the 32-127 range that are not special for TeX are acceptable, including punctuation characters; don't use accented characters when the package inputenc is loaded. Sticking to letters, numbers, hyphen, colon, and perhaps & seems the best. Spaces are also allowed, but not recommended, mostly because editors might break lines at them.

Some of the special characters can be employed: &, _, ^, but not %, ~, #, and \. Even { and } might be used, as long as they are properly balanced. However this is not recommended practice. The underscore and & may be useful delimiters for giving structure to the labels; often the colon is used for this.

Warning. Some characters might give problems when babel is loaded along with varioref (for example the colon : with French and the double quote " with many languages). Without varioref these should be OK. As Martin points out, some packages might redefine _, making it unusable in labels.

egreg
  • 1,121,712
  • 2
    I disagree on # and ~. Both generate errors for me. # can't be used in macro definitions done internally by \label and ~ can expand to \nobreakspace {} which will break when written to the .aux file. Also some people like to redefine _ which would also break if it is defined fragile. I would avoid all characters special to TeX and also all non-ASCII characters. – Martin Scharrer May 15 '11 at 22:21
  • Excelent, that gives a lot of options to my automatic system generator. Thanks. – mjsr May 15 '11 at 22:22
  • @MartinScharrer do you think that using all the letters and punctuation characters is perfectly safe? – mjsr May 15 '11 at 22:24
  • @Martin: right, I presumed that the protection mechanism covered also ~ and #. – egreg May 15 '11 at 22:28
  • @voodoomsr: It depends on your used packages. Some languages supported by the babel package defines different punctuation marks to be active. Then they might cause trouble. In my quick test " still seems to work in \labels after [german]{babel} defined it active. You should stick to a sane selections of character anyway. I'm using only letters, numbers and :, -, _ and never had any issues or needed more characters. – Martin Scharrer May 15 '11 at 22:29
  • @MartinScharrer, @egreg thanks both for the explanations. – mjsr May 15 '11 at 22:33
  • 7
    The important point here, though, is that spaces are allowed. This can be very helpful in formulating non-cryptic label names. – Ryan Reich May 16 '11 at 03:42
  • @MartinScharrer So it is correct that using - at the end of a label or using -- within a label is okay too? Just checking, as I am not familiar with (La)TeX's parsing internals and thus don't know how LaTeX processes en-dashes internally. – Lover of Structure Sep 03 '12 at 07:19
  • 1
    @user14996 Yes, there's no problem in using them; -- is transformed into an en dash at a deeper level, at printing time, not when tokens are formed. – egreg Sep 03 '12 at 09:29
  • 2
    By saying "ASCII character that are not special for TeX are acceptable," you just beg the following question: which ascii characters are special for TeX? Can numbers be used in variable names? This is an important question, and deserves an exhaustive answer. Please give a full overview of valid variable names. – conradlee Sep 24 '12 at 17:29
  • 2
    @conradlee By ASCII I mean the printable ones below 128, that is in the range 32-127. Some of them are disallowed, as I said in my answer; others are not recommended. Sticking to letters, numbers, hyphen, and perhaps & seems the best. The underscore may be risky if the underscore package is loaded. Accented characters are definitely out of the question, unless you're using XeLaTeX or LuaLaTeX. – egreg Sep 24 '12 at 18:05