3

For my document I need to create several 'listof' pages that all must be formatted in a uniform way. So the standard feature support from lets say the listings package is insufficient. Therefore I use the document class memoir and use its feature newlistof, newfloat & newlistentry. So far so good, but consider the following code:

\begin{ftlisting} %the float to be shown in my listoflistings
\lstinputlisting{code.file}
\caption{Code}
\label{lst:code}
\end{ftlisting}

This template compiles and behaves perfectly unless the \lstinputlisting{} command includes a file that needs to be split up into several pages. Suddenly either the compilation fails or the file is not split up over several pages. I found out that this problem originates from the fact that floats cannot span more than one page. When I use the \lstinputlisting command on its own, it splits up the file correctly in several pages. I know that there exists a partial solution consisting of manually cutting up the file by passing a firstline/lastline option to the \lstinputlisting command but this solution only works for the listings package. Is there a better workaround this issue? A more complete MWE:

\documentclass{memoir}
\usepackage{listings}

\lstset{language=Java,nolol}

\newlistof{listoflistings}{lol}{List of \lstlistingname{}s}
\newfloat[chapter]{ftlisting}{lol}{\lstlistingname}
\newlistentry[chapter]{ftlisting}{lol}{0}

\begin{document}
\begin{ftlisting}
\lstinputlisting{code.java}
\caption{Code}
\label{lst:code}
\end{ftlisting}
\end{document}
3
  • But the listings package already offers a way to produce a list: \lstlistoflistings; you don't need a new float. Simply use \begin{lstlisting}[caption={text here}]... \end{lstlisting}; you can even selectively decide which environments should be treated as floats (thus avoiding page breaks) by adding the float key: \begin{lstlisting}[float, caption={text here}]... \end{lstlisting}. Feb 1, 2014 at 22:49
  • the class memoir is relevant since I use features offered by it (newlistof etc.). As explained in the question I want to format all lists in exactly the same way and listings is just one of them. I also define lists for own environments and for definitions & theorems. Therefore I refrain from using the standard listings provided lstlistoflistings command. Feb 1, 2014 at 23:30
  • Ah, now I see, and I've provided an answer. Perhaps you could add your comment as an edit to your question, so it becomes clear for everyone why this question is different and why you refrain from using the mechanisms provided by listings. Feb 2, 2014 at 3:56

1 Answer 1

3

floating objects don't admit page breaks. You can use memoir's \newfixedcaption to define a captioning command to be used for your listings outside a floating environment; define an auxiliary (non-floating) environment to contain listings that should admit page breaks and use the new captioning command inside this environment to provide captions. In the following example, myenv is this containing environment (defined using a list, so page breaks are allowed).

\documentclass[a5paper]{memoir}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{beramono}
\usepackage{filecontents}

\lstset{
  basicstyle=\ttfamily,
  breaklines=true,
  columns=fullflexible
}

\begin{filecontents*}{code.java}
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
No file 2014-01-30.aux.
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./2014-01-30.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsf
onts/cm/cmr10.pfb>
Output written on 2014-01-30.pdf (1 page, 8170 bytes).
SyncTeX written on 2014-01-30.synctex.gz.
Transcript written on 2014-01-30.log.
\end{filecontents*}

\lstset{language=Java,nolol}

\newlistof{listoflistings}{lol}{List of \lstlistingname{}s}
\newfloat[chapter]{ftlisting}{lol}{\lstlistingname}
\newlistentry[chapter]{ftlisting}{lol}{0}
\newfixedcaption{\lstcaption}{ftlisting}

\newenvironment{myenv}
  {\list{}{%
    \leftmargin=0pt
    \topsep=6pt
    \listparindent=\parindent
    \itemindent=\parindent
  }\item\relax}
  {\endlist}

\begin{document}

\listoflistings

\begin{ftlisting}[!ht]
\begin{lstlisting}
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./2014-01-30.tex
LaTeX2e <2011/06/27>
\end{lstlisting}
\caption{Code in a floating environment}
\label{lst:fcode}
\end{ftlisting}

\begin{myenv}
\lstinputlisting{code.java}
\lstcaption{Code in a non floating environment}
\label{lst:nfcode}
\end{myenv}

\end{document}

enter image description here

The filecontents package and environment were used just for the example, to be able to have a convenient file code.java; the a5paper option again was used only for the example; these settings are not essential to the solution.

1
  • Thx, this is exacly what I wanted. Now you can chose between a floated one page environment and a fixed multipage caption both ending up in the same listof. Feb 2, 2014 at 16:25

You must log in to answer this question.

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