3

I am trying to get a nand gate to work like the one mentioned in this article, and it takes issue with the ifnum being fed a single integer rather than a pair of integers with an equals. When I fix that, I come up with code that looks something like this:

 \newcommand{\nand}[2]{
 \count0=#1
 \count1=#2
 \ifnum \count0=\count1 \ifnum \count0=1 0\else 1 \fi \else 1 \fi
 } 

and at first seems to work fine, i.e. when I do \nand{1}{1} I get 0. However, when I try \nand{\nand{1}{1}}{1} I get =1 0 0. This does not seem to make any sense. Do I need to use a different keyword to force LaTeX to use the result of the command? What is causing this not to output 1?

  • First of all, \count0 is reserved for the page counter. – John Kormylo Jun 23 '16 at 3:26
2

Your assignments within the macro make it not expandable, and therefore you cannot nest it within other macros. If you go without the assignment, then you can nest them:

enter image description here

\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}
| improve this answer | |
3

It works if you write it this way:

\newcommand\nand[2]{
    \ifnum #1=#2 \ifnum #1=1 0 \else 1 \fi \else 1 \fi
}
| improve this answer | |
  • Interesting, any idea why? – nosyarg Jun 23 '16 at 3:54
  • I think, from Werner's explanation, that by having \counter1 and \counter2 defined/called within the macro, \counter1 ends up being nested inside itself. – Jeffery Shivers Jun 23 '16 at 7:12

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.