10

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.