5

I'm doing a step-by-step tutorial, and need to repeat the base tikz picture while adding new paths. I'm trying to avoid copy/paste. I've looked around, tried using macros, saveboxes and tikz pic command but I can't make it work (or even compile).

I'm using subcaptions in the original, but removed here since I don't think it's relevant.

(Edit: maybe something like what it is used in beamer would be ideal?)

With savebox:

\documentclass[10pt,a4paper,twoside]{report}
%---------------------------------- tikz ---------------------------------------
\usepackage{tikz}
\usetikzlibrary{positioning,chains,fit,shapes,calc,arrows,patterns,external,shapes.callouts,graphs,decorations.pathreplacing}

\usepackage{fixltx2e} 

\newsavebox{\myborder}
\savebox{\myborder}{%
\begin{tikzpicture}
    \matrix[row sep=10mm,column sep=10mm] {
        % First row:
        \node (a)  {a}; & \node (e) {e}\\
        % Second row:
        \node (b)  {b}; & \node (d) {d} \\
        % Third row:
        \node (c)  {c};  & \node (z) {z} \\
        };
\end{tikzpicture}}

\begin{document}

% align edges

\begin{tikzpicture}
{\usebox{\myborder}}
 \path
          (1) edge  (b); 
\end{tikzpicture}


\begin{tikzpicture}
{\usebox{\myborder}}
   \path
          (a) edge  (b) 
          (a) edge  (c);
\end{tikzpicture}

\end{document}

With tikz pic

    \documentclass[10pt,a4paper,twoside]{report}
    %---------------------------------- tikz ---------------------------------------
    \usepackage{tikz}
    \usetikzlibrary{positioning,chains,fit,shapes,calc,arrows,patterns,external,shapes.callouts,graphs,decorations.pathreplacing}

    \usepackage{fixltx2e} 

\tikzset{radiation/.style={{decorate,decoration={expanding waves,angle=90,segment length=4pt}}},
         antenna/.pic={
        code={\tikzset{scale=5/10}
                \matrix[row sep=10mm,column sep=10mm] {
                % First row:
                \node (a)  {a}; & \node (e) {e}\\
                % Second row:
                \node (b)  {b}; & \node (d) {d} \\
                % Third row:
                \node (c)  {c};  & \node (z) {z} \\
                };
  }}
}

\begin{document}
\begin{tikzpicture}
    \path (0,0) pic[scale=0.5,] {antenna};
        \path
          (1) edge  (b); 

    \path (4,0) pic[scale=0.5,rotate=45] {antenna};
        \path
          (a) edge  (b) 
          (a) edge  (c);
    \path (8,0) pic[scale=0.5,rotate=90] {antenna};
\end{tikzpicture}

\end{document}
  • Related: http://tex.stackexchange.com/a/168705/1952, http://tex.stackexchange.com/a/214787/1952 – Ignasi Oct 16 '15 at 06:49
  • Thanks! I updated the question to suggest something similar to beamer, but I'm doing this for a document, I need the images side-by-side. I don't think I can use beamer functionality. – Fernando César Oct 16 '15 at 07:12
  • Please, look at my second suggestion, it shows that you can draw step by step with beamer and later on include all your figures inside an article side by side – Ignasi Oct 16 '15 at 07:19
  • I was kind of hopping I could do that in just one document. But your answer looks a great idea. – Fernando César Oct 16 '15 at 07:28
  • If you only need to add new stuff in every "frame" and not take old stuff away, then probably this could be a starting point? – Tom Bombadil Oct 16 '15 at 08:44
  • You not only need the image, you need to define the locations of (a) etc.A savebox is just an image with a height, depth and width. – John Kormylo Oct 16 '15 at 14:49

1 Answers1

3

You have some missing semicolons and I replaced the undefined (1) with (d). LaTeX has a problem with & so I used ampersand replacement.

You can place a macro inside a tikzpicture but not inside a path or matrix, at least not without using \pgfextra{}. Tikz uses its own parser which behaves differently than LaTeX parsing.

\documentclass[10pt,a4paper,twoside]{report}
%---------------------------------- tikz ---------------------------------------
\usepackage{tikz}
%\usetikzlibrary{positioning,chains,fit,shapes,calc,arrows,patterns,external,shapes.callouts,graphs,decorations.pathreplacing}

\newcommand{\stepa}{%
\matrix[row sep=10mm,column sep=10mm,ampersand replacement=\&] {
  % First row:
  \node (a)  {a}; \& \node (e) {e}; \\
  % Second row:
  \node (b)  {b}; \& \node (d) {d}; \\
  % Third row:
  \node (c)  {c}; \& \node (z) {z}; \\
};}

\newcommand{\stepb}{\path (d) edge  (b);}

\newcommand{\stepc}{\path (a) edge  (c);}

\begin{document}

\begin{tikzpicture}
\stepa
\end{tikzpicture}
\hfill
\begin{tikzpicture}
\stepa
\stepb
\end{tikzpicture}
\hfill
\begin{tikzpicture}
\stepa
\stepb
\stepc
\end{tikzpicture}

\end{document}

stepwise tikzpicture

John Kormylo
  • 79,712
  • 3
  • 50
  • 120