# How to render some macro content twice?

I am looking for a three-column layout, in which two columns contain two seperate lists, and the third column a summary of the two lists.

I am under the impression that there is no "native" way to do this in LaTeX (and haven't found a package that appears to do the trick), so in a first step I have prepared a custom environment and a custom item entry macro:

\documentclass{scrbook}
\usepackage{multicol}
\newenvironment{MyList}[1]{
\subsection{#1}
\begin{enumerate}
}{
\end{enumerate}
}
\newcommand{\myEntry}[2]{\item \textbf{#1} -- #2}
\begin{document}
\begin{multicols}{3}
\begin{MyList}{ListOne}
\myEntry{OneOne}{Some text regarding item OneOne}
\myEntry{OneTwo}{Some text regarding item OneTwo}
\end{MyList}
\columnbreak
\begin{MyList}{ListTwo}
\myEntry{TwoOne}{Some text regarding item TwoOne}
\myEntry{TwoTwo}{Some text regarding item TwoTwo}
\end{MyList}
\columnbreak
\end{multicols}
\end{document}


But that does not even take me half of the way. I need to render the contents of my macros twice: Once in the summary column, once in the individual list column. Not only that, but on the opposite page I would have to do it the other way around (i.e., individual column first, summary column second). It's pretty clear that no simple replace-this-with-that transformation can do that, and that some "programming" is required, but I have no idea if there is some way to do this in LaTeX.

I could probably write some Perl script to do it via a preprocessing step, but boy does that feel hack-ish...

If there is a way to do this in LaTeX, could someone please give me some pointers what to look for?

-
One way to repeat stuff is to save it as a macro. So change your \myEntry to include \def\tempmacro{#1} so later you can use \tempmacro to repeat the stuff. This won't work because you want to repeat it after having gone through the whole list. So you'd need to namespace all your macros properly. If you could do things as a tabular instead, then this method would be easier. – Seamus Aug 19 '11 at 10:57
Otherwise, you can write things to an auxiliary file and print that back out as the third column. – Seamus Aug 19 '11 at 11:03
@Seamus: Would you care to turn this into an answer? I think this is the right way to go but would have to play Q&A for a round or two on this (and would like to give it the "checkmark of thanks" if it floats my boat). – DevSolar Aug 19 '11 at 11:06

Here's how to do this with a new auxiliary file:

\documentclass{scrbook}
\usepackage{multicol}
\newenvironment{MyList}[1]{
\noexpand\begin{enumerate}
}
\subsection{#1}
\begin{enumerate}
}{
\end{enumerate}
}

\newcommand{\myEntry}[2]{%
\item \textbf{#1} -- #2
}

\usepackage{newfile}
\newoutputstream{summary}
\newcommand\dosummary{%
\closeoutputstream{summary}
\subsection{Summary}
\input{\jobname.sum}
}
\openoutputfile{\jobname.sum}{summary}
\begin{document}
\begin{multicols}{3}
\begin{MyList}{ListOne}
\myEntry{OneOne}{Some text regarding item OneOne}
\myEntry{OneTwo}{Some text regarding item OneTwo}
\end{MyList}
\columnbreak
\begin{MyList}{ListTwo}
\myEntry{TwoOne}{Some text regarding item TwoOne}
\myEntry{TwoTwo}{Some text regarding item TwoTwo}
\end{MyList}
\columnbreak
\dosummary
\end{multicols}

\end{document}


The important stuff is the newfile package which makes writing to files easier, and the \addtostream command which actually adds stuff to the auxiliary file. You have to be careful about macros being expanded when writing things to files, hence the \noexpands in front of the \items.

I've modified the MyList environment and the \myEntry macro to write the relevant things to the auxiliary file which is then printed out by the \dosummary macro. Find out more about auxiliary files here.

-
Might need compiling twice, by the way. – Seamus Aug 19 '11 at 11:32
That's definitely the hint I've been looking for. It will need some additional tinkering (as my example was much dumbed down for simplicity's sake), but I can "see the path" now (even without additional Q&A). Thanks (again)! – DevSolar Aug 19 '11 at 14:02
One question though: Will this auxiliary file be included in the same pass it is created? I.e., could this process be repeated (and the summary output stream be "recycled"), or would I have to create a new output stream (possibly numbered with help of \thesubsection or somesuch) for every instance of MyList? – DevSolar Aug 19 '11 at 15:39
Nevermind. Tested it, it works just fine. – DevSolar Aug 19 '11 at 17:18