42

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?

30

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.

  • 1
    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
  • 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 3:42
  • 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 3 '12 at 9:29
  • 1
    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
  • 1
    @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

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.