2

Eventually, I'd like to draw a picture inside of a node of a bigger picture. Since nesting tikzpictures is not recommended, I thought that the pic syntax provided a convenient alternative.

However, pic doesn't seem to work as I thought when there are edge nodes: In the pic picture, the edge node is where the end of the path is instead of in between.

Can someone explain what's going on here and suggest a way out of this?

\documentclass{article}
\usepackage{tikz}

\begin{document}

\tikzset{test/.pic={
        \node (a) at (0,0) {a};
        \node (b) at (3,0) {b};
        \draw (a)--node{edge node} (b);
    }
}
\begin{tikzpicture}
\pic {test};
\end{tikzpicture}


\begin{tikzpicture}
\node (a) at (0,0) {a};
\node (b) at (3,0) {b};
\draw (a)--node{edge node} (b);
\end{tikzpicture}

\end{document}

enter image description here

1
  • Thanks, and thanks, fixed.
    – ga325
    Feb 2, 2020 at 15:24

1 Answer 1

1

Unfortunately I have no idea what's going on, but specifying the pos option to the edge node solves the placement:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\tikzset{test/.pic={
        \node (a) at (0,0) {a};
        \node (b) at (3,0) {b};
        \draw (a)--node[pos=0.5]{edge node} (b);
    }
}
\begin{tikzpicture}
\pic {test};
\end{tikzpicture}


\begin{tikzpicture}
\node (a) at (0,0) {a};
\node (b) at (3,0) {b};
\draw (a)--node{edge node} (b);
\end{tikzpicture}

\end{document}

enter image description here

4
  • 2
    This is IMHO the correct approach. You can also put a pic along a path. So if you were to use the OP's version with \pic[pos=0]{test}; the edge node would sit at the other side. What I wan to say is that in a pic it is usually true that you need to set the pgf keys more thoroughly unless you not want them to set from outside.
    – user194703
    Feb 2, 2020 at 15:07
  • I never use pic but after reading the beautiful pgfmanual I've learned that a pic is neither a node nor a tikzpicture. Possible \tikzset{test/.pic={\node{edge node};}} Feb 2, 2020 at 15:29
  • Thanks @Schrödinger'scat - that also answers why it happens, at least partially.(From the tikz&pgf manual, it appears that pic is not intended to be used for elaborate pictures anyway.)
    – ga325
    Feb 2, 2020 at 15:32
  • 3
    @ga325 Depends on what you mean by "elaborate". I found them quite handy, and use them a lot. There is always the issue that the ambient pgf keys can (a) be set by just using some other key and (b) will in general have an impact on the other stuff in the path. What I think is going on is that if you say \path (0,0) -- (1,2) pic{test}; you want the pic to sit at (1,2) so maybe this is why TikZ automatically issues a pos=1 when the object is not inside a path. In your code \path (0,0) -- pic {test} (0,0); also leads to the desired outcome.
    – user194703
    Feb 2, 2020 at 15:41

You must log in to answer this question.

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