7

I have a grammar in my document using the package syntax like described in the answer to How to type a Backus-Naur form in LaTeX.

Now I want it to be treated like the listings I have in my document. This means I want to add a label, a caption (that says "Listing 42" in the beginning) and it should appear in \lstlistoflistings. Is there a way to achive this?

A strongly simplified example of my document so far:

\documentclass[12pt,tablecaptionabove]{scrartcl}
\usepackage[final]{listings}
\usepackage[nounderscore]{syntax}
\begin{document}
   \lstlistoflistings

    some Text

    \begin{lstlisting}[caption={A listing},label=myListing]
        myListingCode
    \end{lstlisting}

    some Text

    \begin{grammar}
        <S> ::= someTerminal
    \end{grammar}
\end{document}
6

You can define a new environment that calls the caption type to be the same as for a lstlisting, with the help of the caption package:

\documentclass[12pt,tablecaptionabove]{scrartcl}
\usepackage[final]{listings}
\usepackage[nounderscore]{syntax}
\usepackage{caption}

\usepackage{lipsum} % just to provide mock text

\newenvironment{BNF}
  {\captionsetup{type=lstlisting}}
  {}

\begin{document}
\lstlistoflistings

\section{A section}

\lipsum[1]

\begin{lstlisting}[caption={A listing},label=myListing]
myListingCode
\end{lstlisting}

\lipsum[2]

\begin{BNF}
\caption{A grammar}
\begin{grammar}
   <S> ::= someTerminal
\end{grammar}
\end{BNF}

\lipsum[3]

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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