Place section later in document than defined

Is is possible to have a section appear later in the document than where it is defined?

\documentclass{article}

\begin{document}
\section{Intro}
This section appears first.

\section{Arcane details}
This section should not appear here, but later.

\section{Interesting bits}
That people want to see.

\appendix
% The Arcane details section should appear here.

\end{document}


In this simple example, I don't want the second section I defined (Arcane details) to appear as the second section, but rather to be an appendix at the end of the document.

I know I could just move that section to the location I want it, but the case I want to do this for is for an automatically generated document (specifically Sweave from R) and the section I want to appear later involves calculations that need to be done before the rest of the document can be generated.

-
Do you only have one of these sections? Are you able to modify the output code so that you can write something before/after this section? –  Werner Jan 9 '12 at 16:56
My current problem is only for one section (and all of its subsections). I have full control of the LaTeX part of the code as well. –  Brian Diggs Jan 9 '12 at 17:07

2 Answers

If you have one such section (more specifically, a continuous stretch of code), you could write that particular content to a file, and read it in later. File writing is supported by the filecontents package. Here's a minimal example:

\documentclass{article}
\usepackage{filecontents}% http://ctan.org/pkg/filecontents
\begin{document}
\section{Intro}
This section appears first.

\begin{filecontents*}{arcane.tex}
\section{Arcane details}
This section should not appear here, but later.
\end{filecontents*}

\section{Interesting bits}
That people want to see.

\appendix
\input{arcane}% The Arcane details section appears here.

\end{document}


that produces

Within the filecontents (starred or not) environment, everything is dumped to the file verbatim, while \input reads the contents in as if it was placed in the original source at that location. This approach should work for even large and complex constructions within your section(s). You should use a different file name for each "delayed usage", otherwise the file will be overwritten (if you're using filecontents*).

If you're interested in literally doing a cut-and-paste of the section (keeping the section number and removing it from the sequence within the document's main contents) more work is required, of course.

-
I do not want a literal cut-and-paste (keeping the numbering as if it were in the original location). I want it numbered according to the new location. –  Brian Diggs Jan 9 '12 at 22:40

You want something like this:

\documentclass{article}

\begin{document}
\section{Intro}
This section appears first.

\def\arcanesection{%
\section{Arcane details}
This section should not appear here, but later.
}

\section{Interesting bits}
That people want to see.

\appendix
% The Arcane details section should appear here.
\arcanesection
\end{document}

-
This solution has the advantage that it does not require creating (and naming) and external file, but I will always be able to write a file out. Thank you for showing me an alternative approach. –  Brian Diggs Jan 9 '12 at 22:42