Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Are there any precompiler options for latexmk I can use inside my LaTeX source files? (For instance, to tell latexmk to compile using XeLaTeX instead of PDFLaTeX.) I know how to do that with the .latexmkrc file, but it would be more useful to me if I could do it within my source files.

share|improve this question
I think you are searching for Arara –  mhp Jul 23 '12 at 7:29
I think something like the arara parsing should be easy to add to latexmk. Send an email to the latexmk developer and suggest it, it is a useful feature. –  daleif Jul 23 '12 at 8:20
@daleif: In my opinion, the current approach, i.e. adapting the relevant rules in a local Latexmk initialization file, is cleaner and, potentially, also more secure. –  mhp Jul 23 '12 at 10:43
True, but anoying for editor users who do not understand the various syntaxes for configuring stuff. in that sense I like Emacs' concept of local variables to be set inside the document and used to configure which engine Emacs should use. –  daleif Jul 23 '12 at 11:07
@daleif: Yes, but there is a reason why Emacs doesn’t let you easily set unsafe file-local variables. –  mhp Jul 23 '12 at 12:06

3 Answers 3

up vote 10 down vote accepted

Here's an improved way of using latexmk from within arara (my thanks to Paulo for helping sort out my initial catastrophic mess).

First, install aRaRa (you'll never look back)

Second, cut-and-paste this into a file in your arara rules directory called xelatexmk.yaml:

# LaTeXmk with XeTeX rule for arara
# author: Brent Longborough
# last edited by: Brent Longborough
# made to work by: Paulo Cereda
identifier: xelatexmk
name: XeLaTeXmK
command: 'latexmk -e "$pdflatex=q/xelatex @{ action == "" ?  "" : "--interaction=" + action  } @{shell} @{ synctex == "" ? "--synctex=1" : synctex } @{expandoptions} %O %S/" -pdf @{file}.tex'
- identifier: action
  flag: '@{value}'
- identifier: shell
  flag: '@{value.toLowerCase() == "yes" || value.toLowerCase() == "true" || value.toLowerCase() == "on" ? "--shell-escape" : "--no-shell-escape" }'
- identifier: synctex
  flag: '@{value.toLowerCase() == "no" || value.toLowerCase() == "false" || value.toLowerCase() == "off" ? "--synctex=0" : "--synctex=1" }'
- identifier: expandoptions
  flag: '@{value}'

Then, in your TeX file, one of these, or something else that suits you:

% arara: xelatexmk
% arara: xelatexmk: { synctex: no }

The addition of an optional style file designator for makeindex is left as an exercise for the reader.

share|improve this answer
What's the advantage of this solution over the predefined xelatex.yalm? –  NVaughan Jul 23 '12 at 19:30
@NVaughan: with the xelatex rule, you are running... well, xelatex on your .tex file. With Brent's rule, you are running xelatex via latexmk and all the goodies from the latter are automatically available - e.g, no compilation is triggered if the file is updated. Brent is passing the compilation options to latexmk via arara directives. :) –  Paulo Cereda Jul 23 '12 at 20:06
@PauloCereda Cool! Thanks for arara. BTW, why did you choose coding it in java? –  NVaughan Jul 23 '12 at 20:23
@NVaughan: A cite from arara’s usermanual: “Another feature worth mentioning is the fact that arara is platform independent. I wrote it in Java, so arara runs on top of a Java virtual machine, available on all the major operating systems …” –  Speravir Jul 23 '12 at 22:51
@Speravir Thanks. I just was wondering, because Perl and Python are also platform independent... –  NVaughan Jul 23 '12 at 23:36

I don't think latexmk does any parsing of input files. It parses the output/log files to determine which files to be looking at, etc, but I'm pretty sure it doesn't actually read the input files themselves, which would mean that this wouldn't work.

An option I haven't explored fully might be to write a small parser script of your own, and specify this as the LaTeX processing program to use. In your script, read the .tex file, and check for preprocessing directives that you have decided on, then act accordingly in choosing the right compiler, which options, etc.

share|improve this answer
My understanding is that Latexmk does no parsing at all; it (mainly) compares MD5 hashes. Moreover, it can make use of the -recorder option of pdftex. –  mhp Jul 23 '12 at 7:50
You're right about the hashes. I may not have been clear, I was simply meaning it parses the output to determine which files it should be hashing. It does do a small amount of file reading, but only for a couple of cases, like the hash_cavlc_ignore_pattern option –  lxop Jul 23 '12 at 20:46
Yes, that’s right. For instance, Latexmk inspects the log file to find the source files. –  mhp Jul 23 '12 at 21:11

As mph and daleif have suggested, I tried arara. It looks promising. After installation and setup, one just has to include a series of precompiler commands describing the compilation process. E.g.,

% arara: xelatex
% arara: xelatex

\documentclass[12pt, a4paper, article, oneside]{memoir}
. . . 

Or more complex setups, as e.g.,

% arara: pdflatex
% arara: makeindex: { style: test.ist }
% arara: biber
% arara: pdflatex
% arara: pdflatex
. . . 

However, arara doesn't seem to be quite as “smart” as latexmk. For instance, if I want to clean the auxiliary files, I must do that from "within" the source code---obviously, as it is a precompiler command. But that means that the aux files are trashed after every successful compilation, which is not very useful always. Moreover, one must always declare expressly which aux files to trash---of which, by contrast, latexmk has a default set predefined.

% arara: pdflatex
% arara: pdflatex
% arara: makeindex
% arara: pdflatex
% arara: clean: { files: [ helloindex.aux, helloindex.idx, helloindex.ilg, helloindex.ind, helloindex.log ] }

As this code sample taken from the user manual shows, you cannot state which aux file extensions to trash (presumably because arara doesn't check whether those files belong to the compiled source). So you have to explicitly declare them in the precompiler option.

Perhaps arara will become “smarter” in future versions. As for now, it seems to me that latexmk is more productive and efficient, especially when you have a local .latexmkrc file declaring special compilation parameters.

share|improve this answer
Please ask a new question. The rule for cleaning can be improved. However this should be a separate question –  Marco Daniel Jul 23 '12 at 16:56
I don't think this is a new question---indeed, it's not a question but an answer to my original question. Cleaning is a feature of both latexmk and arara. –  NVaughan Jul 23 '12 at 17:44
I meant the cleaning rule of arara. As I understood you answer correct you are wishing a more intuitive way. –  Marco Daniel Jul 23 '12 at 17:53
Yes, I was trying to see whether the functionality of latexmk can be obtained using pre-compiler instructions, and I was directed to arara. But after checking it out, I found some shortcomings, in comparison to latexmk. –  NVaughan Jul 23 '12 at 17:57
I wish I could take a photo of my notes when I was thinking about arara - they are a mess! :) In the beginning, I thought of performing checksums, log analysis and other cool stuff in order to ensure the exact number of passes needed to generate a proper PDF from a .tex file. But then I gave up, not because it was difficult, but we already have great tools which do a very fine job - e.g, rubber and the awesome latexmk (I'm a fan of John Collins). I decided to write a general-purpose tool to ease the automation process - arara can help with it, but not the compilation itself. (CTD) –  Paulo Cereda Jul 23 '12 at 20:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.