# Non-English identifiers in \label / \ref (“Missing \endcsname inserted”)

I found that LaTeX chokes on certain non-ASCII identifiers in \ref.

This works fine:

\documentclass{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\begin{document}

\section{Foobär}
\label{sec:Foobär}

See \ref{sec:Foobär}

\end{document}


The same thing with "Barbaß" instead of "Foobär" aborts with "Missing \endcsname inserted." (huh?).

Now, the naive sollution would be "don't use non-ASCII identifiers", of course. But since I'm using some macro magic to get labels auto-defined by section headers / table captions, as well as generating some surrounding text for references, I would really like to use native language in labels, instead of crimping them into ASCII-7.

Any way to get this done?

-
Barbaß works with XeLaTeX and LuaLaTeX. –  Qrrbrbirlbel Jan 14 '13 at 10:39
Other than feeding both the argument of \label and of \ref to \detokenize there isn't much you can do with standard LaTeX. –  egreg Jan 14 '13 at 11:03

From "The LaTeX Companion", 2nd edition:

2.4 Managing references

[...] To allow cross-referencing of elements inside a document, you should assign a key (consisting of a string of ASCII letters, digits, and punctuation) to the given structural element and then use that key to refer to that element elsewhere.

\label{key}  \ref{key}  \pageref{key}


Technically the key should survive \csname...\endcsname. Package babel makes some punctuation active as shorthand characters, but it patches the referencing system to allow shorthands inside keys. That can be used to redefine \IeC that is an internal wrapper for the Unicode letters of utf8.def:

\documentclass{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\makeatletter
\renewcommand*{\IeC}{%
\ifx\protect\@typeset@protect
\if@safe@actives
\expandafter\expandafter\expandafter\IeC@detokenize
\else
\expandafter\expandafter\expandafter\@firstofone
\fi
\else
\noexpand\IeC
\fi
}
\newcommand*{\IeC@detokenize}[1]{\detokenize{#1}}
\makeatother

\begin{document}

\section{Barbaß}
\label{sec:Barbaß}

See \ref{sec:Barbaß}

\end{document}

-
yes you're braver than I am:-) I chickened out of redefining IeC and made a new command instead, but then I had to redefine ref to use it, so probably redefining IeC is better in the end:-) apart from that we both just added \detokenize (which wasn't available when we did inputenc as we were restricting to tex3 at the time) –  David Carlisle Jan 14 '13 at 13:03
Looks like something that should find its way into the proper packages somehow... –  DevSolar Jan 14 '13 at 13:18
Sheesh, will you look at this: Heiko's solution works fine. But if I add \usepackage{thumbs} to the preamble, and \begin{titlepage} \begin{center} Foo \end{center} \end{titlepage} to the body, the same error pops up again. That's probably the most illogical way-to-reproduce I've posted so far, but there it is. :-D Anyway, that is hardly Heiko's fault, so this is the accepted answer. –  DevSolar Jan 14 '13 at 14:18
@DevSolar I could not reproduce the problem with package thumbs. –  Heiko Oberdiek Jan 14 '13 at 15:37

Hmm This seems to work, adding a bit of extra etex to the latex protection mechanism:

\documentclass{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\makeatletter

\def\IeCb#1{\string\IeCb{\detokenize{#1}}}
\def\label#1{\@bsphack
\protected@write\@auxout{\let\IeC\IeCb}%
{\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
\@esphack}

\let\oldref\ref
\protected\def\ref#1{\let\IeC\IeCb\oldref{#1}}

\let\oldpageref\pageref
\protected\def\pageref#1{\let\IeC\IeCb\oldpageref{#1}}

\makeatother

\begin{document}

\section{Foobärß}
\label{sec:Foobärß}

See \ref{sec:Foobärß}

\end{document}

-