8

I am writing a document for a software which has two versions. Most of the content in the document is the same except few specific details. Right now, I am using the conditionals to build the document for a specific file, like so,

\usepackage{etoolbox}
..
\newtoggle{version5}    
\togglefalse{version5}

\iftoggle{version5} {
    \title{Version 5 document}
} {
    \title{Version 6 document}
}

\begin{document}
    \chapter{Chapter 1}
        \iftoggle{version5} {
            \section{Version 5 section}
        } {
            \section{Version 6 section}
        }
\end{document}

This works prefectly, but the problem is everytime I need either of the document, I need to toggle the variables each time. Is there a way to build both the document in a single run?

1

3 Answers 3

8

This uses the shell-escape features (\immediate\write18) and writes \togglefalse{version5} or \toggletrue{version5} to a file called foo.cfg

This foo.cfg is read by foo.tex.

The compilation is done in a subshell, the rename can be done with the --jobname= option of the pdflatex binary (or xelatex etc.

\documentclass{article}


\newwrite\foocfg% Get a file handle

\newcommand{\prepareconfigfile}[2]{%
\immediate\openout\foocfg=#1 % open the file in #1 -- Be carefull here!
\immediate\write\foocfg{%
  \expandafter\string\csname toggle#2\endcsname{version5}%
  }%
\immediate\closeout\foocfg% Close the file
}



\begin{document}
\prepareconfigfile{foo.cfg}{true}
\immediate\write18{pdflatex --jobname=footrue.pdf foo}

\prepareconfigfile{foo.cfg}{false}
\immediate\write18{pdflatex --jobname=foofalse.pdf foo}
\end{document}

Compilation source (foo.tex)

\documentclass{book}

\usepackage{etoolbox}

\newtoggle{version5}    
\togglefalse{version5}

\InputIfFileExists{foo.cfg}{}{}

\iftoggle{version5}{%
  \title{Version 5 document}
}{%
  \title{Version 6 document}
}

\begin{document}
\maketitle
    \chapter{Chapter 1}
    \iftoggle{version5}{%
      \section{Version 5 section}
    }{%
      \section{Version 6 section}
    }%
\end{document}
2
  • 1
    I found that we can rename the output of pdfLatex without using the mv command (Gnu-Utils). We could generate a name that we want by using the job-name parameter with the pdflatex command, like so, pdflatex -job-name=my_file_name foo.
    – Some guy
    Aug 7, 2015 at 16:28
  • @Rakesh: Oh yes, I had that in mind too, but on LInux it's --jobname=... and I failed, but I did use the wrong order! Thanks, I will update!
    – user31729
    Aug 7, 2015 at 16:30
7

As mentioned in my comment, the question Passing parameters to a document allready points in a right direction.

File Master.tex

\documentclass{scrbook}
\usepackage{etoolbox}
\usepackage{xstring}

\newtoggle{version5}
\IfStrEq{\jobname}{\detokenize{Version5}}{\toggletrue{version5}}{\togglefalse{version5}}

\iftoggle{version5} {
    \title{Version 5 document}
} {
    \title{Version 6 document}
}

\begin{document}
    \chapter{Chapter 1}
        \iftoggle{version5} {
            \section{Version 5 section}
        } {
            \section{Version 6 section}
        }
\jobname
\end{document}

After that you can add a (or two) symlink(s). One named Version5.tex and the other named Version6.tex. This works even under Windows (but needs the cmd-line or a Batch-File and administrator rights; I can provide a batch-file if needed).

After that, you can compile Version5.tex and Version6.tex and get the following output: Version5 Version6

5

Here is another solution based on the file name. Create a file Master.tex and two symbolic links to it: Version5.tex and Version6.tex. When compiling one of these, a switch versionV is set to true if Version5 is being compiled and set to false otherwise.

I have used the more low level \newif to create a switch and \ifversionV to branch based on its value. No extra packages are necessary.

\documentclass{article}

\newif\ifversionV
\makeatletter
\edef\@tempa{\jobname}
\def\@tempb{Version5}
\@onelevel@sanitize\@tempb
\ifx\@tempa\@tempb \versionVtrue\else\versionVfalse\fi
\makeatother

\ifversionV
    \title{Version 5}
\else
    \title{Version 6}
\fi

\author{Franz K.}

\begin{document}
\maketitle

One morning, as Gregor Samsa was waking up from anxious dreams, he
discovered that in his bed he had been changed into a monstrous verminous
bug. He lay on his armour-hard back and saw, as he lifted his head up a
little, his brown, arched abdomen divided up into rigid bow-like sections.

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .