7

I am currently writing context-free grammars in Latex, I have one production in each line, e.g.

S -> a | B
B -> C
C -> D | d

Currently I am using the math mode for this and a manual line break. Is there a better environment for my case? One where I e.g. don't need manual line breaks? Thanks :-)

1
  • 1
    Please add an MWE so we know what you're using at the moment. For this particular case, I'd recommend the amsmath package and the align 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 use align* or an aligned environment inside of an equation environment, depending on your equation numbering needs. Commented Mar 13, 2013 at 15:43

1 Answer 1

8

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:

Sample CFG specification using lstlistings environment

5
  • wow, that's great, thank you! Do you know how I can indent the complete lstlisting block? Is it possible to disable the justification on the | sign? Several | vertically don't look that good, as it's quite close to a line. Commented Mar 13, 2013 at 20:51
  • hmm and how would one write an \alpha in the lstlisting? Commented Mar 13, 2013 at 20:54
  • found the answers to my questions and edited Niel's post :-) Commented Mar 13, 2013 at 21:06
  • @stefan.at.wpf: if you want to avoid the almost-solid-line, you can increase the vertical spacing in the grammar using basicstyle=\itshape\setstretch{1.3} and the setspace package. Commented Mar 14, 2013 at 16:02
  • This is nice but it doesn't seem to address potential alignment needs, for example, it's common to have things like | xxx on the next line aligned with \leftarrow from previous lines.
    – Jack
    Commented Jan 27, 2021 at 4:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .