0

I've tried the following

\newdimen\fooDimen
\fooDimen=10.0pt
\newdimen\barDimen
\barDimen=20.0pt
 
\newcommand{\fooCommand}{2}{%
\ifdim#1 < #2
\typeout{Test}
\fi
}
 
\begin{document}
\fooCommand{\fooDimen}{\barDimen}
\end{document}

but I get

! Missing number, treated as zero.
<to be read again>
                   ##
l.18 \ifdim#
            1 < #2

What's wrong? Many thanks in advance.

2
  • 1
    Use \newcommand{\fooCommand}[2]{...} instead. The number of arguments is provided in []. Also you should comment out the line ending after \typeout{Test}.
    – Skillmon
    Commented Sep 28, 2021 at 12:19
  • @Skillmon Many thanks!
    – Toru
    Commented Sep 28, 2021 at 12:21

1 Answer 1

1

I added \documentclass{article} and report the file with line numbers for reference:

  1 \documentclass{article}
  2 \newdimen\fooDimen
  3 \fooDimen=10.0pt
  4 \newdimen\barDimen
  5 \barDimen=20.0pt
  6
  7 \newcommand{\fooCommand}{2}{%
  8 \ifdim#1 < #2
  9 \typeout{Test}
 10 \fi
 11 }
 12
 13 \begin{document}
 14 \fooCommand{\fooDimen}{\barDimen}
 15 \end{document}

If I run LaTeX on this document, I get

! Missing number, treated as zero.
<to be read again>
                   ##
l.8 \ifdim#
           1 < #2
?

so you can clearly see that it's not the call of \fooCommand that triggers the error, which instead happens when TeX is examining line 8.

Now you should know that TeX makes no interpretation whatsoever of the replacement text when it is defining a new command and just stores it away. However, here the intended replacement text is actually being processed!

And the error message says that the # character is out of place: TeX wants to evaluate \ifdim, but it finds # instead (in the error message it appears doubled to show that the offending token has category code 6). Here no argument replacement is made, because TeX is not defining a command.

Where's the problem? You're using the wrong syntax for \newcommand: the number of arguments should be in brackets, not in braces. So what you're actually doing is to define \fooCommand to stand for 2. Then the definition has ended and TeX starts to process what comes along.

\newcommand{\fooCommand}[2]{%
  \ifdim#1 < #2
    \typeout{Test}%
  \fi
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .