How to use the command 'latexmk' (with examples)

How to use the command 'latexmk' (with examples)

Latexmk is a powerful tool for automatically compiling LaTeX documents. It simplifies the process of turning LaTeX source files into polished, finished documents, typically in formats like PDF or DVI. One of the standout features of latexmk is its ability to automatically determine the number of runs necessary to resolve cross-references, citations, and other dependencies in a document. Furthermore, its auto-update feature is extremely useful for those who need their document to be continually refreshed during editing. Its various clean-up options also help users maintain an uncluttered working directory by removing auxiliary files generated during compilation.

Use case 1: Compile a DVI (Device Independent file) document from every source

Code:

latexmk

Motivation:

Often, when working on multiple LaTeX documents within the same directory, you might want to compile all of them to ensure their completeness and correctness. Running latexmk without any arguments will automatically detect and compile every .tex file in the current directory into DVI format.

Explanation:

  • latexmk: Executing the command without additional arguments triggers latexmk to process all LaTeX source files in the specified or current working directory. It intelligently handles multiple files, making sure references are consistent throughout each document.

Example Output:

Latexmk: applying rule 'latex'...
Latexmk: All targets (document1.dvi document2.dvi) are up-to-date

Use case 2: Compile a DVI document from a specific source file

Code:

latexmk path/to/source.tex

Motivation:

While working on a single document, focusing your compilation efforts on that specific file saves time and computational resources. This is particularly useful when you constantly fine-tune a single document, ensuring it compiles without the need to process unrelated LaTeX files.

Explanation:

  • latexmk: The base command to start the compilation process.
  • path/to/source.tex: Indicates the specific LaTeX source file you want to compile into a DVI document.

Example Output:

Latexmk: applying rule 'latex'...
Latexmk: All targets (source.dvi) are up-to-date

Use case 3: Compile a PDF document

Code:

latexmk -pdf path/to/source.tex

Motivation:

PDF format is more commonly used for sharing and printing documents compared to DVI. The command is essential when you need a polished and universally viewable version of your document, making it ready for publication or peer reviews.

Explanation:

  • latexmk: The command to invoke the compilation process.
  • -pdf: This option tells latexmk to produce a PDF file from the specified source file.
  • path/to/source.tex: Specifies the LaTeX file to be compiled.

Example Output:

Latexmk: applying rule 'pdflatex'...
Latexmk: All targets (source.pdf) are up-to-date

Use case 4: Open the document in a viewer and continuously update it whenever source files change

Code:

latexmk -pvc path/to/source.tex

Motivation:

For writers who make frequent tweaks to their documents, seeing changes reflected in real-time is invaluable. The -pvc option stands for “preview continuously,” allowing you to view the compiled PDF or DVI while editing the source file, automatically updating it on save.

Explanation:

  • latexmk: Initiates the compilation process.
  • -pvc: Enables the preview-continuous mode, maintaining an up-to-date preview of your document.
  • path/to/source.tex: The LaTeX source file to compile.

Example Output:

Latexmk: Previewing...
Latexmk: File changed '/path/to/source.tex', recompiling...

Use case 5: Force the generation of a document even if there are errors

Code:

latexmk -f path/to/source.tex

Motivation:

When a document contains errors that do not immediately impact the generation of a preliminary version (such as unresolved citations), yet you need a compiled version to share or review, the -f option forces the completion of the process.

Explanation:

  • latexmk: Starts the compilation.
  • -f: Short for ‘force,’ allowing latexmk to continue processing despite existing issues.
  • path/to/source.tex: The specific file for processing.

Example Output:

Latexmk: ignoring errors.
Latexmk: applying rule 'pdflatex'...
Latexmk: Errors, but generating document: source.dvi

Use case 6: Clean up temporary TEX files created for a specific TEX file

Code:

latexmk -c path/to/source.tex

Motivation:

LaTeX compilation often generates several auxiliary files like .aux, .log, and .out files. Cleaning these up becomes necessary to maintain an organized environment, particularly when finalizing a document.

Explanation:

  • latexmk: Invokes the command suite.
  • -c: Cleans up intermediate and log files related to the compilation process.
  • path/to/source.tex: The source to which the cleanup should be applied.

Example Output:

Latexmk: Deleting intermediate files...
Latexmk: Done deleting old files

Use case 7: Clean up all temporary TEX files in the current directory

Code:

latexmk -c

Motivation:

Upon finalizing multiple documents, there’s often a need to clean the workspace of generated clutter to simplify managing files and reducing confusion or errors in future edits.

Explanation:

  • latexmk: Initiates the latexmk command.
  • -c: Specifies cleanup of temporary and log files within the current directory context.

Example Output:

Latexmk: Deleting intermediate files...
Latexmk: Done deleting old files

Conclusion:

Latexmk is an incredibly versatile tool within the LaTeX ecosystem, helping users streamline their document production processes. From compiling various file formats to automating updates and cleaning temporary files, latexmk provides a robust suite of functionalities, dramatically enhancing productivity and organization for LaTeX users. Understanding how to leverage these commands effectively can greatly improve your experience with LaTeX document editing and compilation.

Related Posts

How to Use the Command 'cgclassify' (with examples)

How to Use the Command 'cgclassify' (with examples)

The command cgclassify is a tool used in Linux to move running processes to specific control groups (cgroups).

Read More
How to use the command `ppmquant` (with examples)

How to use the command `ppmquant` (with examples)

ppmquant is a command-line tool from the Netpbm suite used for color quantization of images.

Read More
How to use the command 'systemd-machine-id-setup' (with examples)

How to use the command 'systemd-machine-id-setup' (with examples)

The ‘systemd-machine-id-setup’ command is a utility provided by systemd, a widely-used system and service manager for Linux operating systems.

Read More