# Amsmath environments eating \active characters

\documentclass{amsart}
{\catcode\z\active
\global\def\activate{\catcode\z\active\defz{active}z}}
\begin{document}
{$$\activate z$$} %1
$$\activate z$$ %2
\begin{align}$$\activate z$$\end{align} %3
{\activate\begin{align}z\end{align}} %4
\end{document}

(1) and (2) behave as I would expect, printing out 'activeactive' (in a displayed equation, for (2)). However, (3) prints out 'activez', and I can't figure out why.

EDIT: (4) also behaves as I would expect, printing out 'active' before the align, and then another 'active' inside the align. Is the align environment (unlike, say, the equation environment) reading its body as an argument?

• ams alignments treat their body as the argument of a command so like any catcode change, your \activate or \verb etc will not work. – David Carlisle Mar 8 '15 at 21:32
• This is documented restriction why you can not define shorthands like \ba \ea. The body is set twice to do measurement and layout tests. – David Carlisle Mar 8 '15 at 21:48
• @LSpice breqn is definitely experimental, and incompatible with most things, also if you are wanting things to work in mathjax, no catcode tricks are supported there. – David Carlisle Mar 8 '15 at 22:35
• I noticed afterwards but that threw me as there is no mathjax on this site:-) – David Carlisle Mar 8 '15 at 22:53
• No if you use \newcommand\foo{..}...\foo then that is normal supported use and any system can understand it, if you use low level catcode assignments that change the underlying syntax then typically non-tex systems like mathjax or tex4ht will have problems, and many tex based journal submission requirements would not allow it as if you allow such things you basically lose control over anything the document is doing so it is so much harder for a journal class to force a house style. (actually this particular automatic & is fairly benign, but checking it's benign is hard...) – David Carlisle Mar 8 '15 at 23:05

AMS alignments treat their body as the argument of a command so like any catcode change, your \activate or \verb etc will not work.

This is documented restriction why you can not define shorthands like \ba \ea and must use the \begin ... \end syntax.

The body is set twice to do measurement and layout tests, so the code grabs the body as an argument so that it can be reused.

Instead of using active characters, you can use a math active character, which doesn't require changing category code and so avoids the problem due to the fact that align loads the contents as the argument to a macro.

\documentclass{article}
\usepackage{amsmath}

\mathchardef\equal=\mathcode=

\newenvironment{autoalign}
{\activateequal\align}
{\endalign}
\newcommand{\activateequal}{%
\mathcode==\string"8000
\begingroup\lccode~==
\lowercase{\endgroup\def~}{&\equal}%
}

\begin{document}

\begin{align}
a     &= b+c \\
a^{2} &= b^{2}+2bc+c^{2}
\end{align}

\begin{autoalign}
a     = b+c \\
a^{2} = b^{2}+2bc+c^{2}
\end{autoalign}

\end{document}

In the autoalign environment, the = character is made math active (mathcode "8000) and its active version is defined to be &\equal. Then the normal align environment is started.

I see no usefulness in this approach, though, but just code obfuscation.

• Thanks for this answer. Of course it's a matter of taste, but I don't see what's obfuscatory about the latter version. It definitely favours implicit over explicit, but I don't think that's the same thing. (Also, if it's any argument, this seems to be a very minor version of the magic worked by breqn, so at least there is precedent.) – LSpice Mar 8 '15 at 22:50
• Also, is there something missing at the end of the 4th line of your code sample, or is it really supposed to end with the = character? – LSpice Mar 8 '15 at 22:51
• @LSpice supposed to end that way, it's defining \equal to act like = that final = is a character not part of an assignment syntax. – David Carlisle Mar 8 '15 at 22:59
• @DavidCarlisle, I see, thanks. I was incorrectly reading \mathcardef\equal= as the complete definition, and the rest as a new command. – LSpice Mar 8 '15 at 23:02