[Tex/LaTex] hyperref error when using XeLaTeX and fontspec with LaTeXmk

fontspechyperreflatexmkxetex

I have defined a compilation tool for TeXWorks as follows:

Program: latexmk
Arguments: -e
           $xelatex=q/xelatex $synctexoption %0 %S/
           -pdf
           $fullname

It works fine in most cases, but I'm having problems with one file that imports the hyperref package. Here's a minimal example:

\documentclass{article}
\usepackage{fontspec}
\usepackage[xetex,bookmarks=true,colorlinks=true,linkcolor=headernfooter,urlcolor=headernfooter,linktoc=section,]{hyperref}
\begin{document}
hello, paper!
\end{document}

If I use the (predefined) XeLaTeX compilation tool instead, it works. If I comment the line importing hyperref, it works. But if I try to combine them, I get the following error message:

! Package hyperref Error: Wrong driver option `xetex',
(hyperref) because XeTeX is not detected.

If I remove the xetex option and the import of fontspec, both work, but then I can't choose the fonts I want in my real document.

How do I combine these requirements?

Best Answer

There is a xetex option for hyperref, but it's recommended not using it. In some cases it's necessary to pass hyperref an explicit driver option, but not when using

• LaTeX+dvips
• pdfLaTeX
• XeLaTeX
• LuaLaTeX

because in these cases hyperref is able to detect the driver automatically. The indication of the driver is necessary for

• LaTeX+dvipdfmx

or some other less common engines (VTeX, for instance).

Notice that the same holds for other commonly used packages whose behavior changes depending on the engine and driver used:

graphicx, color, xcolor, geometry

don't need to be passed explicitly a driver name. For geometry it may be necessary when used alongside with crop.

However (thanks to Ulrike Fischer for pointing it out), a xetex options should do no harm if the engine used is XeTeX and raise an error otherwise. So in your case this means that latexmk is not using XeLaTeX for processing your document.

As Brent Longborought remarks, the argument to be passed to latexmk should be

$pdflatex=q/xelatex $synctexoption %0 %S/

(notice pdflatex just after the first $ symbol).

Related Question