0

I have a basic tikzpicture I want to re-use in other tikzpictures and be able to use the coordinates and paths defined within it.

For instance, I have a geometric figure used in a problem and the same geometric figure with labels used in a solution. If I put a tikzpicture inside a \newcommand, I can insert it in another tikzpicture, but the coordinates are messed up. In simple cases I can redefine them, but with complex figures I would like to be able to refer to the coordinates already defined from another tikzpicture.

In the template below the red node A in the second picture was created from the coordinates defined in the first picture: I would like it to be exactly on top of the lower-left, blue node A.

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

% basic figure to be reused
\newcommand{\basic}{%
  \begin{tikzpicture}[remember picture]% doesn't help

  \coordinate (A) at (0,0);
  \coordinate (B) at (1,1);
  \node [blue,below] at (A) {A};
  \draw [blue] (A) -- (B);

  \end{tikzpicture}
}%


\begin{tabular}{@{}c@{}}

\begin{tikzpicture}

  \node at (0,0) (pic1) {\basic};

\end{tikzpicture}

\vspace{1cm} \\

\begin{tikzpicture}

  \node at (0,0) (pic2) {\basic};

  \node [red,below] at (A) {A};

\end{tikzpicture}

\end{tabular}

\end{document}

enter image description here

Perhaps I can automate a coordinate adjustment in the second picture? Or maybe there is a better approach altogether. I tried to use the remember picture option, but didn't quite know how to.

I saw this question but Peter Grill, in his answer, writes "I am not recommending nesting tikzpicture environments." So I wonder what a better solution would be. There is also that question, but in my attempts to use a scope with a yshift, the coordinates were also shifted. Or did I miss something?

2 Answers 2

2

Instead of a \newcommand you can use a pic. This is what pic are for, reuse part of tikzpictures.

If the pic has a name, all inner nodes can be referenced combining pic's name and nodes' names. Here you have an example:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
%basic figure to be reused
    myfig/.pic={
  \coordinate (A) at (0,0);
  \coordinate (B) at (1,1);
  \node [blue,below] at (A) {A};
  \draw [blue] (A) -- (B);
    }
}

\begin{document}

\begin{tabular}{@{}c@{}}

\begin{tikzpicture}

  \pic {myfig};

\end{tikzpicture}

\vspace{1cm} \\

\begin{tikzpicture}

  \pic (original) {myfig};

  \node [red,below] at (originalA) {A};

\end{tikzpicture}

\end{tabular}

\end{document}

enter image description here

1
  • This is neat Ignasi, thanks! Very easy to use. Working well so far, except that \pgfmathsetmacro defined inside basic figure does not appear to be available when recalling the figure. Am I right? Workaround? Thanks!
    – PatrickT
    Jul 1, 2020 at 21:40
2

The new scontents package does quite what you want, as it allows you to reuse content. IMO is quite slow, but so far it is the only caveat

%Standalone for illustrative purposes
\documentclass{standalone}
\usepackage[store-cmd=tikz]{scontents}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
%Called with \getstored[1]{tikz}
\Scontents*{\coordinate (A) at (0,0);
    \coordinate (B) at (1,1);
    \node [blue,below] at (A) {A};
    \draw [blue] (A) -- (B);}
%If you need more content, use
%\Scontents*{mycontent...}
\begin{document}
\begin{tabular}{c}
\begin{tikzpicture}
\getstored[1]{tikz}
\end{tikzpicture}
\\
\begin{tikzpicture}
\getstored[1]{tikz}
\node [red,below] at (A) {A};
\end{tikzpicture}
\end{tabular}
\end{document}

enter image description here

2
  • 1
    Great idea, thanks JairoADelRio, I'd never heard of the scontents package!
    – PatrickT
    Jul 1, 2020 at 10:14
  • 1
    It is somewhat inspired by ConTeXt buffers, which allow you to reuse chunks of code. It can be used outside TikZ and, well, it's a nicely done package :)
    – user193767
    Jul 1, 2020 at 10:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .