22

In the code below, it took me a long time to figure out that the space in the second line below beginning with \def generates an error.

I'm new here. Where is the documentation of \def that would help me understand this issue?

\documentclass[aspectratio=1610]{beamer} 

\begin{document} 
\def\funa(#1,#2){ (#2)*(#1)^2} %  OK
%\def\funa(#1,#2) { (#2)*(#1)^2 }  % NOT OK ...WHY?

\begin{frame} 
      \pgfmathparse {\funa(3,4)}  \pgfmathresult \\   
\end{frame}
\end{document}
3
27

When writing a \definition, you have to understand that, with one minor exception, every character counts (see TeX By Topic, starting at p. 112, "Delimited parameters"). Namely, if you write

\def\macro<some text pattern involving #1, #2, ...>{...}

then TeX will search for the exact appearance of the pattern you give. Thus, if you write

\def\funa(#1,#2) { (#2)*(#1)^2 }

then in use, TeX will expect to see \funa followed by a (, something without , that it takes to be #1, then some more stuff without ) that it takes to be #2, then a ), and finally a space, because you wrote a space there. So if you use it without the space you will get an error.

The minor exception is that spaces immediately following the macro name are ignored: \def\macro{...} and \def\macro {...} are the same. This is a side-effect of how TeX converts your input into its internal directives, namely, that all spaces following a control word are ignored during tokenization. This elision occurs before the definition is even begun, and is not a property of \def at all but of general TeX syntax. However, for you, the space is protected from \funa by the intervening text, and is thus interpreted according to the macro definition syntax.

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.