# Create and center nice EBNF

I used the package syntax to create the following EBNF.

1. I would like to center it, however \begin{center}...\end{center} and other commands do not seem to work on the grammar environment.
2. When I set \grammarindent to a larger value also the ::= gets indented. Do you have an idea how I can indent the \alt behind the ::=?
\documentclass[a4paper,12pt]{article}
\usepackage[nounderscore]{syntax}
\begin{document}
\shortverb{\|}
\setlength{\grammarindent }{2cm}
\begin{grammar}
<stmt> ::= <stmt>; <stmt> | \textbf{skip} | $v :=$ <expr> | \textbf{input} $\vec{v}$ | \textbf{output} $\vec{v}$
\alt \textbf{if} <expr> \textbf{then} <stmt> \textbf{else} <stmt> | \textbf{while} <expr> <stmt>

<expr> ::= $v$ | \textbf{tt} | \textbf{ff} | <expr> $\lor$ <expr> | $\lnot$<expr>.
\end{grammar}
\end{document}

• – knut
Aug 10 '13 at 18:37

The grammar environment is based on a list structure, which is naturally left aligned. One way to get a centering effect is to put the whole structure into a minipage inside a center environment.

The original command \alt is defined to be

 \\\llap{\textbar\quad}


i.e. this starts an new line and prints to the left of the new column a vertical bar followed by a space of one \quad = 1em. You can just define a similar command that removes the \llap so the vertical bar (and space) are not pushed out leftwards. However, I suggest a command that does a little more:

\newcommand{\indalt}[1][2]{\\\hspace*{#1em}\textbar\quad}


This provides by default 2em of extra space before the bar, but takes an optional argument so \indalt[0] will give no indent and \indent[4] will give an indent of 4em.

\documentclass[a4paper,12pt]{article}

\usepackage[nounderscore]{syntax}

\shortverb{\|}
\setlength{\grammarindent}{2cm}

\begin{document}

\hrule

\begin{center}
\begin{minipage}{0.8\linewidth}
\begin{grammar}
<stmt> ::= <stmt>; <stmt> | \textbf{skip} | $v :=$ <expr>
\indalt \textbf{input} $\vec{v}$ | \textbf{output} $\vec{v}$
\indalt[4] \textbf{if} <expr> \textbf{then} <stmt> \textbf{else} <stmt>
\indalt \textbf{while} <expr> <stmt>

<expr> ::= $v$ | \textbf{tt} | \textbf{ff} | <expr> $\lor$
<expr> | $\lnot$<expr>.
\end{grammar}
\end{minipage}
\end{center}
\end{document}


If you wish to redfine the \alt command itself you will have to do this in the grammar environment itself, as it is not defined globally.