Continuous LaTeX Compilation Using latexmk

Now that we know how to precompile the preamble to cut down the running time of latex, here is a tool that furthers the goal of saving time in the document production process—latexmk.

As its name suggests, latexmk is a “make” utility for LaTeX files. In particular, it understands when a particular document requires varying number of runs to resolve all references, with a bibtex and/or makeindex throw in at the right moment in-between the runs. Recent versions even support multiple bibliographies and the like. In short, assuming no source errors, if you execute latexmk foo.tex, you will get a fully compiled foo.dvi without any manual intervention. (PDF users: add the -pdf option. nomencl(ature) and glossaries package users: see below.)

Now latexmk would be a great piece of software to recommend just for the features I have mentioned above. Indeed, over the years I have seen various Makefile solutions using the make utility, some of which are fairly sophisticated. There are also direct competitors like texify and the like. (See this article in EmacsWiki.) While I find that latexmk fits my workflow in terms of ease of use and customizability, you may also want to check out these other intelligent options too. They all beat the “latex, latex, bibtex, latex, latex” batch script.

But latexmk is better than that—it has support for continuous/automatic compilation: if you run latexmk -pvc main.tex, then whenever it detects that one of the dependent source files of main.tex has been changed—be that a TeX, bib, an included graphics, or any file you declare to be a source, latexmk will automatically recompile it for you. That means you don’t need to start the compilation manually after changes and an oven-fresh dvi/pdf will be delivered to your previewer as soon as it is ready.

Finally, here are some notes on latexmk:

  • You really want the latest version, even though the version number may say “beta”. The script is very mature. Also note that MiKTeX ships with latexmk.exe, which is a wrapper to call Perl on TEXROOT\scripts\latexmk\perl\latexmk.pl
  • , which is the script you want to replace.

  • You can set up your .latexmkrc to override defaults or even define new dependencies. Below I have attached some interesting bits of my .latexmkrc just for an example. For instance, you can avoid launching a previewer even though you are using the -pvc option, and you can support the nomencl package and the like by declaring a custom dependency. And since this is a Perl script, you can use any Perl constructs. You can read the documentation for more possibilities.

    $latex = 'latex -src-specials -parse-first-line -c-style-errors';
    $pdflatex = 'pdf' . $latex;
    $dvi_previewer = 'exit'; # don't start a previewer for me
    @default_files = ('main');
    $clean_ext = # space separated string
    join(' ', qw( fmt acn acr alg gls glo glg ist nls nlo nlg brf out pdfsync rel));
    push @cus_dep_list, "nlo nls 0 nlo2nls"; # nomenclature
    sub nlo2nls { system("makeindex $_[0].nlo -s nomencl.ist -o $_[0].nls -t $_[0].nlg"); }
    push @cus_dep_list, "acn acr 0 acn2acr"; # glossaries and acronym hack
    sub acn2acr { system("makeindex $_[0].acn -s main.ist -o $_[0].acr -t $_[0].alg"); }
    push @cus_dep_list, "glo gls 0 glo2gls"; #MiKTeX 2.6 has a broken makeglossaries.exe
    sub glo2gls { system("makeindex $_[0].glo -s main.ist -o $_[0].gls -t $_[0].glg"); }

  • The -silent option suppresses the output of latex and show you only the errors in a succinct format. Here is an example transcript when I run latexmk -silent foo.tex with two planted errors.

    No specific requests made, so default to dvi by latex
    Latexmk: Run number 1 of rule 'latex'
    Latexmk: Running 'latex -src-specials -parse-first-line -c-style-errors -interaction=batchmode -c-style-errors "foo.tex"'
    This is pdfTeX, Version 3.141592-1.40.4 (MiKTeX 2.6)
    entering extended mode
    foo.tex:3: Undefined control sequence
    foo.tex:4: Undefined control sequence
    Latexmk: Use the -f option to force complete processing.

Have fun with latexmk!

P.S. I should note one fine point: if you have the habit of saving your file after every single sentence, then perhaps continuous compilation can be a bit counterproductive. Emacs users can consider tuning their auto-save-timeout and auto-save-interval instead.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>