# Typesetting a calculus' grammar

I am trying to typeset the grammars needed to describe the applied pi calculus. The outcome should look somewhat similar to the following attached example, especially concerning the alignment of the right hand side text.

To me, this does not seem to be solvable straightforward with the syntax package. Though i am fairly convinced this is the package to go with, i have no idea on how to typeset such a layout despite reading the documentation. The grammar-environement seems to be rather restrictive. Any ideas are thus highly appreciated.

• Would there be a problem using a simple tabular? – Steven B. Segletes Nov 11 '15 at 11:43
• I am not quite sure how i would be able to fit this into one tabular-enivoronement as of the indentation of the second line and following. – foobar Nov 11 '15 at 11:47
• This question is strongly related to that one on BNF grammar, but egreg's solution is much more simpler (at last to me) than what was proposed earlier. – Clément Nov 11 '15 at 15:13
• I would argue it is related, however not useful to the specific problem. There are - at least by skimming through the package's documentation - tight restrictions as to how a grammar looks that is typeset using the syntax package. The solution offered in the question you referenced does not address this issue and thus looks just like a grammar i had typeset before. Thus, i am glad egreg posted such a wonderful answer. – foobar Nov 11 '15 at 16:21
• Let me rephrase my comment as "I'd like the expression BNF to appear in that post". When I tried to display BNF grammar nicely with latex, I encountered the same difficulties as you, and felt the packages mentioned in that topic were not very helpful. – Clément Nov 11 '15 at 16:34

This seems to replicate your example:

\documentclass{article}
\usepackage{mathtools,array}

\newenvironment{grammar}[2]
{\begin{tabular}{@{\qquad}>{$}l<{$}@{\qquad}l@{}}
\multicolumn{1}{@{}l@{}}{$#1$}&\multicolumn{1}{l@{}}{\hspace{-2em}#2}\\}
{\end{tabular}}

\begin{document}

\begin{grammar}{P,Q,R\Coloneqq}{processes (or plain processes)}
\mathbf{0}                        & null process \\
P\mid Q                           & parallel composition \\
!P                                & replication \\
\nu n.P                           & name restriction (new'') \\
\text{if $M=N$ then $P$ else $Q$} & conditional \\
u(x).P                            & message input \\
\bar{u}(N).P                      & message output
\end{grammar}

\end{document}


• Thank you very much. Any easy way to get this centered and to add a little more space to top and bottom? – foobar Nov 11 '15 at 12:13
• @foobar Just place the environment between $ and $. – egreg Nov 11 '15 at 12:53