考虑以下逻辑陈述:
If \conditionA != 100 and \conditionB != 100 do ``Something~A''
If \conditionA != 100 and \conditionB = 100 do ``Something~B''
If \conditionA = 100 and \conditionB != 100 do ``Something~C''
If \conditionA = 100 and \conditionB = 100 do ``Something~D''
我如何将其翻译成
\ifnum ... do ... \else do ... \fi
论点类型?
我发现这个帖子由 Joseph Wright 编写,但我不知道如何修改示例以使其实现我想要的效果。
PS 请随意添加缺失的标签。
答案1
如果需要,可以使用 LaTeX3 语法来扩展该命令:
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\int_new:N \l_svend_condition_A_int
\int_new:N \l_svend_condition_B_int
\cs_new:Npn \svend_check:
{
\int_compare:nTF { \l_svend_condition_A_int = 100 }
{
\int_compare:nTF { \l_svend_condition_B_int = 100 }
{
\typeout{Both~are~100}
}
{
\typeout{A~is~100,~B~is~not~100}
}
}
{
\int_compare:nTF { \l_svend_condition_B_int = 100 }
{
\typeout{A~is~not~100,~B~is~100}
}
{
\typeout{A~is~not~100,~B~is~not~100}
}
}
}
\int_set:Nn \l_svend_condition_A_int {100}
\int_set:Nn \l_svend_condition_B_int {100}
\svend_check:
\int_set:Nn \l_svend_condition_A_int {100}
\int_set:Nn \l_svend_condition_B_int {101}
\svend_check:
\int_set:Nn \l_svend_condition_A_int {101}
\int_set:Nn \l_svend_condition_B_int {100}
\svend_check:
\int_set:Nn \l_svend_condition_A_int {101}
\int_set:Nn \l_svend_condition_B_int {101}
\svend_check:
这是终端上的输出:
Both are 100
A is 100, B is not 100
A is not 100, B is 100
A is not 100, B is not 100
使用传统语法是一样的:
\documentclass{article}
\newcommand{\Check}[2]{%
\ifnum#1=100
\ifnum#2=100
\typeout{\#1 is 100, \#2 is 100}%
\else
\typeout{\#1 is 100, \#2 is not 100}%
\fi
\else
\ifnum#2=100
\typeout{\#1 is not 100, \#2 is 100}%
\else
\typeout{\#1 is not 100, \#2 is not 100}%
\fi
\fi
}
\Check{100}{100}
\Check{101}{100}
\Check{100}{101}
\Check{101}{101}
输出如下:
\#1 is 100, \#2 is 100
\#1 is not 100, \#2 is 100
\#1 is 100, \#2 is not 100
\#1 is not 100, \#2 is not 100
答案2
为了完整起见,这里有一个etoolbox
执行:
\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newcommand{\Test}[2]{%
\ifnumequal{#1}{100}{%
\ifnumequal{#2}{100}{%
First arg = 100; Second arg = 100%
}{%
First arg = 100; Second arg != 100%
}}{%
\ifnumequal{#2}{100}{%
First arg !=100; Second arg = 100%
}{%
First arg !=100; Second arg != 100%
}}%
}
\begin{document}
\Test{100}{100} \par
\Test{ 99}{100} \par
\Test{101}{101} \par
\Test{100}{101}
\end{document}
答案3
解决方案是否必须达到 TeX 基元级别?如果不是,我想您可以加载ifthen
包并执行如下操作:
\ifthenelse{\NOT\conditionA=100}%
{\ifthenelse{\NOT\ConditionB=100}%
{"Do SomethingA"}%
{"Do SomethingB"}}%
{\ifthenelse{\NOT\ConditionB=100}%
{"Do SomethingC"}%
{"Do SomethingD"}}
这里,我假设\ConditionA
并\ConditionB
求值/扩展为整数。如果不是这种情况,则\NOT...
分支将始终占上风。
附录:假设宏\ConditionA
和\ConditionB
是可扩展的并且求值为整数,那么您也可以使用\ifnum
“原始”控制序列。请注意,为了简单起见,测试的是相等而不是不等;因此“Do”序列相对于前面的示例是相反的。
\ifnum\conditionA=100%
\ifnum\conditionB=100
{"Do SomethingD"}
\else {"Do SomethingC"}
\fi
\else
\ifnum\conditionB=100
{"Do SomethingB"}
\else {"Do SomethingA"}
\fi
\fi
答案4
为了完整性,(希望很快被完全弃用的) TeX-core 解决方案是完全可扩展的,就像@egreg 的解决方案一样:
\long\def\FirstOfFour#1#2#3#4{#1}
\long\def\SecondOfFour#1#2#3#4{#2}
\long\def\ThirdOfFour#1#2#3#4{#3}
\long\def\FourthOfFour#1#2#3#4{#4}
\def\HundredTest#1#2{%
\ifcase\numexpr
\ifnum #1=100 1\else0\fi +
\ifnum #2=100 2\else0\fi \relax
\expandafter\FirstOfFour\or
\expandafter\SecondOfFour\or
\expandafter\ThirdOfFour\or
\expandafter\FourthOfFour
\fi
}
\HundredTest{100}{111}{NN}{YN}{NY}{YY} %YN
\HundredTest{100}{100}{NN}{YN}{NY}{YY} %YY
\HundredTest{111}{111}{NN}{YN}{NY}{YY} %NN
\HundredTest{111}{100}{NN}{YN}{NY}{YY} %NY
\bye