4
\documentclass{amsart}
{\catcode`\z\active
\global\def\activate{\catcode`\z\active\defz{active}z}}
\begin{document}
{\activate z} %1
\begin{equation}\activate z\end{equation} %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?

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
LSpice
  • 1,448
  • 3
    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 08 '15 at 21:32
  • Oh, as I speculated in my edit. Do you know why this is done? – LSpice Mar 08 '15 at 21:36
  • 1
    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 08 '15 at 21:48
  • Thank you. Would you be willing to post that as an answer, so that I can accept it? – LSpice Mar 08 '15 at 22:02
  • yes I was just looking if it's a duplicate but nearest seems to be this which is a bit different http://tex.stackexchange.com/questions/112558/some-newcommand-instructions-not-working – David Carlisle Mar 08 '15 at 22:12
  • Can you explain if you have an application in mind or if it's just a theoretical question? Note that, even if the \activate problem was solved, you'd need to repeat it in every align cell. – egreg Mar 08 '15 at 22:15
  • @egreg, I use a lot of {align} environments, and I wanted my = to act automatically as alignment points, so I used \let\equal==\catcode\=\active\def={&{}\equal} (there should be a backtick in there, but it confuses MathJax). Doing this once before the {align} seems to work. (As I was looking up the restriction DavidCarlisle quoted, I came across the breqn package, which looks like it tries to do such trickiness automatically. Is this package recommended for use? There are indications in various places that it should be considered experimental.) – LSpice Mar 08 '15 at 22:26
  • 1
    @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 08 '15 at 22:35
  • @DavidCarlisle, thanks. My reference to MathJax was literally to it processing my comment above, not to the document that I am trying to write; I don't know how to escape the backtick that appears after \catcode so that MathJax doesn't see it. – LSpice Mar 08 '15 at 22:49
  • 1
    I noticed afterwards but that threw me as there is no mathjax on this site:-) – David Carlisle Mar 08 '15 at 22:53
  • Oops, right you are. I should have said Markdown (or whatever the SO flavour of it is called). – LSpice Mar 08 '15 at 22:56
  • I certainly wouldn't make the alignment on = automatic like this even with the math-active version egreg showed, which is slightly safer, it makes the alignment syntax in your documents incompatible with anyone else, which makes them much harder to process (eg convert to html with text4ht or mathjax etc, and harder to share with other tex documents) – David Carlisle Mar 08 '15 at 22:57
  • @DavidCarlisle, those points seem to be an argument against using any macros in my code. Also, while I'm sympathetic to the first goal (writing conversion-friendly documents), should "share with other TeX documents" really be a goal? If I'm not writing a code sample in a package document, then I don't expect others to include my code literatim in their documents! – LSpice Mar 08 '15 at 23:00
  • 3
    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 08 '15 at 23:05

2 Answers2

4

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.

David Carlisle
  • 757,742
3

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.

enter image description here

egreg
  • 1,121,712
  • 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 08 '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 08 '15 at 22:51
  • 1
    @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 08 '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 08 '15 at 23:02