# Why does hyperref complain about “token not allowed in a PDF string” in my glossary when I've set the default language?

I have a glossary defined with the glossaries package, and I use the hyperref package to control aspects of the generated PDF file. When I set the document's language with polygossia's \setmainlanguage command, I get the following warning near the end of compilation (whether with lualatex or xelatex):

(./test.toc) (./test.gls

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing \textenglish' on input line 1.

)


It evidently doesn't like the \textenglish command that appears in the table of contents:

\contentsline {section}{\textenglish {Glossary}}{1}{section*.2}


If I don't set the language, then the warning doesn't appear.

\documentclass{article}
\usepackage{polyglossia}
% Removing the following line makes the warning go away,
% but it introduces a biblatex error instead
\setmainlanguage{english}

\usepackage[backend=biber]{biblatex}

\usepackage{hyperref}
\usepackage[toc]{glossaries}

\makeglossaries
\newglossaryentry{foo}{
name=foo,
description={is foo}
}

\begin{document}
\tableofcontents

\section{foobar}
\gls{foo}

\printglossary
\end{document}


I've read related questions that all recommend using \texorpdfstring to separate the text that goes into the PDF table of contents, but I'm not sure where I'm supposed to do that in this case since I'm not the one generating the table-of-contents code in the first place.

I'm not sure what effect \textenglish really has on my document, especially considering all the text is already in English anyway. I can see that it comes from glossaries-english.ldf when polyglossia has been loaded, but I'm unsure what other effects the loading order has. Besides, if I remove the \setmainlanguage command, then compilation fails at \begin{document}, apparently because biblatex lacks some language information:

! Undefined control sequence.
<argument> blx@lng@\bbl@main@language

l.16 \begin{document}


How do I quell the hyperref warning?

• this sounds like a complaint about math (or other non-pdf-able material) in a bookmark. you might be able to quell it simply by excluding \textenglish from the pdf string: \texorpdfstring{\textenglish}{}; that would go into the \section entry that is in turn used for the contents and the bookmarks. (not tested.) – barbara beeton Jan 2 '15 at 19:48
• Right, @Barbara, but as I said, I'm not the one who's writing \section anywhere. It's generated on my behalf somewhere inside of \printglossary. – Rob Kennedy Jan 2 '15 at 19:50
• looking into the documentation for the glossaries-english package, it looks like you may be able to get around the problem with a simple definition: \renewcommand{\glossaryname}{\texorpdfstring{\textenglish}{}{Glossary}}. again, not tested (don't have latest version of this package installed locally). if this fails, it's a question for @NicolaTalbot, the author of the glossaries package. – barbara beeton Jan 2 '15 at 20:03

You can either remove the \textenglish locally when you call \printglossary via the following redefinition in your preamble:

\let\oldprintglossary\printglossary
\renewcommand{\printglossary}{{\let\textenglish\relax\oldprintglossary}}


But this may affect other uses of \textenglish inside the glossary, if any. Another alternative might be to update \glossaryname. However, the easiest way to get around this (in my opinion) is to specify your own title for the glossary using the title key:

\printglossary[title=Glossary]


This would override any language-specific alterations and set it as-is.

• Redefining \glossaryname to use \texorpdfstring didn't seem to have any effect. Specifying the title at the time of \printglossary indeed seems like the least amount of hassle. – Rob Kennedy Jan 2 '15 at 22:46

You can use hyperref's \pdfstringdefDisableCommands:

\documentclass{article}
\usepackage{polyglossia}
\setmainlanguage{english}

\usepackage[backend=biber]{biblatex}

\usepackage{hyperref}
\usepackage[toc]{glossaries}

\pdfstringdefDisableCommands{%
\let\textenglish\@firstofone
}

\makeglossaries
\newglossaryentry{foo}{
name=foo,
description={is foo}
}

\begin{document}
\tableofcontents

\section{foobar}
\gls{foo}

\printglossary
\end{document}
`