0

This question is about the best use of conditional in LaTeX3.

I want to add somthing like this using expl3's syntax

\newif\ifsomething 
\newcommand{\Ifsomething}[2]{%
    \ifsomething #1\else #2\fi}
\Ifsomething{if trut}{if false}

I can do this with

\bool_new:N \g_myclass_something_bool  
\bool_if:NTF \g_myclass_something_bool {if~trut}{if~false}

but i can also do

\bool_new:N \g_myclass_something_bool
\prg_new_conditional:Npnn \myclass_Ifsomething: {T, F, TF}
{
    \bool_if:NTF \g_myclass_something_bool
        {\prg_return_true:}
        {\prg_return_false:}
}  
\myclass_Ifsomething:TF {if trut}{if false}

So what is the best choice? and what is the diference?

2
  • 1
    When you already have a boolean variable, I'd just use \bool_if:NTF. Your conditional doesn't add any functionality and is just overhead, imho.
    – Skillmon
    Jan 16, 2021 at 18:52
  • @Skillmon this is what i think, but i want to be more sure. Jan 16, 2021 at 18:59

1 Answer 1

1

One difference is that the expl3 versions clear away all the tokens that build the conditional so just the T or F tokens remain, which means that they may look ahead, see here where the argument form does not work in the first case (as #1 is \fi)

enter image description here

\documentclass{article}
\newcommand\zzes[1]{yes(#1)}
\newcommand\zzno[1]{no(#1)}
\begin{document}
\newif\ifsomething 
\newcommand{\Ifsomething}[2]{%
    \ifsomething #1\else #2\fi}
\Ifsomething{\zzyes}{\zzno}{A}

\par\bigskip
I can do this with

\ExplSyntaxOn
\bool_new:N \g_myclassa_something_bool
\bool_if:NTF \g_myclassa_something_bool {\zzyes}{\zzno}{B}
\ExplSyntaxOff

but i can also do

\ExplSyntaxOn
\bool_new:N \g_myclassb_something_bool
\prg_new_conditional:Npnn \myclassb_Ifsomething: {T, F, TF}
{
    \bool_if:NTF \g_myclassb_something_bool
        {\prg_return_true:}
        {\prg_return_false:}
}  
\myclassb_Ifsomething:TF {\zzyes}{\zzno}{C}
\ExplSyntaxOff

\end{document}

The issue in the first case is fixable via the usual \expandafter\@firstoftwo idiom, other than that it is really a matter of choice and how much you need the "new" boolean to interact with tex primitive if.

The second form is OK for one-off tests but if you have a module with lots of tests, the third form with a module-specific test might be more natural.

4
  • the question is about latex3. I edit my question. Sorry. Jan 16, 2021 at 19:12
  • @studentmaths my answer used the same expl3 forms as your question, so I don't understand your comment? Jan 16, 2021 at 19:15
  • what i really want is the difference between the expl3 two forms. Jan 16, 2021 at 19:17
  • @studentmaths the last one is a wrapper around the other so it's slower but gives a module-specific conditional which may look better in some circumstances. Jan 16, 2021 at 19:19

You must log in to answer this question.

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