1

I want to do something like this:

\DeclareDocumentCommand{\myCondition}{m}
{
    \ifx <stuff>
        return true
    \else
        return false
    \fi
}

And use it like this:

\ifx \myCondition{something}
    % do stuff
\fi

Are there any alternatives?

  • Much simpler if you use \if\condition{something} do stuff \fi. – Manuel Oct 26 '17 at 13:11
  • So, how can I make a macro return something? Can I make something similar to programming? – Emil Terman Oct 26 '17 at 13:14
  • Macros expand as text. Even macros used for numerical values are actually the text version of those numbers. You might try \ifnum instead of \ifx and return 0=0 or 0=1. – John Kormylo Oct 26 '17 at 13:36
1

If this is all that you want to do then you only need to define a new if-statement:

\newif\ifCondition

and then just set \Conditiontrue and \Conditionfalse. In more detail:

\newif\ifCondition
\DeclareDocumentCommand{\myCondition}{m}
{
    \ifx <stuff>
        \Conditiontrue  % return true
    \else
        \Conditionfalse % return false
    \fi
}

and then later you can use:

\myCondition{something}% set the \Conditiontrue or \Conditionfalse
\ifCondition% use the condition
    % do stuff
\fi

If \myCondition is likely to be hidden inside a group you might need to use \global\Conditiontrue etc -- although, this really depends on your use-case because, personally, I often find it really useful to be able to make "conditional changes" local to a group.

  • Is there a more general/cleaner way? – Emil Terman Oct 27 '17 at 18:14
  • @EmilTerman Without knowing more about your actual use-case it's hard to say but I think that it would be cleaner to "do stuff" inside the \myCondition macro. This way you don't need the \ifCondition at all. – Andrew Oct 27 '17 at 20:15
  • What If I will need to do if (\myCondition and \anotherCondition)? – Emil Terman Oct 28 '17 at 15:23
  • With abstract questions without specific details it is always hard to say. Perhaps you need something like the ifthenelse package. For the question you asked I'd take this approach. – Andrew Oct 28 '17 at 18:53
0

If you use

\if\condition{1}
  do stuff
\else
  do other stuff
\fi

You can survive with having

\def\condition#1{\relax\relax\fi % this ends the \if test
  \ifx<your tests> % here you put your code to test: \ifnum, \ifmmode, or whatever
    \csname iftrue\expandafter\endcsname % and this would reopen a new \if test which is true
  \else
    \csname iffalse\expandafter\endcsname % and this would reopen a new \if test which is false
  \fi}

Can't test right now if it works with xparse unexpandable definitions or if they do insert something else. You can use \DeclareExpandableDocumentCommand to define \something.

\DeclareExpandableDocumentCommand\condition{m}{..}
  • I wonder if you mean \if1\condition{arguments}% and the macro \condition expands to either 0 or 1, or would that be an altogether other idea? – user4686 Oct 26 '17 at 14:01
  • That would be other idea, here it does expand to 11\fi<contents and leave a new if open>\iftrue. – Manuel Oct 26 '17 at 14:21

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.