[Tex/LaTex] conditional content

conditionals

I'm writing a document in LaTeX that I want to compile in two different versions, V1 and V2 (with two different formatting specifications). Sometimes I need to insert a bit of content into V1 that isn't in V2, and vice versa. To specify which version is being compiled, I have a variable called cond, which I set to True to flag the V1 formatting, and False to flag the V2 formatting. Then, the following commands are specified:

% Display the argument if \cond is True
\newcommand{\IfCondTrue}[1]{\expandafter\ifstrequal\expandafter{\cond}{True}{#1}{}}

% Display the argument if \cond is False
\newcommand{\IfCondFalse}[1]{\expandafter\ifstrequal\expandafter{\cond}{True}{}{#1}}

Thus, to insert content into V1 that isn't in V2, I simply insert it into the command \IfCondTrue{...}.

This works exactly as desired except for one issue; when the content that I'm inserting is paragraph text, eg. This is fake \IfCondTrue{text}.. If cond is set to False, then the word text won't display, as desired. Unfortunately, it will be replaced with an empty space. For example, suppose in V1 I want to have This is fake text. and in V2 I want to have This is fake hooplah., then ideally I could write: This is fake \IfCondTrue{text}\IfCondFalse{hooplah}.. Then, when V1 compiles, what I'll see is This is fake text . (with a space between text and ..

How can I modify the above commands so that they won't display an empty space when their content is not displayed?

Thanks

Best Answer

You could \unskip and/or \ignorespaces as part of your condition. However, version control is easier when you combine the possible outputs in a single macro:

enter image description here

\documentclass{article}
% Display the argument if \cond is True/False
\newcommand{\IfCond}[2]{%
  \ifnum\pdfstrcmp{\cond}{True}=0
    \ifnum\pdfstrcmp{}{#1}=0\unskip\else#1\fi%
  \else
    \ifnum\pdfstrcmp{}{#2}=0\unskip\else#2\fi%
  \fi\ignorespaces}

% Display the argument if \cond is False
\newcommand{\IfCondFalse}[1]{%
  \ifnum\pdfstrcmp{\cond}{True}=0 \unskip\else #1\fi\ignorespaces}
\begin{document}
\def\cond{True}% Version control
This is fake \IfCond{text}{hooplah}. \par
This is fake \IfCond{}{hooplah}.

\def\cond{False}% Version control
This is fake \IfCond{text}{hooplah}. \par
This is fake \IfCond{text}{}.
\end{document}

String comparison is done using pdfTeX's \pdfstrcmp.

Related Question