\def, \newcommand, \span and \align

I learnt something about TeX/LaTeX today.

I got the following cryptic error message:
! Missing # inserted in alignment preamble.

\crcr
l.38 \end{align*}

Now when I went to google “Missing # inserted in alignment preamble”, I immediately got a lovely website which was able to answer my question. But hey, information survives by being copied, so if you’ll kindly allow me to continue, I shall.

Stripped down to a minimal example, here is the problematic code.

\documentclass[11pt, reqno]{amsart}

\usepackage{amsmath}

\def\span{\,\mbox{span}\,}

\begin{document}

\begin{align*}
u &= \arctan x & dv &= 1 \, dx
\\ du &= \frac{1}{1 + x^2} dx & v &= x.
\end{align*}

\end{document}

Actually stripping down to a minimal example kind of makes it obvious where the error is – it must be somewhere in the \def\span line.

It turns out that \def and \newcommand act differently. I’d always just assumed that \def and \newcommand were synonymous, and used the former since it involved less typing. And it’s true, most of the time you don’t notice the difference.

The problem in the above code is that \span is already defined to be something important needed to internally execute the \align command. By using \def, we’re redefining what \span means. If we used \newcommand instead, we wouldn’t be able to redefine \span and we’d get an instructive error message.

So the moral of the story is: \newcommand is safer.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: