8

I'm new to LaTeX so I'm not quite sure how to proceed with this. I'm trying to have different versions of the same document, depending on which variables are set. What I want is to change the whole document changing one line.

So far, I've been trying something I found in a template, but I haven't been able to get it to work.

\newif\if@thing\@thingfalse
\newcommand*{\displaything}{\@thingtrue}

That way, when I write \displaything, the variable should be set. If someday I don't want the variable, I'd just comment this line. Then, I want to pass this variable to another command, like this:

\newcommand*{\foo}[2]{
  \ifthenelse{#1}
  {
    % do something if set, using #2
  }
  {
    % do something else if not set, using #2 (default)
  }
}

The idea is to have multiple \foo calls with more arguments, and different flag-things, and then use them like this:

\foo{\@thing}{arg}, \foo{@thing2}{arg}

Maybe this isn't even the right way to do it, any thoughts?

5
  • 3
    This is, in principle, correct. However \ifthenelse does not take as its argument a command created with \newif. You have to do something like \newboolean{thing} and the later \ifthenelse{\boolean{thing}}.
    – Dan
    Commented Mar 20, 2014 at 17:59
  • Already answered by: tex.stackexchange.com/questions/5894/… Commented Mar 21, 2014 at 4:51
  • @WillRobertson What I want is to pass them to a command, which is not covered by that answer. Commented Mar 21, 2014 at 15:19
  • @Dan I would never have thought it had to be that way. So far, I have no knowledge of the difference between a command and a newif command. Commented Mar 21, 2014 at 15:21
  • @ArthurChamz The difference is irrelevant. The point is that \ifthenelse isn't followed by an if-command in braces, but rather by a boolean expression in braces. See cfr's answer. See also the ifthen documentation for what syntax is required by \ifthenelse.
    – Dan
    Commented Mar 21, 2014 at 19:11

2 Answers 2

6

I'd use etoolbox for this, rather than ifthen.

\documentclass{article}
\usepackage{etoolbox}

\newtoggle{thing}

\newcommand{\foo}[2]{%
  \iftoggle{#1}
    {%
     --#2--% do something if set, using #2
    }%
    {%
     \fbox{#2}% do something else if not set, using #2 (default)
    }%
}

\begin{document}

% thing is initially false

\foo{thing}{baz}

\toggletrue{thing}

\foo{thing}{baz}

\end{document}

enter image description here

2
  • Could you explain what the advantage of using etoolbox is? I'm curious as I've never used this method and I'm wondering why it is preferable.
    – cfr
    Commented Mar 21, 2014 at 2:44
  • @cfr \ifthenelse is never “fully expandable”, while several of the conditional macros of etoolbox are.
    – egreg
    Commented Mar 21, 2014 at 10:41
3

There are different ways of defining conditionals. When you use or set the value of a conditional, you need to use the right syntax for the definition you've chosen.

In addition to the etoolbox method demonstrated in egreg's answer (which I've never used), there are at least two other very common methods. Your problem is caused by trying to use the syntax for one of these methods when handling a conditional defined using the other method.

The following example demonstrates the two methods:

\documentclass{article}
\newif\iffoo% new conditional defined using method 1
\footrue
\newcommand*{\fooboo}{%
  \iffoo
    {\Huge FOO!\par}
  \else
    {\tiny fooless\dots\par}
  \fi}
\usepackage{ifthen}% method 2 requires ifthen
  \newboolean{bar}% new conditional defined using method 2
  \setboolean{bar}{true}
  \newcommand*{\barboo}{%
    \ifthenelse{\boolean{bar}}{\Huge BAR!\par}{\tiny barless\dots\par}}

\begin{document}
\fooboo
\foofalse% note syntax for foo
\fooboo
\barboo
\setboolean{bar}{false}% note syntax for bar
\barboo
\end{document}

conditional foos and bars

2
  • What are the % at the end of the lines for, as seen in \newcommand*{\fooboo}{% ? Commented Mar 21, 2014 at 15:25
  • @ArthurChamz It just prevents the introduction of spurious spaces. Normally a newline would be interpreted as a space and you probably don't want that built into the command. In the other cases, they are just there so TeX doesn't try to read the comment.
    – cfr
    Commented Mar 21, 2014 at 15:38

You must log in to answer this question.

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