Using the same picture over and over?

Suppose I have a complex figure made ​​with TikZ. How do I use it several times without having to repeat all the commands?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}

Base figure

\begin{tikzpicture}
\draw[blue] circle (2);
\foreach \i in {0,10,...,350}
\draw[blue] (\i:1.9) -- (\i:2.1);
\end{tikzpicture}

Using base figure.

\begin{tikzpicture}
\draw[blue] circle (2);
\foreach \i in {0,10,...,350}
\draw[blue] (\i:1.9) -- (\i:2.1);
\foreach \i in {0,10,...,350}
\node at (\i:2.7) {\tiny $\i^\circ$};
\end{tikzpicture}

Using base figure.

\begin{tikzpicture}
\draw[blue] circle (2);
\foreach \i in {0,10,...,350}
\draw[blue] (\i:1.9) -- (\i:2.1);
\foreach \i in {0,10,...,350}
\draw (0,0) -- (\i:2);
\end{tikzpicture}

\end{document}


-
Somewhat related? tex.stackexchange.com/questions/5338/… –  Torbjørn T. Nov 15 '11 at 7:33
A very complex picture could also be saved in a box register and placed using a node into the other pictures. This has a speed benefit. If you use my storebox package the picture is also only stored once in the output PDF. –  Martin Scharrer Nov 15 '11 at 8:02

You could define a macro that constructs the base figure, and supply an optional argument that is added to the base figure. That allows you to use it as-is, but optionally add stuff. This abstraction promotes consistency and allows for easy changes in the future:

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\begin{document}

\newcommand{\myfigure}[1][]{%
\begin{tikzpicture}
\draw[blue] circle (2);
\foreach \i in {0,10,...,350}
\draw[blue] (\i:1.9) -- (\i:2.1);
#1
\end{tikzpicture}%
}

Base figure

\myfigure

Using base figure.

\myfigure[%
\foreach \i in {0,10,...,350}
\node at (\i:2.7) {\tiny $\i^\circ$};%
]

Using base figure.

\myfigure[%
\foreach \i in {0,10,...,350}
\draw (0,0) -- (\i:2);%
]
\end{document}


You use \myfigure for your base figure and \myfigure[<addition>] for adding <addition> to the base figure.

-

Enclose it in a newcommand

\newcommand{\bluecircle}{
\draw[blue] circle (2);
\foreach \i in {0,10,...,350}
\draw[blue] (\i:1.9) -- (\i:2.1);
}


Now your final figure would be:

\begin{tikzpicture}
\bluecircle
\foreach \i in {0,10,...,350}
\draw (0,0) -- (\i:2);
\end{tikzpicture}

-
\newcommand. Of course. Thanks. –  Regis da Silva Nov 15 '11 at 4:07
It seems to be my answer to every question. I should probably learn something else about LaTeX... –  qubyte Nov 15 '11 at 12:44