[Tex/LaTex] latexmk with xelatex xdv mode

latexmkxetex

I want to know how to use latexmk with xelatex and tell it to write to xdv file a number of times until all the references are correct, then use xdvipdfmx once to generate the final output. This is practical because for manuscripts with a lot of figures and references and needs several passes to get all the references right, writing to xdv files are way, way faster than writing to pdf every pass.

Best Answer

Important update to old answer

The old answer (below) was appropriate when it was written (in April 2014), but it uses a nasty trick. Since then, changes in latexmk's support of xelatex were made, starting in v. 4.52. Latexmk now does exactly what the question asked for, without any special configuration. For details, see the latexmk documentation, in the explanations of the option -pdfxe and the variable $xelatex.

So if you invoke a current version of latexmk by latexmk -pdfxe or latexmk -xelatex, it will now do exactly what was asked.

Old Answer

With the present latexmk, a simple trick is to arrange to treat this by the dvi-pdf route for making pdf files. You do this by getting the 'xdv' file copied to a 'dvi' file. The following lines in an initialization file implement this:

$pdf_mode = 3;
$postscript_mode = $dvi_mode = 0;
$latex = 'xelatex --no-pdf %O %S && cp %Z%R.xdv %D';
$dvipdf = 'xdvipdfmx %O -o %D %S';
$clean_full_ext .= ' xdv';

The definition of $latex is appropriate for UNIX-like operating systems (e.g., linux and OS-X), and will need to be changed for MS-Windows.

Given the improved performance by this method, I should enhance latexmk to do it properly, with a -pdfxdv option in addition to its current -pdfdvi option.

Notes

The tail && cp %Z%R.xdv %D of the command line defined in $latex uses an idiom of the sh/bash shells: The part to the right of the && is run only if the command on the left (i.e., xelatex) succeeded. The cp part does the copying (under UNIX-type operating systems) of the xdv file to the dvi file. %Z denotes the output directory with any necessary trailing separator, %R denotes the rootname of the 'tex' file, and %D denotes the destination file, i.e., the dvi file; these will be replaced by the correct values by latexmk, as explained in its documentation.