# Add text which can be turned on or off during compilation

I have written a book in LaTeX which I am reviewing at the moment. However, in the book, I want to insert more details for e.g. a printed version. Thus, I am searching for a possible option to include text that is compiled and shown in the document based on a variable (such as a debug flag). I want to give an example. The first version should have the following text:

The keywords has to be added to the \textit{fvSolution} file,
otherwise, the solver might indicate an error.


For the second version I would like to extend the text:

The keywords has to be added to the \textit{fvSolution} file,
otherwise, the solver might indicate an error. \secondVersion{However, default
values are used if nothing is specified}


I also might use it for coloring text in the second version such as:

The \secondVersion{\{\red}keywords\secondVersion{\}} has to be
added to the \textit{fvSolution} file, otherwise, the solver might
indicate an error.


During compiling the text inside \secondVersion{} should be compiled if a flag is set or not. I guess something like that might be possible. Any hints are appreaciated.

Thanks in advance.

## 3 Answers

You can define two commands:

\documentclass{article}
\usepackage{xcolor}

\makeatletter
\newif\ifsecondversion

% text that should be omitted in the first version
\newcommand{\sv}[1]{%
\ifsecondversion
#1%
\else
\@bsphack\expandafter\@esphack
\fi
}
% text that should be colored in the second version
\newcommand{\svx}[2][red]{%
\ifsecondversion
\textcolor{#1}{#2}%
\else
#2%
\fi
}

\begin{document}

\secondversionfalse

The \svx{keywords} has to be added to the \textit{fvSolution} file,
otherwise, the solver might indicate an error. \sv{However, default
values are used if nothing is specified.} Blah, \svx[blue]{blah}.

\secondversiontrue

The \svx{keywords} has to be added to the \textit{fvSolution} file,
otherwise, the solver might indicate an error. \sv{However, default
values are used if nothing is specified.} Blah, \svx[blue]{blah}.

\end{document}


• That is a much nicer way than I added yesterday. Thank you for the answer. – Tobi Nov 1 '19 at 12:27
• Just one question, if one uses, e.g., begin{lstlisting} inside the \sv{} it is not working. There is a message: ! Extra \else. – Tobi Dec 29 '19 at 21:04
• @Tobi You can't have verbatim environments (including lstlisting) in the argument to another command. – egreg Dec 30 '19 at 21:21

Thanks to @Phelype Oleinik for the hints in the comment section. I used the following approach https://tex.stackexchange.com/a/33579/116084 which lead to:

\newif\ifsecondVersion
\secondVersiontrue

\begin{document}
The \ifsecondVersion {\red \fi keywords \ifsecondVersion }\fi has to be
added to the \textit{fvSolution} file, otherwise, the solver might
indicate an error.
\end{document}


Works perfect.

Why not simply

...
\newcommand{\secondVersion}[1]{#1}
\newcommand{\secondVersion}[1]{}
...


and comment out the version you don't want?
