# Two versions from same tex file

I'm writing a LaTeX text, but I need two versions of the same text, one normal and one with some more comments inside the text. How can I write both the versions using the same TEX file?

To be more clear I would like to obtain for example these two versions:

...first simple version...

...first simple version with some more comments is now second version...

And I would like for both of them the code to be something like:

...first simple version \secondversion{with some more comments is now second version}....


So that if I change a flag at the beginning of my TEX file I can obtain either the first or the second version.

• Where are the comments going to appear? What kind of text? Long, short? – Alenanno Aug 30 '16 at 11:35
• IMO, the key is to use \jobname. Here is what i did to have two different PDFs from the same source when debugging some inconsistent behavior: tex.stackexchange.com/q/325102/37291. – Alexey Aug 30 '16 at 11:55
• I'd use conditionals and \jobname (as I did here): tex.stackexchange.com/a/71559/8528 – jon Aug 30 '16 at 14:54
• Please note that unless your supply the -jobname argument to pdflatex, your second version will overwrite the first version, unless you move the first version manually before compiling the second one. – Alexey Sep 3 '16 at 8:51

Here's a solution using a flag. You can set the flag in the preamble, change it on the fly in the document if you wish.

With a little more work you could set the flag on the compile command line without having to edit the file.

\documentclass{article}

\usepackage{etoolbox}
\newtoggle{vtwo}

% set status globally in the preamble
\togglefalse{vtwo}

\newcommand{\secondversion}[1]{%
\iftoggle{vtwo}{%
#1
}
% else
{}
}

\begin{document}

...first simple version \secondversion{with some more comments is now
second version}....

% change status locally if you wish
\toggletrue{vtwo}

...first simple version \secondversion{with some more comments is now
second version}....

\end{document}


Here is my solution that relies on inspecting \jobname.

The source file:

% source.tex

\documentclass{article}

\usepackage{etoolbox}  % for \ifstrequal

%% NOTE: i do not know how this works (how \expandafter works),
%%   but it works
\newcommand{\secondVsFirstVersion}[2]{%
\expandafter\ifstrequal\expandafter{\jobname}{second-version}{#1}{#2}}

\newcommand{\mainversion}[1]{\secondVsFirstVersion{}{#1}}
\newcommand{\secondversion}[1]{\secondVsFirstVersion{#1}{}}

\begin{document}
...first simple version \secondversion{with some more comments is now second version}....

Moreover, \mainversion{this will appear only in the main version,}
\secondVsFirstVersion{this will appear only in the second version}{
and this only in the first again}.
\end{document}


To obtain the main version of the output, run

pdflatex -jobname='main-version' source.tex


(or just pdflatex source.tex). To obtain the second, run

pdflatex -jobname='second-version' source.tex


Alternatively, create two hard links to this file called main-version.tex and second-version.tex and compile them as usual, without -jobname parameter.

IMO even better is to use a makefile with latexmk:

# Makefile

.DELETE_ON_ERROR:

.PHONY: all
all: v1 v2

.PHONY: v1
v1: main-version.pdf

.PHONY: v2
v2: second-version.pdf

main-version.pdf: source.tex
latexmk -pdf -jobname='main-version' '$<' second-version.pdf: source.tex latexmk -pdf -jobname='second-version' '$<'

.PHONY: mostlyclean
mostlyclean:
rm -f *.log

.PHONY: clean
clean: mostlyclean
rm -f *.aux *.out *.toc *.fdb_latexmk *.fls

.PHONY: distclean
distclean: clean
rm -f main-version.pdf second-version.pdf


Then to get both versions at once simply run make in the source directory.

There are various packages that do precisely this. I use versions. In its documentation one can find a comparison to other packages. Below there is a minimum usage example.

\documentclass{article}
\usepackage{versions}

\includeversion{abridged}
\excludeversion{unabridged}
\excludeversion{redact}
\begin{document}
Text for the \begin{abridged}short\end{abridged}\begin{unabridged}long and really longwinded, opaque and boring\end{unabridged} version of the paper. Punctuation works correctly\begin{unabridged}because sphack is used\end{unabridged}.
\begin{comment} This is deleted by default. \end{comment}
\begin{redact}This information was withdrawn\end{redact}
\end{document}


One has to pay attention not to leave blank spaces or add newline, for example, before the \begin{abridged} to get the proper spacing between word.