12

I use endfloat to move all figures and tables to the end of my document. I want to apply the same to other floats that I have in my document, for example listing environments.

Is there some hack that I can use to make endfloat work for them or can I use something else?

EDIT

According to section 8.3 of the endfloat package documentation,

The trick is using the command

\DeclareDelayedFloat{⟨float⟩}[⟨file extension⟩]{⟨heading⟩}

which makes the new floating environment known to the endfloat package

but this does not seem to work for lstlisting floats.

5
  • 3
    I've added the information about lstlisting floats to your question, and deleted my answer. A question with no answers is more likely to draw attention from someone who actually knows what they are doing, unlike me. Apr 23, 2014 at 13:34
  • 1
    Perhaps, it is possible to use the newfloat package to wrap lstlisting floats in order to make them float to the end in conjunction with endfloat? (See ctan.org/pkg/newfloat)
    – user31729
    Apr 23, 2014 at 13:38
  • @IanThompson: What did you say in your answer?
    – user31729
    May 1, 2014 at 8:30
  • @ChristianHupfer --- basically just what's in the question after 'EDIT'. May 1, 2014 at 13:16
  • @IanThompson: Well, that was indeed a very helpful edit.
    – user31729
    May 2, 2014 at 4:54

2 Answers 2

8
+100

I think, I have found a possible solution

First some explanation:

\DeclareDelayedFloat{⟨float⟩}[⟨file extension⟩]{⟨heading⟩}

This command from endfloat package prepares the package for floating environments other than standard figure or table, say for example

\DeclareDelayedFloat{exercise}{fexe}{List of Exercises}

It requires (see lines 350 and following of endfloat.sty) that there is a listofexercises command.

Well, this is the point where

\DeclaredDelayedFloat{lstlisting}{flol}{List of Listings}

fails:

There is no \listoflstlistings command ;-)

It is called lstlistoflistings instead. The clue is just to say

\newcommand\listoflstlistings}{\lstlistoflistings}%

\documentclass[12pt]{book}

\usepackage{listings}
\usepackage{blindtext} % Just for dummy content
% use \usepackage[nomarkers]{endfloat} to drop the markers in the text
\usepackage{endfloat}  

%%% \DeclareDelayedFloat looks for the \listoflstlistings command
%%% since it is assumed, that there is an associate command for the list of listings
%%% 'environment' lstlistings as given to \DeclareDelayedFloat ---> listoflstlistings.
%%% However, `listings` uses \lstlistoflistings, so define this new command
%%% to make \DeclareDelayedFloat happy. 
\newcommand{\listoflstlistings}{\lstlistoflistings}%

% Change `List of Listings` as third argument at will.
\DeclareDelayedFloat{lstlisting}[flol]{\textbf{List of Listings}}

\begin{document}

\chapter{First content}


\blindtext

\begin{lstlisting}[language=C,float=t,caption={Hello World - a famous example}]
#include <stdio.h>

int main(int argc, char **argv)
{
  printf("Hello World\n");
  return(EXIT_SUCCESS);
}
\end{lstlisting}

\blindtext

\begin{lstlisting}[language=C++,float=t,caption={Hello World - a famous example, again}]
#include <iostream>

int main(int argc, char **argv)
{
  cout << "Hello World" << std::endl;
  return(EXIT_SUCCESS);
}
\end{lstlisting}

\end{document}

enter image description here

And if you are now confused about \listof and \lstlisting, \listoflstlistings ... I know, what you mean ;-)

File versions:

Endfloat:

\def\filename{endfloat}
\def\fileversion{v2.5d}
\def\filedate{2011/12/25}
\def\docdate{2011/12/05}

Listings:

\def\filedate{2014/03/04}
\def\fileversion{1.5c}

So, listings is quite new.

4
  • When I tried this example, I got the error Runaway argument? ! File ended while scanning use of \efloat@xfloat. May 5, 2014 at 8:06
  • @IanThompson: I tried with latex/pdflatex/xelatex/lualatex, all run without errors. I use the latest TeXLive state 'just' before the recent freeze state. I will post the versions of packages.
    – user31729
    May 5, 2014 at 11:50
  • I updated my packages, but it still doesn't work. Since it works for you, and for @John, something must be wrong on my machine. No wonder I couldn't solve the problem! May 5, 2014 at 16:34
  • I have an old TeXLive setup on another computer, I can try to compile the code, but the error message has its origin probably from endfloat package. Thank you very much for awarding the bounty anyway.
    – user31729
    May 5, 2014 at 19:42
2

Christians solution is great when relying on the float commands of lstlisting.

But I wanted to use the caption and float package to change all of my floats. Tables, Figures, Listings the whole shebang.

So based on this and Christians brilliant find I came up with the following:

\documentclass[12pt]{scrbook}

\usepackage{listings}
\usepackage{blindtext} % Just for dummy content
\usepackage{endfloat}  
\usepackage{tocloft}
\usepackage{float}

%%% Declare new float environment for Listings
\newcommand{\listofsnippetname}{List of Listings}
\newlistof{snippet}{lol}{\listofsnippetname}
\newfloat{snippet}{thp}{lol}[chapter]
\floatname{snippet}{Listing}
\newcommand{\snippetautorefname}{Listing}
\renewcommand{\thesnippet}{\thechapter.\arabic{snippet}}

%%% \DeclareDelayedFloat looks for the \listofsnippets command
\newcommand{\listofsnippets}{\listofsnippet}

%%% This makes endfloat aware of the snippet floats
\DeclareDelayedFloat{snippet}[flol]{List of Listings}

\begin{document}

\chapter{First content}

\blindtext

\begin{snippet}
\begin{lstlisting}[language=C]
#include <stdio.h>

int main(int argc, char **argv)
{
  printf("Hello World\n");
    return(EXIT_SUCCESS);
    }
\end{lstlisting}
\caption[Hello World]{The usual example.}
\label{lst:helloworld}
\end{snippet}

\end{document}
3
  • You did not ask for floating of figures/tables ... that is already done by endfloat, therefore I omitted it in my solution. It addresses the listings problem only.
    – user31729
    May 4, 2014 at 15:31
  • Wasn't meant as criticism. My question was slightly ambiguous.
    – John
    May 4, 2014 at 17:21
  • I did not take it as criticism, I was rather confused whether I misinterpreted your question, after I read your answer. Good question, by the way!!!
    – user31729
    May 4, 2014 at 18:18

You must log in to answer this question.

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