`\bool_if:NTF` 和 latex3 中条件的最佳使用

`\bool_if:NTF` 和 latex3 中条件的最佳使用

这个问题是关于 LaTeX3 中条件的最佳用法。

我想使用expl3语法添加类似这样的内容

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

我可以这样做

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

但我也可以做

\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}

那么最好的选择是什么? 又有什么区别呢?

答案1

一个区别是,expl3 版本清除了构建条件的所有标记,因此只保留 T 或 F 标记,这意味着它们可以向前看,请参见此处,其中参数形式在第一种情况下不起作用(#1原样\fi

在此处输入图片描述

\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}

第一种情况下的问题可以通过通常的\expandafter\@firstoftwo习语来解决,除此之外,这实际上是一个选择的问题,以及你需要多少“新”布尔值来与 tex 原语进行交互。

第二种形式适用于一次性测试,但如果您有一个包含大量测试的模块,则第三种形式对于特定于模块的测试可能更自然。

相关内容