# Print document portions in specific order

I'm using knit within R to create my Latex document. The document is a report that involves data analysis. My problem is that I want to report the outcomes of the calculations (which I only know after the document is processed) in the Executive Summary (which is naturally at the start of the document). I could do all the calculations before the ES and then report them in the body but that creates its own inconveniences.

So, I'm looking for something like (edited; original example was needlessly general):

\begin{part}{first}
\chapter{Introduction}
...
\chapter{Data}
...
\chapter{Analysis}
...
\end{part}{first}

\chapter*{Executive Summary}
...

\print{first}


Any thoughts? Could I capture the first part in an environment and print it later? Thanks all!

• Welcome to TeX.SX! In your document the summary seems to come after the calculations, so doing them first and using the result later would be fine. The usual (and actually only) approach in TeX to bring information from later parts of the document to the front is to write the information to an auxiliary file and read them back in on a second compilation run. Depending on how complex that data is, you could either write it to the standard .aux file or create your own auxiliary file. – siracusa Jul 5 '19 at 0:43
• See also this or this question for more information – siracusa Jul 5 '19 at 0:43
• Thanks! What if instead of bringing later parts to the front, I'd be happy to move earlier parts to the back? – Andrew Robinson Jul 5 '19 at 2:02
• You can define new macros, set integer/dimension/token registers etc. in the front part and access them later in the document again. For example, if you use \newcommand\myresults{some text here} to define a new macro \myresults without printing the text. You can then use \myresults wherever you want in your document (not before the definition, of course) to actually print the text. There may be better solutions, depending on the type of data you are trying to print. – siracusa Jul 5 '19 at 4:34
• Ah, that seems promising - thanks! I'm trying to print quite a lengthy portion of a document, though - it will have chapter headings, tables, figures, links, etc. – Andrew Robinson Jul 5 '19 at 4:42

If you want to define parts of your document in one place and use them in another, the simplest solution is to define a new macro that stores the text as its replacement text. In a later part of the document you can then use that macro to actually print the text.

A small example:

\documentclass{article}

\begin{document}
\newcommand\myresults{
\section{Data and Analysis}
Data goes here ...
}

\section{Executive Summary}
Summary goes here ...

\myresults
\end{document}