11

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?

3
  • 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.
    – jub0bs
    Feb 28, 2014 at 11:53
  • 1
    You probably want to use the aligned (or gathered) environment nested in grammar.
    – egreg
    Feb 28, 2014 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, 2014 at 12:06

3 Answers 3

7

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}

instead?

enter image description here

\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}
4
6

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.

enter image description here

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}}
0

Coincidentally, I was also making a custom align environment for grammars. I'm not using left-aligned equations (so no fleqn option for amsmath), but I did want the grammars to be left-aligned. Here's an environment-based solution that could help someone in a similar situation:

\documentclass[a4paper,11pt]{report}

\usepackage{amsmath}
\let\nr\relax%          https://tex.stackexchange.com/a/512854/203081
\usepackage{nccmath}  % https://tex.stackexchange.com/a/467619/203081

\newlength{\grammarindent}
\setlength{\grammarindent}{3em}
\newcounter{grammarcounter}[chapter]
\renewcommand{\thegrammarcounter}{\thechapter.\arabic{grammarcounter}}

\newenvironment{grammar}
{%
    \refstepcounter{grammarcounter}%
    \begin{fleqn}[\grammarindent]\begin{equation*}\left\{\:\tag{$G_{\thegrammarcounter}$}\begin{aligned}%
}%
{%
    \end{aligned}\right.\end{equation*}\par\end{fleqn}% You need the \par to prevent \end{fleqn} from acting like a space.
}


\begin{document}

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

\end{document}

nccmath provides the fleqn environment. I'm not sure why the above answers don't include the aligned inside the new grammar environment, but I have done so here and it works fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .