[Tex/LaTex] Make latexmk 4.54c use synctex with xelatex

forward-inverse-searchlatexmkxetex

I usually want latexmk to use pdflatex and synctex by default, so I have the following in my RC file and everything works hunky-dory:

$pdf_mode = 1;
$pdflatex = 'pdflatex -interaction=nonstopmode -synctex=1 %O %S';

However, some documents I want to compile into PDFs using xelatex and synctex. How can I change my RC file so that latexmk still uses pdflatex and synctex by default, but if I pass a single command-line option, it will instead use xelatex and synctex?

I tried (among many other things) the following RC file:

$pdf_mode = 1;
$pdflatex = 'pdflatex -interaction=nonstopmode -synctex=1 %O %S';
$xelatex = 'xelatex -interaction=nonstopmode -synctex=1 %O %S';

With this file, invoking latexmk without arguments still works as expected, but running latexmk -xelatex or latexmk -pdfxe throws an error message:

Latexmk: Log file says output to 'foo.pdf'
Latexmk: ===For rule 'xelatex', actual output 'foo.pdf'
       ======appears not to match expected output 'foo.xdv'
Failure to make 'foo.xdv'
Latexmk: Errors, so I did not complete making targets
Collected error summary (may duplicate other messages):
  xelatex: failed to create output file
Latexmk: Use the -f option to force complete processing,
 unless error was exceeding maximum runs of latex/pdflatex.

There is a previous similar thread on this subject but the solution given there works only for older versions of latexmk. According to a comment by the author, the behaviour of the program has since changed, and latexmk should now "do what you want", though I haven't been able to figure it out.

Best Answer

latexmk splits the xelatex compilation. From the documentation

Latexmk first uses xelatex to make an xdv file, and does all the extra runs needed (including those of bibtex, etc). Only after that does it make the pdf file from the xdv file, using xdvipdfmx.

To get this behaviour the original definition for the variable $xelatex contains the -no-pdf switch:

# xelatex is used to give xdv file, not pdf file
$xelatex = 'xelatex -no-pdf %O %S';

If you want to change the variable you should use the switch too.

Related Question