This looks like a job for the listings
package, which not only will automatically do your line breaks, but will allow
- the
->
arrows to be rendered as proper TeX-ish arrows;
- unicode characters (e.g. greek characters) to be rendered by the TeX-ish mathematical equivalent;
- easy indentation and inline presentation of the grammatical rules;
and other aspects of control of the formatting.
Example Source.
\documentclass{article}
\usepackage{listings}
\lstset{
basicstyle=\itshape,
xleftmargin=3em,
literate={->}{$\rightarrow$}{2}
{α}{$\alpha$}{1}
{δ}{$\delta$}{1}
}
\begin{document}
\noindent Example of a simple grammar:
\begin{lstlisting}
S -> α | B
B -> C
C -> D | δ
\end{lstlisting}
Note that the rules ``\lstinline{S -> α | B}\,'' and
``\lstinline{C -> D | δ}\,'' involve terminals.
\end{document}
The various ways in which the formatting is controlled above are as follows:
- The
basicstyle=\itshape
key indicates that the characters are to be typeset in italics, as is typical for a mathematics environment.
- The
xleftmargin=3em
describes a left indentation to apply for a "displayed" listing in the lstlisting
environment.
- The
literate=...
instructions indicate that 'literate' ways to render various sequences of characters. For instance, the two characters ->
is with the corresponding arrow, and that for spacing purposes it should be given approximately two characters worth of horizontal space. (listings
tries to give each character the same amount of space, even if you do not use a monospaced typeface; the default is about 0.6em
.) The other entries describes how to typeset the unicode greek characters.
- It is possible also to use these features inline, using the
\lstinline
macro.
Result:
amsmath
package and thealign
environment, so\begin{align} S &\to a \mid B \\ B &\to C \\ C &\to D \mid d \end{align}
. I don't think you'll get around manual line breaks... You also might want to usealign*
or analigned
environment inside of anequation
environment, depending on your equation numbering needs.