Place longer figures in appendix

I am writing a technical document that includes many source code listings. I have been asked to place longer listings in the appendix instead of inline. Is there a way to do this automatically? It is also important that references to figures in the appendix indicate where to find them.

• Thanks, I'm sure this question has been asked before but I'm not familiar enough with tex to find it. – everett1992 Aug 25 '13 at 3:51
• Are you talking about figures or listings? – egreg Aug 25 '13 at 14:23
• "It's supposed to be automatic, but you actually have to push this button." If you want to be able to swap them back and forth, put each in a separate file and \input where desired. – John Kormylo Aug 25 '13 at 18:02
• @egreg, I am using listings inside figures, but I don't think listings are relevant to the question, I would like to do the same thing for listings, graphics, or any element in a figure. – everett1992 Aug 25 '13 at 19:06

Some readers of my texts have shown some confusion with floats which interrupt text. So putting all figures to the end of a document (or chapter) is not a bad idea. Forcing an exact position of a float or figure is not always possible nor advised, compare How to influence the position of float environments like figure and table in LaTeX?.

But I like to keep the code of the figures, tables etc. next to its logical place in .tex-files.

The following MWE allows both. The magic happens in https://ctan.org/pkg/endfloat.

\documentclass[paper=a4,twoside]{book}

\usepackage{fontspec,polyglossia}
\setdefaultlanguage[variant=usmax]{english}

\usepackage{hyperref,cleveref}
\usepackage[nomarkers, nolists]{endfloat}

\newcommand{\apref}[1]{\cref{#1} on \cpageref{#1}}

\begin{document}

\chapter{Lorem foo}
Lorem ipsum, compare \apref{fig:1}. Lorem ipsum dolor sit, see \apref{fig:1,fig:2}.

\begin{figure}
\centering
\Huge FIGURE
\caption{figure1}
\label{fig:1}
\par
\end{figure}

\begin{figure}
\centering
\Huge FIGURE
\caption{figure2}
\label{fig:2}
\par
\end{figure}

\chapter{Lorem bar}
Some text \cref{fig:3,fig:4} on \cpageref{fig:3,fig:4}.

\begin{figure}
\centering
\Huge FIGURE
\caption{figure3}
\label{fig:3}
\par
\end{figure}

\begin{figure}
\centering
\Huge FIGURE
\caption{figure4}
\label{fig:4}
\par
\end{figure}

\appendix

\end{document}