Skip to content

Instantly share code, notes, and snippets.

# adamjgnoel/latex_multi_version_example.tex

Last active Oct 30, 2016
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. Learn more about bidirectional 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}
to join this conversation on GitHub. Already have an account? Sign in to comment