# CMakeUserUseLATEX

## Description

Compiling LaTeX files into readable documents is actually a very involved process. Although CMake comes with FindLATEX.cmake, it does nothing for you other than find the commands associated with LaTeX. I like using CMake to build my LaTeX documents, but creating targets to do it is actually a pain. Thus, I've compiled a bunch of macros that help me create targets in CMake into a file I call "UseLATEX.cmake". Here are some of the things UseLATEX.cmake handles:

• Runs LaTeX multiple times to resolve links.
• Can run bibtex and makeindex to make bibliographies and/or indexes.
• Optionally runs configure on your latex files to replace @VARIABLE@ with the equivalent CMake variable.
• Automatically finds png, jpeg, eps, and pdf files and converts them to formats latex and pdflatex understand.

## Usage

Using UseLATEX.cmake is easy. For a basic LaTeX file, simply include the file in your CMakeLists.txt and use the ADD_LATEX_DOCUMENT command to make targets to build your document. For an example document in the file MyDoc.tex, you could establish a build with the following simple CMakeLists.txt.

PROJECT(MyDoc NONE)

INCLUDE(UseLATEX.cmake)

dvi
Creates MyDoc.dvi.
pdf
Creates MyDoc.pdf using pdflatex. Requires the PDFLATEX_COMPILER CMake variable to be set.
ps
Creates MyDoc.ps. Requires the DVIPS_CONVERTER CMake variable to be set.
safepdf
Creates MyDoc.pdf from MyDoc.ps using ps2pdf. Many publishers prefer pdfs are created this way. Requires the PS2PDF_CONVERTER CMake variable to be set.
html
Creates html pages. Requires the LATEX2HTML_CONVERTER CMake variable to be set.

One caveat about using UseLATEX.cmake is that you are required to do an out-of-source build. That is, CMake must be run in a directory other than the source directory. This is necessary as latex is very picky about file locations, and the relative locations of some generated or copied files can only be maintained if everything is copied to a separate directory structure.

### Using a Bibliography

For any technical document, you will probably want to maintain a BibTeX database of papers you are referencing in the paper. You can incorporate your .bib files by adding them after the BIBFILES argument to the ADD_LATEX_DOCUMENT command.

This will automatically add targets to build your bib file and link it into your document. To use the BibTeX file in your LaTeX file, just do as you normally would with \cite commands and bibliography commands:

\bibliographystyle{plain}
\bibliography{ParallelVolumeRenderingInParaView}

You can list as many bibliography files as you like.

### Incorporating Images

To be honest, incorporating images into LaTeX documents can be a real pain. This is mostly because the format of the images needs to depend on the version of latex you are running (latex vs. pdflatex). With these CMake macros, you only need to convert your raster graphics to png or jpeg format and your vector graphics to eps or pdf format. Place them all in a common directory (e.g. images) and then use the IMAGE_DIRS option to the ADD_LATEX_DOCUMENT macro to point to them. UseLATEX.cmake will take care of the rest.

If you want to break up your image files in several different directories, you can do that, too. Simply provide multiple directories after the IMAGE_DIRS command.

ADD_LATEX_DOCUMENT(MyDoc.tex BIBFILES MyDoc.bib IMAGE_DIRS icons figures)

Alternatively, you could list all of your image files separatly with the IMAGES option.

SET(MyDocImages
logo.eps
icons/next.png
icons/previous.png
figures/flowchart.eps
figures/team.jpeg
)

Both the IMAGE_DIRS and IMAGES can be used together. The combined set of image files will be processed. If you wish to provide a separate eps file and pdf or png file, that is OK, too. UseLATEX.cmake will handle that by copying over the correct file instead of converting.

Once you establish the images directory, CMake will automatically find all png and eps files in it and add makefile targets to use ImageMagick's convert to convert the file times to those appropriate for the build. If you do not have ImageMagick, you can get it for free from http://www.imagemagick.org. CMake will also give you a LATEX_SMALL_IMAGES option that, when on, will downsample raster images. This can help speed up building and viewing documents. It will also make the output image sizes smaller.

One more note about vector graphics. Encapsulated postscript (eps) files have a bounding box that is often lost when converting to pdf types. When using eps files, it is best to search for a line starting with %%BoundingBox: such as

%%BoundingBox: 58 77 734 536

and then copy these numbers to the bb option of the LaTeX \includegraphics command:

\includegraphics[width=\linewidth,bb=58 77 734 536]

### Making an Index

You can make an index in a LaTeX document by using the makeidx package. However, this package requires you to run the makeindex command. Simply add the USE_INDEX option anywhere in the ADD_LATEX_DOCUMENT arguments, and makeidx will automatically be added to the build.

ADD_LATEX_DOCUMENT(MyDoc.tex BIBFILES MyDoc.bib IMAGE_DIRS images USE_INDEX)

### Multipart LaTeX Files

Often, it is convenient to split a LaTeX document into multiple files and use the LaTeX \input or \include command to put them back together. To do this, all the files have to be together. UseLATEX.cmake can take care of that, too. Simply add the INPUTS argument to ADD_LATEX_DOCUMENT to copy these files along with the target tex file. Build dependencies to these files is also established.

INPUTS Chapter1.tex Chapter2.tex Chapter3.tex Chapter4.tex
BIBFILES MyDoc.bib
IMAGE_DIRS images
USE_INDEX
)

### Configuring LaTeX Files

Sometimes it is convenient to control the build options of your tex file with CMake variables. You can achieve this by using the CONFIGURE argument to ADD_LATEX_DOCUMENT.

INPUTS Chapter1.tex Chapter2.tex Chapter3.tex Chapter4.tex
CONFIGURE MyDoc.tex
BIBFILES MyDoc.bib
USE_INDEX
)

In the above example, in addition to copying MyDoc.tex to the binary directory, UseLATEX.cmake will configure MyDoc.tex. That is, it will find all occurrences of @VARIABLE@ and replace that string with the current CMake variable VARIABLE.

With the CONFIGURE argument you can list the target tex file (as shown above) as well as any other tex file listed in the INPUTS argument.