4

I want to create a list of all listings. Listings itself allows to add captions and list them in a list using \lstlistoflistings, but they can not float. If I set up such a floating environment using floatrow they belong to a different list and the number of the caption is wrong. I would like to have the numbers correct in one numbering scheme and add both to the List of Listings.

The caption of the first one is "Listing 1: "

The caption of the second one is "Code 0.0.1: "

\documentclass{scrbook}

\usepackage{listings}
\usepackage{floatrow}
\DeclareNewFloatType{Code}%
{placement=tbhp,within=section,fileext=loc}

\begin{document}
\begin{lstlisting}[caption={LaTeX Listings}]
\begin{document}
\chapter{Introduction}
Some text ...
\chapter{Theory}
\end{document}
\end{lstlisting}

\begin{Code}[H]
\begin{lstlisting}[language={[Visual]Basic}]
Sub Main()
    MsgBox "Hallo Welt!"
End Sub
\end{lstlisting}
  \caption{Hello World in Visual Basic 6}
  \label{lstLaTeXLinesOfCode2}
\end{Code}

\lstlistoflistings
\listof{Code}{List of Code listings}

\end{document}
3

listings allows you to specify a float=<spec> key-value to allow the lstlisting to float so you don't need to make your own floating environment. Here, <spec> can any combination from htbp.

Floating lstlisting

\documentclass{scrbook}

\usepackage{listings}% http://ctan.org/pkg/listings
%\usepackage{floatrow}
%\DeclareNewFloatType{Code}%
%{placement=tbhp,within=section,fileext=loc}

\begin{document}

Here is some text.

\begin{lstlisting}[caption={LaTeX Listings},float=t]
\begin{document}
\chapter{Introduction}
Some text ...
\chapter{Theory}
\end{document}
\end{lstlisting}

Here is some more text.

%\begin{Code}[H]
\begin{lstlisting}[caption={Hello World in Visual Basic 6},label=lstLaTeXLinesOfCode2,language={[Visual]Basic},float=t]
Sub Main()
    MsgBox "Hallo Welt!"
End Sub
\end{lstlisting}
%  \caption{Hello World in Visual Basic 6}
%  \label{lstLaTeXLinesOfCode2}
%\end{Code}

Here is some more text, yet again.

\lstlistoflistings
%\listof{Code}{List of Code listings}

\end{document}​

See p 6 of the listings documentation.

  • This does not solve the wrong numbering in the floatrow code, but it provides the floating that I wanted. So in this case I do not need a new float. – Matthias Pospiech Jan 25 '12 at 19:35

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.