I would like to define a shape for nodes in tikz using another tikzpicture. For instance, I would like to make the following into a node shape called monoid

  \node (m) [circle,scale=.5,draw] at (0, 0) {};
  \node (i1) at (-1,.5){};
  \node (i2) at (-1,-.5){};
  \node (o) at (1,|-m){};
    \draw [-] (m) to (o);
    \draw [-] (i1) to [out=0, in=135] (m);
    \draw [-] (i2) to [out=0, in=-135] (m);

and be able to use it in another tikzpicture. For example, doing something like

  \draw (-1,0)[out=0,in=180]to(M.i1);

If someone could point me to the part of the manual that I need to read in order to do this, it would be great. Thanks a lot in advance!



1 Answer 1


Welcome! This is what pics are for. Instead of a node, define a pic. So, basically, the code of the first tikzpicture in your question goes into

pics/monoid/.style={code={<your code>}}

You can move and/or transform the resulting pic (more or less) like a node.1 What does not work is to use periods to separate the anchors from the name of the pic, but the pgfmanual suggests using hyphens (see the example on the top part of p. 267 of pgfmanual v3.1.5), which I am doing here. Then something like

\pic (M) at (1.2,0.5) {monoid};
 (5.2,-0.5) pic[rotate=30,red](M'){monoid}; 
\draw (-1,0)[out=0,in=180] to (M-i1);

works (but I prefer the \path ... pic ...; syntax). As you can see, one can now access the i1 pic coordinate (or "anchor") from outside. (I changed those to become coordinates instead of extended nodes.)

    \node (-m) [circle,scale=.5,draw] at (0, 0) {};
    \coordinate (-i1) at (-1,.5){};
    \coordinate (-i2) at (-1,-.5){};
    \coordinate (-o) at (1,0|--m){};
    \draw [-] (-m) to (-o);
    \draw [-] (-i1) to [out=0, in=135] (-m);
    \draw [-] (-i2) to [out=0, in=-135] (-m);}}]
 \path (1.2,0.5) pic(M){monoid}
 (5.2,-0.5) pic[rotate=30,red](M'){monoid}; 
 \draw (-1,0)[out=0,in=180] to (M-i1);

enter image description here

1For instance, positioning does not work with pics like nodes, basically because TikZ does not know the boundary paths of the pics.

  • 1
    I think it more about the fact that a pic doesn't have anchors that positioning doesn't work. May 15, 2020 at 9:47
  • 3
    @HenriMenke You are right, that's perhaps a better statement. What I wanted to say is that if you wrap a pic in a \matrix, you can position it to some extent. This is because the matrix wraps a rectangle node around it, and this is what's meant by a boundary path. Yet, technically one really needs only the west and so on anchors, as you say.
    – user194703
    May 15, 2020 at 16:26

You must log in to answer this question.

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