4

What characters are safe in a BibTeX label aka citation key? With safe I mean a wide range of applications, including BibTeX and biber, a wide range of BibTeX or BibLaTeX style files, but also utilities like bibliographic databases and common tools for processing .bib files.

A partial answer based on experimentation is given in the answer to this question, but I encountered some problems with characters not mentioned there (e.g. the underscore, and even the forward slash for some tools like bibmerge) in the past, so I am looking for a more definite answer.

An answer should err "on the safe side", I'd rather exclude a character that is unproblematic in most cases and only throws errors under certain circumstances than to include it.

5

1 Answer 1

6

Various characters are forbidden for various reasons, but it is tricky to come up with a simple list that works for all possible tools (not all .bib-file related tools use the same library to parse .bib files, most notably Biber has much stricter requirements than BibTeX, but there are many other tools out there with who knows which restrictions).

BibTeX

On the BibTeX side the only characters that seem to be forbidden in an entry key are the space , } and , due to the syntax of a .bib entry

@<type>{<entry key>,
  <field> = {<contents>},
}

Generally, non-US-ASCII characters are not guaranteed to be processed properly, but since they are generally passed through as is, things mostly work on the BibTeX side.

Biber

Biber is a bit more fussy about entry keys and disallows

" # ' ( ) , = { } %

See Using BibTeX keys containing parentheses with Biber.

LaTeX

Many characters are disallowed on the LaTeX side either when the processed .bib data is passed back to LaTeX or when LaTeX needs to tell BibTeX which entries to fetch. Note that the exact characters that are forbidden may very well depend on the exact citation/bibliography packages you load.

For example BibTeX accepts { in an entry key, but this will very likely error on the LaTeX side since the { would open a group that would not be closed properly.

Similarly, # has a special meaning for LaTeX and can in general not appear outside of macro definitions.

% starts a comment and is therefore problematic.

Since \ starts command sequences it is also dangerous

This gives us

{ } # % \

Another possible source of issues are active characters like ~, since they might expand to something that cannot be written out cleanly into the .aux file.

The way pdfLaTeX works means that non-US-ASCII characters are also implemented via active characters and so with pdfLaTeX it is best to stay clear of non-US-ASCII characters as well.

All of this suggests that on the LaTeX side A-Za-z0-9 as well as safe punctuation like .:;-_!?"'+=*/ might be acceptable. Some punctuation is made active by some babel language modules. Often this happens in a way that still allows these punctuation characters to be used in entry keys.

But there is no guarantee. babel-german makes the " active, so it can be used as a shorthand (e.g. "a for ä). Then the behaviour of " depends on the following character.

\documentclass[ngerman]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}

\usepackage{natbib}

\begin{filecontents}{\jobname.bib}
@book{".,
  author    = {Anne Elk},
  title     = {A Theory on Brontosauruses},
  year      = {1972},
  publisher = {Monthy \& Co.},
  location  = {London},
}
\end{filecontents}


\begin{document}
Lorem \citep{".}

\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}

works, but

\documentclass[ngerman]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}

\usepackage{natbib}

\begin{filecontents}{\jobname.bib}
@book{"',
  author    = {Anne Elk},
  title     = {A Theory on Brontosauruses},
  year      = {1972},
  publisher = {Monthy \& Co.},
  location  = {London},
}
\end{filecontents}


\begin{document}
Lorem \citep{"'}

\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}

doesn't.

A popular character in labels is :, which does not work with babel-french if you don't listen to the warning produced by natbib

\documentclass[french]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}

\usepackage{natbib}

\begin{filecontents}{\jobname.bib}
@book{elk:bronto,
  author    = {Anne Elk},
  title     = {A Theory on Brontosauruses},
  year      = {1972},
  publisher = {Monthy \& Co.},
  location  = {London},
}
\end{filecontents}

\begin{document}
Lorem \citep{elk:bronto}

\bibliographystyle{plainnat}
\bibliography{\jobname}
\end{document}

: works if you load natbib before babel as recommended by the warning.

LaTeX offers you a great many ways to shoot yourself in the foot. For example in the following MWE k is no longer allowed in entry keys

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}

\begin{filecontents}{\jobname.bib}
@book{elk,
  author    = {Anne Elk},
  title     = {A Theory on Brontosauruses},
  year      = {1972},
  publisher = {Monthy \& Co.},
  location  = {London},
}
\end{filecontents}

\begin{document}
{\catcode`k=\active
\def k{\_}
Lorem \cite{elk}}

\bibliographystyle{plain}
\bibliography{\jobname}
\end{document}

Other tools

As mentioned in the beginning there are several tools out there that operate on .bib files and depending on the library or method they use to parse .bib entries they might come with their own restrictions.


OK, tell me which characters are safe already

Even though there are contrived examples to the contrary, if you want a list of safe characters, I'd definitely include

A-Za-z0-9

because LaTeX would become almost unusable in case those characters were made active.

I prefer to use only lowercase characters, because I don't trust case sensitivity, but that is just me being weird. I believe a-z0-9 already get you very far, though I think that at least one additional character for visual separation might be useful.

But then it already becomes tricky. . seems pretty safe, since I don't think it is usually made active. Many other punctuation commands could end up active and could break.

4
  • +1: Impressive answer. Commented Feb 5, 2021 at 7:43
  • 1
    Just to put it out there, it's not just you being weird, I have fourteen pages of sources in my thesis and all the keys in the .bib are lower case alphabetic characters only. To me, "key" is synonymous with "kiss" (keep it simple, stupid). :p (oh, and love your example reference *clears throat :p).
    – Plergux
    Commented Feb 5, 2021 at 7:56
  • I'm not sure about " in keys for BibTeX, besides space, brace and comma. Just nitpicking. Did you mean } instead of {? Because you later say that { is accepted.
    – egreg
    Commented Feb 5, 2021 at 10:21
  • @egreg Yes, I meant } is not allowed. { is fine for BibTeX. Fixed. Thanks for catching that typo. I was also sceptical about ", but BibTeX accepts them (see also the ". example).
    – moewe
    Commented Feb 5, 2021 at 12:27

You must log in to answer this question.

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