9

I'd like to place some of my floats (specifically code listings) in my documents in a section at the end, without disturbing the numbering. Currently, I have them numbered within sections, so in section 1 all listings are called Program 1.1, Program 1.2 etc.

Is there a way to create a section at the end (like an appendix) where all the program floats go, without changing their numbering? Ideally, I'd like to have

Section 1
Some text in Section 1, and a reference to Program 1.1 and 1.2.

Section 2
Apparently Program 2.1 was the only code shown in this section.

...

Appendix A
Program 1.1:

for i = 1:n
    doStuff();
end

Program 1.2:

% This program doesn't do anything

Etc...

Update:
Before anyone shoots me for not showing exactly how the programs are defined, here's how it works:

% In the preamble
\usepackage{listings}
\usepackage{float}
\newfloat{program}{tbphH}{lop}
\floatname{program}{Program}

\newcommand{\codefrom}[2][Matlab]
{
\begin{program}[hbt]
    \lstinputlisting[language=#1]{#2}
    \caption{#2}
    \label{#2}
\end{program}
}

% In the document
\codefrom{alocalfile.m}
4
  • I'm not sure, but this is perhaps relevant, how did you manage to number your programs by section using listings? Also, do you want to move all of the program listings to the end, or only some of them? Sep 28, 2010 at 12:26
  • @Juan: I have defined a new float environment using the floats package, but my question is not so much related to what kind of float it is than how to move them around. Sep 28, 2010 at 13:50
  • No, the question was more about how do you get the numbering per section, and also if you want all or some of them in the end. I'm thinking of a possible solution by hacking into the counters and temporarily make each appendix number as if it were in a previous section. Sep 28, 2010 at 15:23
  • Just got some minutes and posted a possible answer below. Sep 28, 2010 at 17:19

3 Answers 3

3

So this is my candidate solution which basically consists in manually moving all the floats to the end, and then temporarily hack into the section counter in order to show the desired numbers.

\documentclass{article}

\usepackage{float}
\newfloat{program}{tbphH}{lop}[section]
\floatname{program}{Program}

\newcounter{savesection}
\newenvironment{fromsection}[1]{%
  \setcounter{savesection}{\value{section}}%
  \setcounter{section}{#1}%
  \renewcommand{\thesection}{\arabic{section}}}
  {\setcounter{section}{\value{savesection}}}

\newcommand{\codefrom}[1]{%
\begin{program}[hbt]
    \caption{#1}
    \label{#1}
\end{program}}

\begin{document}

\section{Introduction}
Please see Program~\ref{dostuff} and \ref{donothing}.

\section{Another thing}
Also see Program~\ref{onlyone}.

\appendix
\section{Appendix}

\begin{fromsection}{1}
\codefrom{dostuff}
\codefrom{donothing}
\end{fromsection}

\begin{fromsection}{2}
\codefrom{onlyone}
\end{fromsection}

\section{Yet another appendix}

\end{document}

As the floats don't really need to “float” anymore, I would furthermore suggest to change their placement simply to “H” rather than “hbt”. Also I have no idea about the terrible things that this could cause if you're trying to use this together with hyperref.

1
  • I've tested this a little now, and it seems to work excellently with the hyperref package as well. Thanks! Sep 28, 2010 at 22:15
0

Here's a bit of a hack that might be suitable for what you need. I hope you don't mind the floats appearing on their own pages. See the FAQ answer on floats for something of an explanation here. The code is uncommented but you should be able to get the gist of it. Also, be aware that you won't be able to have more than 36 floats using this technique.

\documentclass{article}
\usepackage{morefloats}
\usepackage{chngcntr}
\counterwithin{figure}{section}

\newcommand\allowfloats{%
    \renewcommand{\topfraction}{.85}
    \renewcommand{\bottomfraction}{.7}
    \renewcommand{\textfraction}{.15}
    \renewcommand{\floatpagefraction}{.66}
    \setcounter{topnumber}{0}
    \setcounter{bottomnumber}{0}
    \setcounter{totalnumber}{0}
}

\newcommand\dontallowfloats{%
    \renewcommand{\topfraction}{.01}
    \renewcommand{\bottomfraction}{.01}
    \renewcommand{\textfraction}{.99}
    \renewcommand{\floatpagefraction}{.99}
    \setcounter{topnumber}{9}
    \setcounter{bottomnumber}{9}
    \setcounter{totalnumber}{20}
}

\usepackage{lipsum}
\newcommand\textandfloat{%
  \lipsum[1-2]
  \par
  \begin{figure}[p]
    \centering\rule{5cm}{5cm}
    \caption{Black square.}
  \end{figure}
}

\begin{document}

\dontallowfloats
\section{Begin document}
\textandfloat\textandfloat
\textandfloat\textandfloat
\section{Middle}
\textandfloat\textandfloat
\textandfloat\textandfloat
\section{End document}
\textandfloat\textandfloat
\textandfloat\textandfloat

\allowfloats
\section{Floats}
Here are all the floats.

\end{document}
3
  • 2
    I can't believe that somebody arbitrarily decided that "36 floats ought to be enough for everybody". LaTeX is full of surprises.. Sep 28, 2010 at 19:04
  • That's 36 floats (18 without morefloat) that haven't yet been placed - big difference. Considering there were only 256 registers, total, at the time, and memory constraints were very real, the limit of 18 reserved slots is very sensible. Sep 29, 2010 at 2:13
  • (With eTeX we now have 2^16 instead of 2^8 slots and worrying about available registers isn't really an issue.) Sep 29, 2010 at 4:48
0

Juan A. Navarro, how can I modify your solution so that it works for figures, inclusive a List of Figures? I have tried the package endfloat for my figures but have encountered certain issues; that's why I am looking for an alternative way of putting all my figures in a separate Chapter in the end of my document but before the supplement.

4
  • Please stop posting comments as if they were answers. Jan 8, 2022 at 18:21
  • Thanks for your feedback, José! Please, tell me what should I do if I have questions regarding a post?
    – KMFWeb
    Jan 8, 2022 at 18:34
  • Post questions and answers and earn reputation. When your reputation reaches 50, you can start commenting posts. Jan 8, 2022 at 18:37
  • 1
    Thanks, José! I'll post my question to Juan A. Navarro as a new question then.
    – KMFWeb
    Jan 8, 2022 at 18:41

You must log in to answer this question.

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