1

I was confronted with a problem of \pic and \foreach in Tikz. Below is an MWE:

\documentclass[tikz]{standalone}
\tikzset{%
 pics/abc/.style={%
 code={
 \node {this is some path};
 }
 }
}
\begin{document}
\begin{tikzpicture}
%local bounding box failed
\foreach \i in {1,2} \pic[local bounding box=\i] at (0,\i) {abc};
\draw(1)circle(1);

%local bounding box successful
\pic[local bounding box=3] at (10,4) {abc};
\draw(3)circle(1);

\end{tikzpicture}
\end{document} 

When I produce the local bounding box (1) and (2) using \foreach and \pic, \draw(1)circle(1); will cause error. But when I produce the local bounding box (3) directly using \pic without \foreach, \draw(3)circle(1); will be all right. Thanks very much for any help!

1

You need braces and may want to add the /.expanded key. I made several additional changes to "modernize" your code.

\documentclass[tikz]{standalone}
\tikzset{%
 pics/abc/.style={%
 code={
 \node {this is some path};
 }
 }
}
\begin{document}
\begin{tikzpicture}
%local bounding box failed
\path foreach \i in {1,2} {(0,\i) pic[local bounding box/.expanded=\i]  {abc}};
\draw (1) circle [radius=1];

%local bounding box successful
\pic[local bounding box=3] at (10,4) {abc};
\draw(3)circle[radius=1];

\end{tikzpicture}
\end{document} 

enter image description here

| improve this answer | |
  • Thanks very much! You've helped me solve the problem. – Misaya Mar 24 at 1:37

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.