LinuxCommandLibrary

latexmk

Automatically build LaTeX documents

TLDR

Compile a DVI (Device Independent file) document from every source

$ latexmk
copy

Compile a DVI document from a specific source file
$ latexmk [path/to/source.tex]
copy

Compile a PDF document
$ latexmk -pdf [path/to/source.tex]
copy

Open the document in a viewer and continuously update it whenever source files change
$ latexmk -pvc [path/to/source.tex]
copy

Force the generation of a document even if there are errors
$ latexmk -f [path/to/source.tex]
copy

Clean up temporary TEX files created for a specific TEX file
$ latexmk -c [path/to/source.tex]
copy

Clean up all temporary TEX files in the current directory
$ latexmk -c
copy

SYNOPSIS

latexmk [options] [file ...]

PARAMETERS

-c
    Cleans up auxiliary files generated during compilation, keeping the main output file (e.g., .pdf, .dvi).

-C
    Cleans up all generated files, including the main output file, reverting the directory to its state before compilation.

-pvc
    Runs in a continuous preview mode. It watches source files for changes and automatically recompiles the document whenever a modification is detected.

-pdf
    Forces latexmk to use `pdflatex` (or `lualatex`/`xelatex` if specified) for compilation, resulting in a PDF output file.

-ps
    Forces latexmk to produce a PostScript output file.

-dvi
    Forces latexmk to produce a DVI output file.

-lualatex
    Instructs latexmk to use the `lualatex` engine for compilation.

-xelatex
    Instructs latexmk to use the `xelatex` engine for compilation.

-f
    Forces latexmk to continue compilation even if errors are encountered during a run, which can be useful for debugging.

-r
    Specifies an alternative configuration file to read instead of the default `.latexmkrc`.

-rules
    Prints out the compilation rules and dependency information that latexmk uses internally.

-time
    Prints diagnostic information about the time taken for various compilation steps.

DESCRIPTION

latexmk is a powerful Perl script designed to completely automate the process of building LaTeX documents. It intelligently determines the correct sequence of commands (like pdflatex, bibtex, makeindex, etc.) and runs them the necessary number of times to resolve all cross-references, citations, and indices. This eliminates the need for users to manually run multiple compilation passes.

It continuously checks for changes in source files (with the -pvc option) and recompiles as needed, making it ideal for active document development. latexmk supports various output formats including PDF, DVI, and PostScript, and can be configured to use different TeX engines like LuaTeX or XeTeX. It also handles cleaning up auxiliary files, simplifying project management.

CAVEATS

Requires a complete TeX distribution (e.g., TeX Live, MiKTeX) to function, as it acts as an orchestrator for TeX compilers and related utilities.
Its dependency resolution is highly effective but might not cover extremely unusual or custom build steps without explicit configuration via a latexmkrc file.
For continuous compilation (-pvc), file watching capabilities can vary across operating systems; on some, external tools like fswatch might be implicitly or explicitly required for optimal performance.

CONFIGURATION FILES

latexmk can be extensively customized using configuration files named .latexmkrc (or latexmkrc on Windows). These files, written in Perl, allow users to define custom commands, specify preferred TeX engines, configure output directories, and override default behaviors. This provides immense flexibility for complex or specialized LaTeX projects.

DEPENDENCY TRACKING

A core strength of latexmk lies in its sophisticated dependency tracking. It parses log files and examines auxiliary files generated during compilation to determine if cross-references, bibliographies, indices, or glossaries are out of date. It then automatically triggers subsequent compilation runs with the necessary tools until all dependencies are resolved and the document is stable, ensuring correct output without manual intervention.

HISTORY

Developed by John Collins, latexmk emerged to address the repetitive and error-prone nature of manually running multiple LaTeX compilation passes. Its initial public release was around 2004, leveraging Perl to provide a robust, cross-platform solution for LaTeX build automation. Over the years, it has gained widespread adoption due to its intelligent dependency tracking, efficient recompilation logic, and comprehensive support for various TeX engines and tools, becoming a de facto standard for LaTeX project management.

SEE ALSO

pdflatex(1), lualatex(1), xelatex(1), bibtex(1), make(1)

Copied to clipboard