我正在尝试让一个与非门像这文章中,它提出了一个问题,即 ifnum 被输入一个整数,而不是一对带等号的整数。当我修复这个问题时,我想出了如下代码:
\newcommand{\nand}[2]{
\count0=#1
\count1=#2
\ifnum \count0=\count1 \ifnum \count0=1 0\else 1 \fi \else 1 \fi
}
一开始似乎运行良好,即当我这样做时,\nand{1}{1}
我得到了0
。但是,当我尝试时,\nand{\nand{1}{1}}{1}
我得到了=1 0 0
。这似乎没有任何意义。我是否需要使用其他关键字来强制 LaTeX 使用命令的结果?是什么原因导致它不输出1
?
答案1
宏中的赋值使其不可扩展,因此您无法将其嵌套在其他宏中。如果您不进行赋值,则可以嵌套它们:
\documentclass{article}
\newcommand{\nand}[2]{%
\ifnum #1=#2 \ifnum #1=1 0\else 1 \fi \else 1 \fi
}
\begin{document}
\verb|\nand{0}{0}|: \nand{0}{0}% 1
\verb|\nand{0}{1}|: \nand{0}{1}% 1
\verb|\nand{1}{0}|: \nand{1}{0}% 1
\verb|\nand{1}{1}|: \nand{1}{1}% 0
\bigskip
\verb|\nand{\nand{0}{0}}{0}|: \nand{\nand{0}{0}}{0}% \nand{1}{0} = 1
\verb|\nand{\nand{0}{1}}{0}|: \nand{\nand{0}{1}}{0}% \nand{1}{0} = 1
\verb|\nand{\nand{1}{0}}{0}|: \nand{\nand{1}{0}}{0}% \nand{1}{0} = 1
\verb|\nand{\nand{1}{1}}{0}|: \nand{\nand{1}{1}}{0}% \nand{0}{0} = 1
\verb|\nand{\nand{0}{0}}{1}|: \nand{\nand{0}{0}}{1}% \nand{1}{1} = 0
\verb|\nand{\nand{0}{1}}{1}|: \nand{\nand{0}{1}}{1}% \nand{1}{1} = 0
\verb|\nand{\nand{1}{0}}{1}|: \nand{\nand{1}{0}}{1}% \nand{1}{1} = 0
\verb|\nand{\nand{1}{1}}{1}|: \nand{\nand{1}{1}}{1}% \nand{0}{1} = 1
\end{document}
答案2
如果你这样写它就会有效:
\newcommand\nand[2]{
\ifnum #1=#2 \ifnum #1=1 0 \else 1 \fi \else 1 \fi
}