Getting Started with CMake for LaTeX


CMake (Cross Make) is a cross-platform make generator. This software tool is used for managing the build process, commonly for projects using programming languages such as C/C++. CMake is a very flexible building tool. However, It does however not have support for the LaTeX build process natively. But, it does support both custom targets (ADD_CUSTOM_TARGET) and modules. By combining these features we can easily create a LaTex build process with a proper build sequence required for building many LaTeX documents.

First, we should take a look at the modules. These are just simply CMake code just like you would find in the CMakeLists.txt file. But, they are used in combination with other CMake files, allowing for the reuse of code. CMake supports multiple official modules. In our LaTex project, we need a module that can find our LaTex executables on our system. We could write our own. But fortunately, the FindLatex module already exists.

The FindLatex Module

The FindLatex module that we will be utilizing is part of the CMake’s official supported modules. By looking at the FindLatex reference site we can see it will provide a set of variables with the path for our executables, see the following variables copied from the website.

LATEX_FOUND: whether found Latex and requested components
LATEX_<component>_FOUND: whether found <component> 
LATEX_COMPILER: path to the LaTeX compiler
PDFLATEX_COMPILER: path to the PdfLaTeX compiler
XELATEX_COMPILER: path to the XeLaTeX compiler 
LUALATEX_COMPILER: path to the LuaLaTeX compiler
BIBTEX_COMPILER: path to the BibTeX compiler 
BIBER_COMPILER: path to the Biber compiler 
MAKEINDEX_COMPILER: path to the MakeIndex compiler 
XINDY_COMPILER: path to the xindy compiler 
DVIPS_CONVERTER: path to the DVIPS converter 
DVIPDF_CONVERTER: path to the DVIPDF converter 
PS2PDF_CONVERTER: path to the PS2PDF converter 
PDFTOPS_CONVERTER: path to the pdftops converter 
LATEX2HTML_CONVERTER: path to the LaTeX2Html converter 
HTLATEX_COMPILER: path to the htlatex compiler

We can see that there is a handful of useful Latex executables that we can use for our build process. However, in many LaTex projects, the PDFLATEX_COMPILER, which will point to the pdflatex executable that will create a PDF from the source is enough. Fortunately, this executable is enough for a simple LaTex project. See the following CMake code example.

Of course, PDFLATEX_COMPILER can be replaced by any of the other compilers.

Using Citation and Index Table Example

There are cases where using the compiler only is enough. For instance, scientific texts and education books/texts that commonly use citations and index tables for keyword lookup tables. Fortunately, the module provides multiple other compiler executables that will solve our problem. In this case, we are interested in BIBTEX_COMPILER and MAKEINDEX_COMPILER respectively. Firstly, the BIBTEX_COMPILER is used for generating the files for citations and the reference list. Secondly, MAKEINDEX_COMPILER, will be used to create the files required for the index tables.

The source code will look similar to the previous example, with the exception of additional custom targets and dependencies. These dependencies are important since they enforce that the build process is executing in the correct order. Because the latex compiler can start complaining of missing data for either the citation or the index table. See the following source code example.