Latexmk

If you use cross-references, you often have to run LaTeX multiple times. 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 takes care of everything else for you.

Follow the instruction given below for a first test drive with Latexmk using the CLI. See the online reference of Latexmk for further information.

Replace FILE with the name of the main file. To build the template use myFile.tex in place of FILE

Note

The BFH

CLI Usage

  • In the simplest case you just have to type

    $ latexmk
    

    This will run LaTeX on all .tex files in the current directory using the output format specified by the configuration files.

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

    $ latexmk FILE
    
  • 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 automatically updated.

    Warning

    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. See the example given below.

    $ latexmk -outdir=_build -pv FILE
    

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
    
  • Previous command doesn’t delete the final .pdf/.ps/.dvi files. If you want to delete those too, use

    $ latexmk -C
    

Configuration File

To customize some latexmk build setting such as the default viewer for the preview, edit the file .latexmkrc. A snippet with some good pre-configurations for latexmk is given below. Add the configuration file to the top-level directory in your documentation folder.

Note

The snippet below is in the so called heredoc format. On a GNU/Linux Shell it creates the .latexmkrc configuration file with the content between the first and last line of that snippet.

If one is not able to use command line, one can copy the snippet without the first cat << "EOF" > .latexmkrc and the last EOF line, into a file called .latexmkrc

cat << "EOF" > .latexmkrc
##
## Default PDF viewer -- system dependent setting
##    See the documentation -- https://mirror.foobar.to/CTAN/support/latexmk/latexmk.pdf
## Windows
#$pdf_previewer = 'start acroread %O %S';
## OS X
#$pdf_previewer = 'open %S';
## GNU/Linux (Debian/Ubuntu like)
#$pdf_previewer = 'start xdg-open %S';

##
## EPS to PDF conversion hook
##
@cus_dep_list = (@cus_dep_list, "eps pdf 0 eps2pdf");
sub eps2pdf {
   system("epstopdf $_[0].eps");
  }

##
## GLO to GLS conversion hook
##
add_cus_dep( 'slo', 'sls', 0, 'makeglossaries' );
add_cus_dep( 'acn', 'acr', 0, 'makeglossaries' );
add_cus_dep( 'glo', 'gls', 0, 'makeglossaries' );
$clean_ext .= "slo sls slg acr acn alg glo gls glg";
sub makeglossaries {
   my ($base_name, $path) = fileparse( $_[0] );
   pushd $path;
   my $return = system "makeglossaries", $base_name;
   popd;
   return $return;
}

##
## To enable shell-escape for all *latex commands  
##   Used i.e. for svg package invoking inkscape
##
set_tex_cmds( '--shell-escape %O %S' );

##
## Set default TeX file
##
#@default_files = ('reportTitle.tex');

##
## Latexmk build properties
##
$pdf_mode = 4;
$postscript_mode = $dvi_mode = 0;
$bibtex_use = 1;

##
## Build directory
##
$out_dir = '_build';

##
## Post process hooks (Linux, OS X only; For Windows install CLI tools)
##
## Copy PDF to a sub directory named "_output"
#$success_cmd = 'mkdir -p _output && cp _build/*.pdf _output/';
## Copy PDF to a sub directory named "_output" and create a link from top level to the PDF file
#$success_cmd = 'mkdir -p _output && cp _build/*.pdf _output/ && ln -s _output/%R.pdf';

##
## List of file extensions to clean up
## 
$clean_ext .= '%R.aux %R.dvi %R.log %R.out tex~';
$clean_full_ext = 'bbl synctex.gz _build';
EOF