# Is \def sensitive to spacing?

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}
-
This has nothing to do with either beamer or spacing (that is, printed spaces) so I changed your tags, if that's okay. –  Ryan Reich Aug 11 '13 at 5:26
Though not as detailed and thorough as @RyanReich's answer, if you're just looking for basic documentation, Wikibooks is often a good place to start (in addition to TeX.SX). Here's the general TeX Wikibook, the LaTeX Wikibook, and the entry for \def. –  Adam Liter Aug 11 '13 at 5:33
add comment

## 1 Answer

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.

-
add comment