# What is wrong with the following definition?

I'm trying to define two commands \bal and \eal instead of inserting \begin{align} and \end{align} in my document, but in the code below, I'll get some errors. What is wrong with it?

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand{\bal}{%
\begingroup
\align%
\@ifstar{\nonumber}{}%
}
\newcommand{\eal}{\endalign\endgroup}
\makeatother
\begin{document}
\bal*
x=y
\eal
‎\end{document}

-
You cannot, align literally looks for \end{align}. In general it is a bad idea to write using shortcuts like these as it makes the code much harder to read. – daleif Feb 27 at 10:55
It has no sense to use align for a one line display; use equation, instead. Those shorthands may seem useful, but they aren't: they make the LaTeX document less readable and searchable. Good editors provide keyboard shortcuts that make easy inputting those environments. – egreg Feb 27 at 11:33
Related questions (possible duplicates?): tex.stackexchange.com/q/3169/86, tex.stackexchange.com/q/65581/86 – Andrew Stacey Feb 27 at 12:48
@egreg my code above is just a MWE. My problem is that why this definition works for, say, equation environment, but doesn't work for align environment. – Vahid Damanafshan Feb 27 at 13:33
They are different beasts – daleif Feb 27 at 13:58
show 1 more comment

The ‘problem’ is known and documented. I don't believe it's a problem at all, actually, for I can't see how

\bal
a&=b\\
c&<d
\eal


is clearer input than

\begin{align}
a&=b\\
c&<d
\end{align}


To the contrary, I firmly believe that the latter syntax is much better than the former, because it clearly marks the document. Good editors provide fast shortcuts for inputting the latter syntax with as few keystrokes as possible.

Let's see why your definition doesn't work. The align environment must absorb all of its contents before starting to typeset it, since it needs to do two passes over the material: the first pass is for measuring it, the second one for doing the real typesetting. This is the key for proper placement of the material and of the equation numbers.

With \newcommand{\bal}{\begingroup\align\@ifstar{\nonumber}{}} you start align, but LaTeX will never see the end of it, because it doesn't expand tokens while absorbing the material, so \eal will be absorbed and not recognized as the end of align.

You could define

\makeatletter
\newcommand{\bal}{\@ifstar{\@bals}{\@bal}}
\def\@bals#1\eal{\begin{align*}#1\end{align*}}
\def\@bal#1\eal{\begin{align}#1\end{align}}
\makeatletter


so that the required tokens are found, but it's clumsy and, as I said, it doesn't provide no benefit whatsoever, while obfuscating the code.

The issue doesn't show with equation, because this environment doesn't do complex tasks for measuring its contents. But the same arguments apply: there's no gain in inputting

\beq
a=b
\eeq


$$a=b$$


and nobody will ever be able to convince me of the contrary.

-

This issue is dicussed in section 6 of the file technote.pdf among the files distributed with amsmath.sty. (on my system texdoc technote.pdf will open this file in a pdf viewer.)

I copy here the end of this section, but you should read it in full.

Some workarounds:

1. \def\bal#1\eal{\begin{align}#1\end{align}}

2. Define \newcommand{\env}[2]{\begin{#1}#2\end{#1}} and then use \env{align}{...}

-