# % and non-ASCII characters in headings

It appears that a % together with some non-ASCII characters on a heading is causing problems for hyperref in xelatex. Here's a minimal working example:

\documentclass[]{article}
\usepackage{hyperref}
\begin{document}
\section{\%á}
\end{document}


It goes through with pdflatex but balks when running under xelatex in a second invocation. The error message is:

Runaway argument?
{
! File ended while scanning use of \@@BOOKMARK.
<inserted text>
\par
l.3    \begin{document}


This is noticed when try to convert a CJK markdown document into LaTeX/PDF using pandoc. See pandoc-discussion.

If this a bug? And suggestions of workaround would be much appreciated.

• Loading hyperref with the unicode option or loading bookmark solves the issue. – egreg Jun 6 '16 at 7:14
• Imho it is a bug. hyperref should in \HyPsd@SanitizeForOutFile (in hxetex.def) also take care of the percentchar. You should report it at github.com/ho-tex/hyperref/issues. – Ulrike Fischer Jun 6 '16 at 8:08
• @egreg I put it into an edef and inserted this into \section: \edef\temp{\%á} \section{\temp} Interestingly, it works until I insert the line \usepackage[utf8]{inputenc}. Even more interestingly, it works, if I substitute utf8 with latin9. Can you explain this (shortly)? – MaestroGlanz Jun 6 '16 at 8:35
• @MaestroGlanz Without inputenc, á is just a character (or a pair of characters if the file is UTF-8 encoded); with inputenc it depends on the encoding: with Latin-9 it is an active character expanding to a character, with UTF-8 the two (active) characters don't survive \edef. – egreg Jun 6 '16 at 8:40

I changed a bit your fine example just to make some more comparisons:

\documentclass[]{article}
\usepackage{ifxetex}
\ifxetex\else\usepackage[utf8]{inputenc}\fi
\usepackage{hyperref}
%\usepackage{bookmark}
\begin{document}
\section{\%á}
\section{\%a}
\end{document}


When pdflatex is used for processing, the .out file reads

\BOOKMARK [1][-]{section.1}{\045\341}{}% 1
\BOOKMARK [1][-]{section.2}{\045a}{}% 2


However, using xelatex produces

\BOOKMARK [1][-]{section.1}{%á}{}% 1
\BOOKMARK [1][-]{section.2}{\045a}{}% 2


and it is clear that something got wrong. Note that using lualatex makes the following .out file:

\BOOKMARK [1][-]{section.1}{\045á}{}% 1
\BOOKMARK [1][-]{section.2}{\045a}{}% 2


Solutions: either call \usepackage[unicode]{hyperref} or load bookmark. I'd recommend the latter way.

\documentclass[]{article}

\usepackage{hyperref}
\usepackage{bookmark}

\begin{document}

\section{\%á}

\section{\%a}

\end{document}

• They both worked, thanks! Here's something which baffles me: while \usepackage[unicode=true]{hyperref} works, \usepackage{hyperref} \hypersetup{unicode=true} produces the same error. – stl Jun 7 '16 at 9:09
• @stl It depends on the loading order: with \hypersetup the setting is done after setting up bookmarks. Something Heiko should look at. – egreg Jun 7 '16 at 9:13