# Multiple LaTeX Document Versions Using Switches

LaTeX is great for typesetting equations and figures to look just the way you want. There are plenty of options for alignment and spacing. However, what if you regularly make major changes and need to keep multiple latex document versions? In this post, I’ll talk about how to do this using switches.

Why would you need multiple versions? For example, I often use the IEEEtran class for writing papers. If you are writing a journal manuscript, then usually there are two versions. The manuscript is reviewed in single-column format but published as double-column. Some IEEE journals even require that you submit both versions at the same time. Personally, I much prefer reading the double column version, so I regularly switch between single and double column while writing. This creates issues for proper equation alignment and sometimes figure placement (especially if the single column version is supposed to keep the figures at the end).

## In the Beginning …

I had a couple of initial solutions to this problem:

1. Assume double column everywhere. If you make all your equations fit in double column, then they will also fit in single column. Two potential problems: 1) paper length is often defined for the single column version, so you might be wasting space if you have a lot of math. 2) This method doesn’t help if the figure placement rules are different.
2. Write two versions and comment out the one you’re not using. If you don’t have many differences between the two versions (e.g., only one really long equation), and you don’t need to change very often, then this might work for you. But this method gets frustrating if you often go back and forth.

## Getting LaTeX Document Versions with Switches

I saved myself a lot of effort once I realized that LaTeX lets you create if/else statements with custom switches within your document (more generally, this is a TeX feature). Using a custom switch (or self defined conditional) makes it easy to maintain two (or more) LaTeX document versions. The general strategy is this:

1. Use newif to create the switch. Syntax is “newififMY_VAR”. By default, it will be false.
2. Add the line “MY_VARtrue” to make the switch true.
3. Use the syntax “ifMY_VAR … else … fi” wherever you need to differentiate between the versions.

Here is a simple tex-file showing variations for single and double column versions of the same document (the image file in this example is here):

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

 % This is a working example to demonstrate use of switches to maintain multiple % versions of a single document % % Uses the IEEEtran class: https://www.ctan.org/pkg/ieeetran % % Created 2016-10-30 by Adam Noel % Define the switch here using "newif" and start its name with "if" % Here, NAME_OF_SWITCH == "OneCol" \newif\ifOneCol % By default, a switch is "false". Use \NAME_OF_SWITCHtrue to set to true % Uncomment the line below to set the switch to "true" \OneColtrue \ifOneCol % One column version \documentclass[onecolumn,11pt]{IEEEtran} \else % Two column version \documentclass[twocolumn,10pt]{IEEEtran} \fi % end if \usepackage[pdftex]{graphicx} \usepackage[cmex10]{amsmath} \usepackage{lipsum} % Dummy text % correct bad hyphenation here \hyphenation{op-tical net-works semi-conduc-tor} \begin{document} % \title{A Sample Document with \\Multiple Versions in One \LaTeX~File} \author{Adam Noel} % Here's a command for a figure. We use a command % because we want to set different options for placement and width, % depending on whether we are compiling the 1- or 2-column version \newcommand{\figOne}[2]{ \begin{figure}[#1] \centering \includegraphics[width=#2\linewidth]{../matlab/matlab_plot.jpg} \caption{Here's a figure. We use a command to define it, which lets us change its location in the two versions. We set its width to a fraction of the line width, so that it's size is comparable in both the single and double column versions. In single column, we place the figure where it is defined. In double column, we anchor it to the top of the page.} \label{my_fig} \end{figure} } \maketitle \section{Introduction} This is a simple document showing how to use a switch to create multiple versions of a document with a single \LaTeX file. In this example, there is a switch \emph{OneCol} in the source file. Comment out the line “\textbackslash OneColTrue'' to compile a double column version. Uncomment that line for single column. Throughout the source file, you will find if statements that show the differences between the two versions. % Switch for some text. Generally not needed for formatting a document whose text doesn't change. \ifOneCol \textbf{This version is single column.} \else \textbf{This version is double column.} \fi \section{A Mathematical Example} Here's an equation that is too long to fit on one line in the double column version but will fit on one line in the single column version. The source file writes out these two versions separately, so if you need to make changes don't forget to change both! \ifOneCol \begin{equation} \label{my_eqn} X = A + B + C + D + E + F + G + H + I + J + K + L + M + N + O + P. \end{equation} \else \begin{align} \label{my_eqn} X = &\; A + B + C + D + E + F + G + H + I \nonumber \\ & + J + K + L + M + N + O + P. \end{align} \fi Eq.~\ref{my_eqn} might give you warnings for repeated label usage, because the same label is written twice in the source file. However, only one label is used at compile-time (depending on the switch value), so there are no referencing issues. \section{Garbage Filler Text to Make Fig.~\ref{my_fig} Anchor Somewhere Meaningful} Skip to Section~\ref{sec_fig}. \lipsum[1-7] \section{A Figure Example} \label{sec_fig} Let's say we want to place a figure. We use a command to place Fig.~\ref{my_fig} so that we can modify the figure in one location but vary the placements options in the two versions. In the double column version, we anchor the figure to the top of the page with a width equal to \textbackslash linewidth. In the single column version, we place the figure after this paragraph with a width that is 55\% of \textbackslash linewidth. \ifOneCol \figOne{h}{0.4} % Anchor 1-column version here \else \figOne{!t}{1} % Anchor 2-column version to top of page \fi % that's all folks \end{document}

This document has single and double column versions, and shows examples of using text, an equation, and a figure differently for the two versions. Here are the compiled PDF files:

## 3 thoughts on “Multiple LaTeX Document Versions Using Switches”

Thanks, Adam. This makes life much simpler!

BTW, why do journals ask for single-column format when they only publish the double-column one?

Like

Single column format usually has more spacing between lines and larger margins. This makes it easier to read and mark in comments, which is very helpful for reviewers (especially if they print it!). Double column formats have much less space to fit in annotations because they fit more content on each page. So, single column is preferred for the review stage, but double column format is preferred for publication.

Or at least this is what makes sense to me.

Like