# [Tex/LaTex] LaTeX equivalent of ConTeXt buffers

contextprogrammingtikz-pgf

While creating a series of graphics (using TikZ) in ConTeXt, I frequently use ConTeXt's buffer to "copy-paste" code. Is there a LaTeX package that implements this feature?

Since most LaTeX users may not know what buffers do, let me explain that. Suppose I want to draw a graph in steps: in the first step, only draw the nodes, in the second step draw the edges, in the third step highlight a part of the graph.

In ConTeXt, I would do this as follows:

\startbuffer[nodes]
% tikz code for drawing nodes
\stopbuffer

\startbuffer[edges]
% tikz code for drawing edges
\stopbuffer

\startbuffer[highlight]
% tikz code for highlight a part
\stopbuffer

\starttext
\startTEXpage \getbuffer[nodes] \stopTEXpage
\startTEXpage \getbuffer[nodes,edges] \stopTEXpage
\startTEXpage \getbuffer[nodes,edges,highlight] \stopTEXpage
\stoptext


This will give me a pdf file with three pages, that build up the graph in steps. How do I do the same in LaTeX?

I can use the preview package to extract TikZ pictures on individual pages, but don't know how to build the graph in steps. One option is to use beamer overlays to create the graph in steps, but creating a presentation for each figure appears to be an overkill. I can just copy paste code, but that is difficult to maintain in the long run. Any suggestions?

### Edit

The proposed solution of storing the contents in a macro does not always work. For example

\documentclass{minimal}
\usepackage{tikz}

\def\NODES
{\matrix
{
\node (a) {$a$} ; & \node (b) {$b$} ; \\
} ;
}

\begin{document}
\begin{tikzpicture}
\NODES
\end{tikzpicture}
\end{document}


gives

! Package pgfbasematrix Error: Single ampersand used with wrong catcode.


Any other suggestions?

Based on what buffers do, the filecontents environment combined with \input (for \getbuffer) and \verbatiminput (for \typebuffer) comes closest, I think.