Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

enter image description here

share|improve this question
    
Somewhat related? tex.stackexchange.com/questions/5338/… –  Torbjørn T. Nov 15 '11 at 7:33
4  
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

2 Answers 2

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:

enter image description here

\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.

share|improve this answer

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}
share|improve this answer
    
\newcommand. Of course. Thanks. –  Regis da Silva Nov 15 '11 at 4:07
4  
It seems to be my answer to every question. I should probably learn something else about LaTeX... –  qubyte Nov 15 '11 at 12:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.