# Errors when trying to define a custom align environment

I'm currently trying to create a custom environment with its own counter.

\documentclass[a4paper,11pt]{report}
\usepackage[fleqn]{amsmath}
\newcounter{grammarcounter}

\newenvironment{grammar}{
\refstepcounter{grammarcounter}
\begin{equation*}
\tag{$\Gamma_{\thegrammarcounter}$}
}{\end{equation*}}
\numberwithin{grammarcounter}{chapter}

\begin{document}

\begin{grammar}
\label{gr:label}
E ::= E + E \\
E ::= a
\end{grammar}

\end{document}


This works nicely for simple equations, but the line split \\ does not work inside the equation* environment. Ideally I would like to replace the equation* environment with the align* environment. However, when simply replacing equation* in the example with align*, I get LaTeX errors indicating \begin{align} on input line .. ended by \end{grammar}.

How can I create a custom equation environment with its own counter that respects line breaks?

-
Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. –  Jubobs Feb 28 at 11:53
You probably want to use the aligned (or gathered) environment nested in grammar. –  egreg Feb 28 at 12:02
Thank you very much! I didn't know of the existence of the aligned environment. I've added \begin{aligned} after \tag{} and \end{aligned} before \end{equation*} and now it works perfectly! :-) –  eider Feb 28 at 12:06

I'm not sure why but you can't use \begin{align} and \end{align} in the definition of a new environment; you have to use the "lower-level" macros \align and \endalign instead. Edit: as pointed by alexwlchan in his comment, you can find more details about that in section 6 of Technical notes on the amsmath package.

Here I've used the equivalent of an align* environment (see Herbert's answer to Define a custom align, and align* environment).

Note that you will get an error if you try to reset your grammarcounter at each chapter in the article class, because the latter doesn't have chapters; \section is the most high-level sectioning command in the article class. Did you mean

\numberwithin{grammarcounter}{section}


\documentclass{article}

\usepackage{amsmath}

\newcounter{grammarcounter}[section]

\makeatletter
\newenvironment{grammar}
{%
\refstepcounter{grammarcounter}
\start@align\@ne\st@rredtrue\m@ne
\tag{$\Gamma_{\thegrammarcounter}$}
}{%
\endalign
}
\makeatother

\begin{document}

\section{Foo}

\begin{grammar}
\label{gr:label}
E &::= E + E \\
E &::= a     \\
E &::= b
\end{grammar}

\begin{grammar}
\label{gr:label2}
E &::= E + E \\
E &::= a     \\
E &::= b
\end{grammar}

\end{document}

-
Thank you! This does the trick perfectly :-) –  eider Feb 28 at 12:08
Section 6 of technote.pdf has a little more detail on why you can't use abbreviations for the {align} environment: <mirrors.ctan.org/macros/latex/required/amslatex/math/…; –  alexwlchan Feb 28 at 18:20
@Jubobs: sorry, I made a Markdown error. Here's a working link: mirrors.ctan.org/macros/latex/required/amslatex/math/… –  alexwlchan Mar 1 at 11:29

You can nest aligned or split in the grammar environment:

\documentclass[a4paper,11pt]{report}
\usepackage[fleqn,tbtags]{amsmath}

\newcounter{grammarcounter}[chapter]
\renewcommand{\thegrammarcounter}{\thechapter.\arabic{grammarcounter}}

\newenvironment{grammar}
{\refstepcounter{grammarcounter}
\begin{equation*}
\tag{$\Gamma_{\thegrammarcounter}$}}
{\end{equation*}}

\begin{document}

\chapter{Start}

\begin{grammar}
\label{gr:label}
\begin{aligned}
E &::= E + E \\
E &::= a
\end{aligned}
\end{grammar}

\begin{grammar}
\label{gr:label-two}
\begin{split}
E &::= E + E \\
E &::= a
\end{split}
\end{grammar}

\end{document}


Using split without the tbtags option is equivalent to aligned (the number will be vertically centered). So I presented two examples to show the difference, adding the option.

I wouldn't make grammar into an automatically multiline environment, as spacing considerations are involved. You can define a mlgrammar environment, if you want:

\newenvironment{mlgrammar}
{\begin{grammar}\begin{aligned}}
{\end{aligned}\end{grammar}}

-