1

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.

2

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}

enter image description here

  • 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
  • 1
    @Tobi You can't have verbatim environments (including lstlisting) in the argument to another command. – egreg Dec 30 '19 at 21:21
0

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.

0

Why not simply

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

and comment out the version you don't want?

  • You need to do something about spaces surrounding \secondVersion's argument in case of not printing the text. Therefore - as also proposed by egreg: \newcommand{\secondVersion}[1]{#1} \newcommand{\secondVersion}[1]{\@bsphack\@esphack} – Ulrich Diez Nov 1 '19 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.