UIT
Catalogue Events Trade/Press College/University Review-Copies Contact News FAQ Jobs About

LatexConditionals

Sometimes we want to produce two different versions of a book from the same source files. We tried (La)TeX-specific approaches but abandoned these because it was too hard to make sure that the if/else mechanism didn't interfere with the LaTeX code that was to be conditionally included or excluded.

An obvious solution is to use a preprocessor like cpp, so we can use #if/#else/#endif to conditionalize chunks of LaTeX. However, cpp has too many capabilities -- it interprets certain C-language contructs as macros, and as these can easily occur in LaTeX code, cpp isn't suitable for our purposes. Nor is m4.

However, there is a very limited preprocessor called unifdef. As its manpage states: "The unifdef utility selectively processes conditional cpp(1) directives. It removes from a file both the directives and any additional text that'' they specify should be removed, while otherwise leaving the file alone."

unifdef is perfect for our purposes.

Here's a simple example:

  • The source file would normally be simple.tex say, but instead the file we work with, and edit, is simple.texpp (where 'pp' stands for 'preprocessor').
  • Use the unifdef program to handle the conditionals. unifdef is very basic which is great -- it leaves just about everything completely untouched apart from its if/else lines. Here's an example:
  	\documentclass[12pt]{article}
  	\begin{document}

  	First

  	#ifdef MME

  	Second
    	Third

  	#else

  	Fourth
    	Fifth

  	#endif

  	Sixth

  	\end{document}

If we are producing the "mass market edition" (MME) of the book we get one output tex file; on the other hand if MME is undefined we get a different variant -- the "normal" version of the book. (The definition or undefinition of MME is in a settings file that we pass to unifdef.)

We send the output of unifdef to simple.tex in this example, and then process simple.tex as normal.

Print - Search - Page last modified on January 17, 2011, at 13:00