0

I have a command \yesifone that should take either 1 or 0 as an argument, and behaves differently depending on these two cases. I want to pass to it the output of a command \one. However, \one relies upon some other command \blah that generates no output (in my concrete case, it sets a variable). My current attempt to make this work fails, presumably because ifnum includes the \blah{} in the comparison, rather than just the number 1. How can I make this work, e.g. by ignoring the \blah{} invocation and only checking the output text?

In other words, I'd like the following to output yes.

\documentclass{minimal}

\begin{document}

\newcommand{\blah}{}

\newcommand{\one}{ \blah{} 1 }

\newcommand{\yesifone}[1]{% \ifnum1=#1\relax{yes}\else{no}\fi }

\yesifone{\one} % error: Missing number, treated as zero.

\end{document}

varkor
  • 539
  • 2
  • 12

1 Answers1

2

With your code \yesifone{\one} yields the tokens
\yesifone{1\one}2

This expands to
\ifnum112=12\one\relax{1y11e11s11}2\else{1n11o11}2\fi.

At the time of gathering tokens that belong to \ifnum's second TeX-⟨number⟩-quantity, i.e., the number behind =12, the token \one gets expanded, thus you have s.th. like

\ifnum112=1210\blah{1}21011210\relax{1y11e11s11}2\else{1n11o11}2\fi.

Space-tokens 10 right behind =12 get removed and \blah gets expanded and just vanishes as its replacement-text is empty. Thus you have s.th. like:

\ifnum112=12{1}21011210\relax{1y11e11s11}2\else{1n11o11}2\fi.

So at the time of gathering tokens that belong to \ifnum's second TeX-⟨number⟩-quantity, i.e., the number behind =12, TeX finds an empty brace-group, i.e., explicit-character-tokens {1 and }2, which definitely does not form the begin of a sequence of tokens that form a valid TeX-⟨number⟩-quantity.


Think about Knuth's analogy of TeX being a beast with eyes and a digestive tract.

  • Expansion of expandable tokens takes place in the gullet in some sort of regurgitation process unless expansion is suppressed as is the case, e.g., with the tokens that form the parameter text or the replacement-text of a \def-assignment. (LaTeX's \newcomand and \NewDocumentCommand etc are sophisticated wrappers for calling \def.)
  • Assignments take place in the stomach.

So separate tasks like assigning values to "variables" which involve the stomach for doing non-typesetting-work from tasks where the gullet/expansion of expandable tokens is sufficient and where digestive organs behind the gullet are involved only for typesetting:

\documentclass{minimal}

% Introduce/initialize things used as variable whose value is % to be set via assignments that take place in the stomach: \newcommand\VariableRelatedToBlah{}

% Define macros for tasks that involve digestive organs behind the gullet for % for non-typesetting-tasks, e.g.,setting values of variables via assignments: \newcommand\SetValueOfVariableRelatedToBlah[1]{% \def\VariableRelatedToBlah{#1}% }

% Define macros for tasks that involve only the gullet, e.g., % retrieving values of variables, or additionally to the gullet % involve digestive organs behind the gullet only for typesetting: \newcommand\RetrieveValueOfVariableRelatedToBlah{% \VariableRelatedToBlah } \newcommand\firstofone[1]{#1}% \newcommand{\yesifone}[1]{% \ifnum1=\expandafter\firstofone\expandafter{\number#1} yes\else no\fi }

\begin{document}

% Now you can keep work that involves the stomach for non-typesetting % separated from work where the gullet is sufficient/where tokens % delivered by the gullet can directly be used for typesetting:

\SetValueOfVariableRelatedToBlah{0}% \yesifone{\RetrieveValueOfVariableRelatedToBlah}

\SetValueOfVariableRelatedToBlah{1}% \yesifone{\RetrieveValueOfVariableRelatedToBlah}

\end{document}

enter image description here

Ulrich Diez
  • 28,770
  • thanks for your answer. I have been very busy this week, so haven't had a chance to read it properly yet, but shall do so soon! – varkor May 13 '22 at 21:57
  • @varkor Don't worry and don't hurry. ;-) This is not necessarily a real-time medium. Find a time window where you can approach things without stress and with care. None of the answerers should be in a hurry ;-) If you have more questions, don't hesitate to ask them here on TeX LaTeX StackExchange. – Ulrich Diez May 14 '22 at 13:19
  • Thank you for your helpful answer, and sorry for taking so long to accept this! – varkor Nov 07 '23 at 22:41
  • @varkor Never mind. ;-) – Ulrich Diez Nov 08 '23 at 04:00