1

I am trying to generate a diagram which repeatedly contains the same graphics. See below a sample graphics:

enter image description here

This is how I am generating it:

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{shapes.misc, shapes.geometric}

\begin{document}
\tikzset{
    line/.style={-, draw=black!30, line width=1pt},
}

\begin{tikzpicture}[node distance=2cm]
    \node[draw=black!30, rectangle, minimum height=8mm, minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle corners={south west, south east},chamfered rectangle xsep=2pt, below] at (0, 0) (b1) {} ;
    \node[draw=black!30, rectangle, minimum height=8mm, minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle corners={south west, south east},chamfered rectangle xsep=2pt, below] at (1, 1) (b2) {} ;
    \node[draw=black!30, rectangle, minimum height=8mm, minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle corners={south west, south east},chamfered rectangle xsep=2pt, below] at (0, 2) (b3) {} ;
    \draw[line] (b1) -- (b2) -- (b3);
\end{tikzpicture}
\end{document}

I wish to define the chamfered rectangle as a .pic so that it can be used later on with \path command.

I am looking for something like this:

\tikzset{
line/.style={-, draw=black!30, line width=1pt},
box/.pic={
    \draw[draw=black!30, rectangle, minimum height=8mm, minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle corners={south west, south east},chamfered rectangle xsep=2pt, below] (0, 0);
    },
}

\begin{tikzpicture}[node distance=2cm]
    \path[line] (0, 0) pic {box} --
                (1, 1) pic {box} --
                (0, 2) pic {box};
\end{tikzpicture}
  • You can use nodes in a path command. – marmot Mar 8 at 14:56
  • @marmot: yes, I can. However I am learning pic at this moment, hence I am asking to define a pic to do the same. – Ravi Joshi Mar 8 at 14:59
2

Are you looking for something like this?

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{shapes.misc, shapes.geometric}

\begin{document}
\tikzset{
    line/.style={-, draw=black!30, line width=1pt},
}

\begin{tikzpicture}[mycham/.style={draw=black!30, rectangle, minimum height=8mm,
minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle
corners={south west, south east},chamfered rectangle
xsep=2pt},pics/champic/.style={code={\node[mycham] (-node){};}}]
    \path (0, 0) pic (b1) {champic}
    -- (1, 1) pic (b2){champic} 
    -- (0,2)  pic (b3){champic};
    \draw[line] (b1-node) -- (b2-node) -- (b3-node);
\end{tikzpicture}
\end{document}

enter image description here

You could define the pic in this case also using pics/champic/.style={code={\node[mycham] (-node){};}}. However, AFAIK, this syntax is less flexible. Imagine you want to pass more than one arguments to the pic, as in

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{shapes.misc, shapes.geometric}

\begin{document}
\tikzset{
    line/.style={-, draw=black!30, line width=1pt},
}

\begin{tikzpicture}[mycham/.style={draw=black!30, rectangle, minimum height=8mm,
minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle
corners={south west, south east},chamfered rectangle
xsep=2pt},pics/champic/.style n args={2}{code={\node[mycham,#2] (-node){#1};}}]
    \path (0, 0) pic (b1) {champic={A}{blue}}
    -- (1, 1) pic (b2){champic={B}{red}} 
    -- (0,2)  pic (b3){champic={C}{green!70!black}};
    \draw[line] (b1-node) -- (b2-node) -- (b3-node);
\end{tikzpicture}
\end{document}

enter image description here

As you can see, with the syntax chosen here this is no problem, but with the /.pic= syntax I wouldn't know how to do that.

Of course, if you have no parameters, and are sure you never need some, you can do

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{shapes.misc, shapes.geometric}

\begin{document}
\tikzset{
    line/.style={-, draw=black!30, line width=1pt},
}

\begin{tikzpicture}[mycham/.style={draw=black!30, rectangle, minimum height=8mm,
minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle
corners={south west, south east},chamfered rectangle
xsep=2pt},champic/.pic={\draw (0,0) node[mycham] (-node){};}]
    \path (0, 0) pic (b1) {champic}
    -- (1, 1) pic (b2){champic} 
    -- (0,2)  pic (b3){champic};
    \draw[line] (b1-node) -- (b2-node) -- (b3-node);
\end{tikzpicture}
\end{document}

as you suggest.

  • Thanks, marmot. This is a cool way to define a pic where you have used node. I wasn't aware of this way. I just added a preferred way of defining the pic in the question, since I can see a similar way inside tikz documentation as well. Can you please have a look and update the answer accordingly? – Ravi Joshi Mar 8 at 15:23
  • @RaviJoshi Actually I believe that the way I use is advantageous as it allows you to define pics that take more than one arguments. If you want the other syntax, you could use ,champic/.pic={\node[mycham] (-node){};} instead of ,pics/champic/.style={code={\node[mycham] (-node){};}} but I wouldn't. Please notice also that there seems to exist a bug if you nest pics (which we are not doing here). – marmot Mar 8 at 15:29
  • (1) As per tikz documentation at page 262 Section 18.3, pics/seagull/.style ={code = {\draw (...) ...;}} and seagull/.pic = {\draw (...) ...;} are same. Hence I guess champic/.pic={\node[mycham] (-node){};} and pics/champic/.style={code={\node[mycham] (-node){};}} are same. (2) Page 259 of the same manual defines a pic using my pic/.pic = {\draw (-3mm,-3mm) rectangle (3mm,3mm);}. Hence may I use box/.pic = {\draw (0, 0) rectangle[chamfered rectangle,chamfered rectangle corners={south west, south east},chamfered rectangle xsep=2pt, below] (8mm, 8mm);} to define the pic? – Ravi Joshi Mar 8 at 15:50
  • 1
    @RaviJoshi I added an explanation for why I prefer the syntax I use. Does that make sense? – marmot Mar 8 at 16:06
  • 1
    Completely agree with you. Thank you very much. – Ravi Joshi Mar 9 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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