Using Latexmk

If you use cross-references, you often have to run LaTeX more than once, if you use BibTeX for your bibliography or if you want to have a glossary you even need to run external programs in-between.

To avoid all this hassle, you should simply use Latexmk!

Latexmk is a Perl script which you just have to run once and it does everything else for you ... completely automagically.

And the nice thing is: you probably have it already installed on your computer, because it is part of MikTeX and it is bundled with many Linux Distributions.

Installation

On M$ Windows with MikTeX:

  • You probably have to install Perl, e.g. from here: http://strawberryperl.com/.
  • If it’s not installed already, open the MikTeX Package Manager and install the latexmk package.

On Linux:

  • Perl is should be already installed.
  • You may have to install a package called latexmk or similar.

On OSX:

  • tlmgr install latexmk
  • tlmgr is the Tex Live package manager.
  • You’ll probably need to use sudo tlmgr install latexmk on OSX

Running Latexmk

Latexmk is a command line application, see below for how to use it with batch files.

In the simplest case you just have to type

latexmk

This runs LaTeX on all .tex files in the current directory using the output format specified by the config files.

If you want to make sure to get a .pdf file as output, just mention it:

latexmk -pdf

If you want to use latex instead of pdflatex but still want a .pdf file in the end, use

latexmk -pdfps

If you want to compile only one specific .tex file in the current directory, just provide the file name:

latexmk myfile.tex

If you want to preview the resulting output file(s), just use

latexmk -pv

And now the Killer Feature: If you want Latexmk to continuously check all input files for changes and re-compile the whole thing if needed and always display the result, type

latexmk -pvc

Then, whenever you change something in any of your source files and save your changes, the preview is automagically updated. But: This doesn’t work with all viewers, especially not with Adobe Reader. See the section about configuration files below for setting a suitable viewer application.

Of course, options can be combined, e.g.

latexmk -pdf -pv myfile.tex

Cleaning Up

After running LaTeX, the current directory is contaminated with a myriad of temporary files; you can get rid of them with

latexmk -c

This doesn’t delete the final .pdf/.ps/.dvi files. If you want to delete those too, use

latexmk -C

Running Latexmk with Batch Files

If you are working on M$ Windows, you may not be used to typing things at the command line. You may prefer clicking on things.

No problem, just create a file (in the same folder as your .tex files) with the extension .bat containing

latexmk
@pause

and double-click on it.

You can of course use all the abovementioned options, don’t forget the especially useful ones -c and -pvc.

Configuration Files

System-wide config file for Windows: C:\latexmk\LatexMk

This file should contain:

$pdf_previewer = "start gsview32";

You’ll need GSview and Ghostscript for that, see http://pages.cs.wisc.edu/~ghost/gsview/.

On Linux/OSX Systems, you can put your configurations into $HOME/.latexmkrc, which could contain something like this:

$dvi_previewer = 'start xdvi -watchfile 1.5';
$ps_previewer  = 'start gv --watch';
$pdf_previewer = 'start evince';

Some previewers use different methods for updating the viewed PDF file. You can change that with $pdf_update_method, like in this example:

$pdf_update_method = 4;
$pdf_update_command = 'bla bla bla';

Full documentation is available in the manpage.

Local Configuration Files

You can also put a configuration file in the current directory for settings which only influence files in the current directory. Such a configuration file has to be named latexmkrc or .latexmkrc and may contain some of the following lines.

To specify if you want PDF or PS output, choose one of those:

$pdf_mode = 1;        # tex -> pdf
$pdf_mode = 2;        # tex -> ps -> pdf
$postscript_mode = 1; # tex -> ps

If you have your work split up into several parts, you have to specify the main file like this:

@default_files = ('main.tex');

Or maybe you want to process several files:

@default_files = ('file-one.tex', 'file-two.tex');

Note

If you don’t specify @default_files, all .tex files in the current directory will be used.

Advanced Options

Latexmk can also do more crazy stuff.

For example it can create a nomenclature (you’ll have to use the nomencl package) like this:

@cus_dep_list = (@cus_dep_list, "glo gls 0 makenomenclature");
sub makenomenclature {
   system("makeindex $_[0].glo -s nomencl.ist -o $_[0].gls"); }
@generated_exts = (@generated_exts, 'glo');

Or, if you are creating your figures in EPS format but you need them in PDF, you can tell Latexmk to convert them for you:

@cus_dep_list = (@cus_dep_list, "eps pdf 0 eps2pdf");
sub eps2pdf {
   system("epstopdf $_[0].eps"); }

If you need to enable shell escape for \write18 (e.g. for on-the-fly figure generation):

$latex = 'latex -interaction=nonstopmode -shell-escape';
$pdflatex = 'pdflatex -interaction=nonstopmode -shell-escape';

And finally, if latexmk -c refuses to remove certain files, you can specify their extensions and next time they’ll be gone:

$clean_ext = "bbl nav out snm";

Have fun!