Using Latexmk§

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

To avoid all this hassle, you should simply useLatexmk!

Latexmk is aPerl script which you just have to run once and it doeseverything else for you … completely automagically.

And the nice thing is: you probably have it already installed on your computer,because it is part ofMacTeX andMikTeX and it is bundled with many LinuxDistributions.

Installation§

OnLinux:
  • Perl should be already installed.

  • You may have to install a package calledlatexmk or similar.

OnmacOS withMacTeX:
  • It’s probably already installed.

  • If not, open “TeX Live Utility”, search for “latexmk” and install it.

  • If you prefer using the Terminal:

    sudo tlmgr install latexmk
OnWindows withMikTeX:
  • You probably have to installPerl,e.g. from here:https://strawberryperl.com/.

  • If it’s not installed already, open the MikTeX Package Manager and installthelatexmk package.

Running Latexmk§

Latexmk is a command line application, seebelow for how to use itwith 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 outputformat specified by theconfiguration files.

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

latexmk-pdf

If you want to uselatex instead ofpdflatex but still want a.pdffile 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:

latexmkmyfile.tex

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

latexmk-pv

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

latexmk-pvc

Then, whenever you change something inany of your source files and save yourchanges, the preview is automagically updated.But: This doesn’t work with all viewers, especially not withAdobe Reader.See the section aboutconfiguration files below for setting a suitable viewerapplication.

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

latexmk-pdf-pvmyfile.tex

Cleaning Up§

After running LaTeX, the current directory is contaminated with a myriad oftemporary 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 onWindows, you may not be used to typing things at thecommand 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 theespecially useful ones-c and-pvc.

Configuration Files§

OnLinux, 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';

OnmacOS, you can also use$HOME/.latexmkrc, e.g. with this content:

$pdf_previewer='open -a Skim';$pdflatex='pdflatex -synctex=1 -interaction=nonstopmode';@generated_exts=(@generated_exts,'synctex.gz');

This usesSkim as preview application, which can be set up to automaticallyupdate its display when the PDF file changes by selecting“Preferences” – “Sync” – “Check for file changes”.While you are at it, you should also activates theSyncTeX feature byselecting you editor right below in the “PDF-TeX Sync support” section.With this selected and with-synctex=1 in your LaTeX call, you canShift-⌘-click in the preview window and jump directly to the correspondingsource text in your editor!

OnWindows, you can use the system-wide config fileC:\latexmk\LatexMk(if the file doesn’t exist yet, just create a new text file with this name).To choose a PDF viewer, use something like this:

$pdf_previewer='start gsview32';

You’ll needGSview andGhostscript for that,seehttp://pages.cs.wisc.edu/~ghost/gsview/.

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 themanpage.

Local Configuration Files§

You can also put a configuration file in the current directory for settingswhich only influence files in the current directory.Such a configuration file has to be namedlatexmkrc or.latexmkrc andmay 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 mainfile 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 thecurrent 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 thenomenclpackage) like this:

@cus_dep_list=(@cus_dep_list,"glo gls 0 makenomenclature");submakenomenclature{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, youcan tell Latexmk to convert them for you:

@cus_dep_list=(@cus_dep_list,"eps pdf 0 eps2pdf");subeps2pdf{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, iflatexmk-c refuses to remove certain files, you can specifytheir extensions and next time they’ll be gone:

$clean_ext="bbl nav out snm";

Have fun!