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?
1 Answers
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,121,712
#and~. Both generate errors for me.#can't be used in macro definitions done internally by\labeland~can expand to\nobreakspace {}which will break when written to the.auxfile. 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~and#. – egreg May 15 '11 at 22:28babelpackage 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-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--is transformed into an en dash at a deeper level, at printing time, not when tokens are formed. – egreg Sep 03 '12 at 09:2932-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 theunderscorepackage is loaded. Accented characters are definitely out of the question, unless you're using XeLaTeX or LuaLaTeX. – egreg Sep 24 '12 at 18:05