10

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

6
  • Note that the space isn't generated by the commands it is the space in your document text between the commands, so the simplest is not to add that space. Sep 5, 2013 at 15:10
  • You can end the definitions with \ignorespaces then use \ if you need a space. Sep 5, 2013 at 15:11
  • I haven't put any space between the two commands in that sample text block. I've just settled on the following hack: This is fake\IfCondTrue{ text.}\IfCondTrue{hooplah. } In this way, the commands automatically account for the spaces introduced by the other commands. Note particularly elegant but it's a bit convenient. Sep 5, 2013 at 15:45
  • There is a space (linebreak) between the \IfCondTrue{text} and \IfCondFalse{hooplah} in the example above, do you mean that isn't in your real code? (This is why it is always best to post a complete document so the problem can be reproduced) Sep 5, 2013 at 15:58
  • I don't know if I'm being thick, but I don't know what you're talking about. I don't believe I inserted a line break, nor do I see one in what I posted. Sep 5, 2013 at 18:15

1 Answer 1

8

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.

0

You must log in to answer this question.

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