# How can I automate the workflow for producing multiple versions of a document? [duplicate]

Say you have two versions of the same LaTeX document.

For instance, you have one version of your lecture slides for actually delivering a lecture: as you're really not a big fan of the "wall of text", this version would be parsimonious in terms of text content, allowing the audience to listen to what you're talking about instead of reading the slides, and giving graphical content centre stage.

Another version of those slides, made available to the audience after the lecture, for self-study, would be more self-contained and include a summary of what you said during that lecture along with the graphical content mentioned above.

Consider that you can toggle between the two versions by either changing a class option or by changing the value of some dedicated counter in your input file; something relatively simple to do "by hand".

To produce two different versions of the same document, my current workflow is

1. "manually" compile the first version,
2. change the filename of the output produced (in order to prevent overwriting in step 4)
3. make appropriate changes in my input file to switch to the second version,
4. "manually" compile the second version.

However, this workflow is tedious and error-prone, especially when the number of different versions increases. I would like to automate it as much as possible. Is there a way to batch compile all the versions of my document, each getting a different output filename?

I guess that would involve modifying the input file at the command line (I'm on Mac OS X), but I don't know how to do that.

Do you have an automated workflow for compiling multiple versions of the same document? How do you do it?

Edit: I figured it out using Martin Scharrer's post and Neil Olver's post on Passing parameters to a document, of which my question is a duplicate.

• @Caramdir Thanks for the link; I didn't know of it. I'm happy enough that my question gets closed as duplicate of yours. – jub0bs Oct 19 '13 at 14:32
• You can use Sweave and then call the R file with Python through Ubuntu terminal, passing commands through a subprocess.Popen function through Python. – user2763361 Oct 19 '13 at 14:56
• @user2763361 What R file? I'm not using any R file... – jub0bs Oct 19 '13 at 15:04
• @Jubobs I understand this is not how you do it now. What I was trying to say is that you can use the R language to automate the entire process. Set up a Sweave document then toggle the generate of the LaTeX document by simply twiddling a few parameters in your Sweave script (in fact completely ignore Python and Ubuntu for now. Just focus on Sweave and R. – user2763361 Oct 19 '13 at 15:23
• @user2763361 Interesting. Could you please elaborate in a full-blown answer? – jub0bs Oct 19 '13 at 15:24

Consider that you can toggle between the two versions by eiher changing a class option or by changing the value of some dedicated counter in your input file; something relatively simple to do "by hand".

Do you have an automated workflow for compiling multiple versions of the same document? How do you do it?

Prepare three files:

• a contents.tex files containing the bulk of your document, everything that lies between \begin{document} and \end{document}.

• a lecture.tex containing the preamble you use to produce your version adapted for lectures and ending with a snippet reading contents.tex.

• a study.tex containing the preamble you use to produce your version adapted for study and ending with a snippet reading contents.tex.

The snippet reading contents.tex is

\begin{document}
\input{contents}
\end{document}


You can then easily prepare the two versions of your document by processing lecture.tex or study.tex while you only edit contents.tex.

If there is a large preamble piece shared between lecture.tex and study.tex you also should consider moving it to a separate file and \input it in the respective file preambles.

Note: If you are interested in automating the file processing itself and willing to use shell methods, you can learn bsdmake(1). I wrote macros for bsdmake(1) that you can use to automate file processing and that could interest you. They are free software and are usable on many modern Unix-like operating systems. Ports or packagers exists for Mac OS X MacPorts, FreeBSD and Debian. See Producing LaTeX documents.

You might be able to use and adapt the following that simulates how to produce 2 PDF outputs based on 2 conditions given at the compile time. The code tells the details.

% compile with pdflatex -shell-escape

\documentclass[preview,border=12pt]{standalone}
\usepackage{filecontents}

\begin{filecontents*}{template.tex}
\documentclass[preview,border=12pt]{standalone}
\newif\ifsecret
\begin{document}
common 1

\ifsecret
I have a top secret message here.
\fi

common 2
\end{document}
\end{filecontents*}

\usepackage{graphicx}

\begin{document}
\immediate\write18{\unexpanded{pdflatex -jobname=secret-included "\AtBeginDocument{\secrettrue} \input{template}"}}
\immediate\write18{\unexpanded{pdflatex -jobname=secret-excluded "\AtBeginDocument{\secretfalse} \input{template}"}}
Done, secret-included.pdf and secret-excluded.pdf have been generated!

\fbox{\includegraphics[width=.5\linewidth]{secret-included}}%
\fbox{\includegraphics[width=.5\linewidth]{secret-excluded}}
\end{document}


## Related

Class beamer in addition to presentation mode for the slides has two modes that may be what you are looking for

• handout mode for handouts looking like the slides plus notes (IIRC)
• article mode for handouts which are rather like a paper

You can mix these modes in one document, and then produce the different versions of the talk.

See "Creating Supporting Material" in the beamer user guide.

However, I've never used these supporting material modes although I frequently deal with a somewhat similar situation:

• There's a long report containing lots of internal information
• A paper for publication
• presentation slides, and possibly
• a poster as well.

The difference between my situation and yours may be:

• if your delivering a lecture translates to teaching a course, then the beamer concept may in fact be what you are looking for.
• the lectures I deliver usually are talks given in a research project context (even if they are teaching lectures: "Introduction to Bilinear Models"). This means that my lecture slides are a document that is not changed any more after the lecture is given. I may go back and copy some slides for another talk later, or derive a new talk from other talks given previously, but my lectures are not an evolving series of lectures on exactly the same topic (as in teaching $\LaTeX$ 101 every year).

My experience is that while report, paper, presentation slides and poster seem to be almost the same, they are still too different to treat them as versions of one document. Usually not even report and paper are similar enough to be treated as one document.

I work a lot with Sweave/knitr which are techniques for report generation in data analysis where results are automatically included in $\LaTeX$ documents.
In that case the difference between long internal and short public version is that the internal version shows all code in the pdf. That is easily toggled by one parameter during the document compilation process.

For everything else, I try to put the common part (which is usually data analysis code including code to produce the graphics) into a common file which I can input into the different documents. Besides that, a presentation does differ so much from the long text, that they are completely different documents for me. And, as I explained above, while the project evolves, each of the documents (maybe with the exception of the report) will be "final" at some point and not be changed thereafter.

A completely different type of two versions of the same paper that I routinely use are manuscripts in the format for journal submission and in a readable format.

These two versions usually differ only in general formatting considerations:

• one column vs. two column
• tables and figures in the text rather than at the end
• line spacing
• list of figures and list of tables for the submitted version
• the final manuscript version (e.g. for download on project pages or arxiv) gets a box stating that
• this document is available at [project page/arxiv/...] and
• isn't changed (or, that a few typos were corrected) compared to the official paper,
• which was published as [publication reference] and link, and
• possibly the sentence the journal asks for in author's manuscripts.

I use the class option final for this:

\documentclass[final]{article}
\makeatletter
\newif\iffinal
\@ifclasswith{article}{final}{
\typeout{FINAL}\finaltrue}{
\typeout{DRAFT}\finalfalse}
\makeatother

\begin{document}
\iffinal
\textbf{\fbox{This is the author's manuscript.}}
\fi

blabla
\end{document}


I use the following method for including or not sections of my CV. Each section is in a separate .tex file, and I have a master file where I load etoolbox.sty with \usepackage{etoolbox} and have for each section

\newtoggle{<section>}
\settoggle{<section>}{true}


in the preamble.

In the body I have

\iftoggle{<section>}{\input{section.tex}}{}


Changing the toggle to false will result in the section not being included.

• Thanks, but that doesn't answer my problem. What I want is a way to produce different versions "externally", without having to modify my input file. – jub0bs Oct 19 '13 at 14:36
• This would require \usepackage{etoolbox}. – Andrew Swann Oct 19 '13 at 15:53