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'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}
share|improve this question
    
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? –  Juan A. Navarro Sep 28 '10 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. –  Tomas Lycken Sep 28 '10 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. –  Juan A. Navarro Sep 28 '10 at 15:23
    
Just got some minutes and posted a possible answer below. –  Juan A. Navarro Sep 28 '10 at 17:19
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
I've tested this a little now, and it seems to work excellently with the hyperref package as well. Thanks! –  Tomas Lycken Sep 28 '10 at 22:15
add comment

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}
share|improve this answer
2  
I can't believe that somebody arbitrarily decided that "36 floats ought to be enough for everybody". LaTeX is full of surprises.. –  Martin Tapankov Sep 28 '10 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. –  Will Robertson Sep 29 '10 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.) –  Will Robertson Sep 29 '10 at 4:48
add comment

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.