Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
Welcome to TeX.SX. –  Claudio Fiandrino Feb 4 '13 at 19:42

1 Answer 1

up vote 5 down vote accepted

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

share|improve this answer

Your Answer

 
discard

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.