Managing two versions in the same document using conditionals and/or comments

overleafverbatim

I have a document that I need to manage multiple versions of, for multiple languages. So far I've kept a different LaTeX project for each, but it's a bit annoying to have to open, edit and save multiple projects every time. I want to be able to "label" parts of the document, and have only the corresponding labeled parts show in the PDF file depending on which "version" option is selected.

The text is interspersed through the document, so keeping different tex files and including them conditionally does not work. Would also prefer not to change the document class.

The best option I've found was to declare an environment to surround the blocks of text with a comment declaration, like so:

\usepackage{verbatim}
\newenvironment{v1}{}{} % to show
% \newenvironment{v1}{\comment}{\endcomment} % to hide

(...)

\begin{v1}
   (...)
\end{v1}

Well, \comment and \endcomment just straight up don't work, even when directly included in the text I get a fatal error from Overleaf. I also tried importing comment instead of verbatim, with the same results.

\begin{comment} and \end{comment} generally (not always) work when directly included in the text, but also throw a fatal error when used in the environment declaration (as in \newenvironment{v1}{\begin{comment}}{\end{comment}}).

The errors I get vary depending on what exactly I try to comment out. I always get a LaTeX3 Error: Command '\FA' already defined! error stemming from one of the template files (it's not being edited), and complaints about runaway arguments or missing } (which I've checked and don't seem to actually exist).

What would be the correct way to utilize \begin{comment} and \end{comment} in the environment declaration? Is there another way to achieve a similar end?

Best Answer

Here's a way to combine parts as you need them using package codesection.

The basic idea is:

  • defining flags as you need them
  • using environments which are flag-sensitive.

Flags: Here I defined E and G for English or German text parts. You are free to use any number of such flags with names you like. To switch versions, set these flags, and compile afterwords. With two flags you have 4 cases, as mentioned in the comment line. Or use \SetCodeSection, see the manual.

% ~~~ define as you need it, e.g. Engl+Germn ~~~
%     tf->E, ft->G, tt->both, ff->none
\DefineCodeSection[true]{E}
\DefineCodeSection[true]{G}

Parts/versions: You use the special environment provided by said package. All you need to do is to enter your flags name. Inside use regular LaTeX commands.

  % ~~~ for the part in English ~~~~
  \BeginCodeSection{E}

  \EndCodeSection{E}

For simplicity I set both flags to true as an example. You see:

  • two languages appear in the order given by the code
  • the section numer increments, as usual, because it now compiles two sections
  • if setting the flags mutually exclusive AND having all content in parallel for both languages, the section numbering will be fine in each language.

Result: result

\documentclass[10pt]{article}
\usepackage[english,ngerman]{babel}% language support
\usepackage{codesection}

% ~~~ define as you need it, e.g. Engl+Germn ~~~
%     tf->E, ft->G, tt->both, ff->none
\DefineCodeSection[true]{E}
\DefineCodeSection[true]{G}

\begin{document}
  % ~~~ some text in English ~~~~~~~
  \BeginCodeSection{E}
    \section{Down the Rabbit-Hole}
    
    Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversations?” 
  \EndCodeSection{E}

  % ~~~ some text in German ~~
  \BeginCodeSection{G}
    \section{Das weiße Kaninchen}
    
    Ich könnte ja Gänseblümchen pflücken und daraus eine Kette flechten, dachte Alice gerade schläfrig bei sich, als sie plötzlich ein Weißes Kaninchen mit rosarot funkelnden Augen über die Wiese nahe am Fluss kommen sah. 
  \EndCodeSection{G}
\end{document}

P.S.: Whlie this setting

% ~~~ define as you need it, e.g. Engl+Germn 
%     tf->E, ft->G, tt->both, ff->none
\DefineCodeSection[true]{E}
\DefineCodeSection[false]{G}

creates just this from the same code:

english