TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I googled and found two packages:

  1. mdwtools
  2. bnf

Unfortunately, both of them have extremely poor documentation. There are only one to two examples for each one, which makes it hard to follow. Furthermore, none of them shows how to indent the grammar properly, rather than just setting up the environment to have a nice look. I wonder is there a major package for writing BNF, or we can just manually use tabbing to handle the indentation? Thank you.

share|improve this question
Personally, I would use listings package with some manual settings. – Leo Liu Aug 4 '11 at 10:42
Backus-Naur Form – Seamus Aug 4 '11 at 11:10
Quick texdoc bnf revealed that there's also a ConTeXt module for that (just in case you are interested;)). – mbork Aug 4 '11 at 12:33
The highlight tool by Andre Simon can export to LaTeX. And I see in its documentation that it supports ABNF (andre-simon.de/doku/highlight/en/highlight_langs.html). I hope there would be a package using it as a back-end (as is minted to pygments) – John Kirollos Aug 4 '11 at 13:17
Are you sure you're looking at the right documentation? The documentation for syntax package (part of mdwtools) is 38 pages, which is fairly substantial. There are three lengths that control spacing within the grammars themselves. Perhaps you could post an example of what you've tried and what doesn't work. – Alan Munn Aug 4 '11 at 13:39
up vote 31 down vote accepted

The syntax package from the mdwtools bundle has simple ways of typesetting BNF grammars, with some reasonable controls over how the rules are formatted.

The space between grammar rules can be set with the \grammarparsep length. This is a rubber length, and defaults to 8pt plus 1pt minus 1pt.

The distance between the left hand side of the production rule and the right hand side is controlled by the \grammarindent length. Increasing this value will move the right hand side of the rule further to the right.

Both of these lengths can be set using the standard LaTeX \setlength command.

The \grammarlabel command controls how the left hand side of the rule is set relative to the production operator. Its default definition is:


where #1 is the nonterminal and #2 is the production operator.

Here's a sample document that plays with the two length values. I've purposefully made the values large so that the effect will be seen. I wouldn't change the definition of \grammarlabel if I were you.


\paragraph{Default settings}

<statement> ::= <ident> `=' <expr> 
\alt `for' <ident> `=' <expr> `to' <expr> `do' <statement> 
\alt `{' <stat-list> `}' 
\alt <empty> 

<stat-list> ::= <statement> `;' <stat-list> | <statement> 

\paragraph{Increase the two lengths}
\setlength{\grammarparsep}{20pt plus 1pt minus 1pt} % increase separation between rules
\setlength{\grammarindent}{12em} % increase separation between LHS/RHS 


<statement> ::= <ident> `=' <expr> 
\alt `for' <ident> `=' <expr> `to' <expr> `do' <statement> 
\alt `{' <stat-list> `}' 
\alt <empty> 

<stat-list> ::= <statement> `;' <stat-list> | <statement> 


output of code

share|improve this answer
Many thanks for your help. I really appreciated it. As you can see, I'm a novice so I honestly didn't know that the three length options can be set with \setlength(). By the way, the mentioned the lhs must be of the form <xxx>, I wonder is there a way to get rid of <>. Once again, thank you. – Chan Aug 5 '11 at 4:39
By the way, besides those two packages, do you know any other alternatives? – Chan Aug 5 '11 at 4:59
The third length isn't a length, its a macro, and needs to be changed with \renewcommand (But I wouldn't recommend changing it.) For the delimeters, see section 1.7 of the manual. You can change the non-terminal < and > with \renewcommand{\syntleft}{} and \renewcommand{\syntright}{}, respectively. (I don't know any alternative packages.) – Alan Munn Aug 5 '11 at 5:15
I see. Thank you :(. – Chan Aug 5 '11 at 5:29
I wonder is there a way to indent the production line, so it can be center aligned? – Chan Aug 21 '11 at 0:00

Perhaps you find an alternative in Syntax diagrams (or railroad diagrams). They represent a graphical alternative to Backus–Naur Form.

For LaTeX you can use the rail-package. It contains a LaTeX-style and a programm (compiled exe or the C-Sources). An example of a document with the package: example for rail-diagramm

Remark: The rail-package contains an old exe. If you have problems to use it, there are German hints how to compile the sources on Windows 7 (64-Bit). The results (adapted makefile and the compiled version) are also available on the website: http://www.karsten-brodmann.de/downloads/Rail.zip.

There is also syngen A tool for generating syntax diagrams from BNF (I have no experience with the package).

share|improve this answer

There is an new package on CTAN:


The package typesets Backus-Naur Form (BNF) definitions. It creates aligned lists of productions, with numbers if required. It can also print in-line BNF expressions using math mode.

share|improve this answer
I've found this package somewhat less finicky than syntax package. Thanks for the reference. – CyberFonic Oct 7 '15 at 5:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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