此代码:
\pic[rotate=40, local bounding box=microfono] at (4.5,-5) {microph};
\draw[black, line width=2pt] (microfono.-45) -- ++(-.2,+.2) ++(.2,-.2) -- ++(0,-2);
在外面有效scope
,但在里面无效(不显示该线)。为什么?
\documentclass[border=0pt]{standalone}
\usepackage{tikz}
\tikzset{
pics/microph/.style={code={
\draw[black, line width=.2em, rounded corners=1.7ex]
(-.85em,4.5ex) -- (-.85em,2ex) -- (.85em,2ex) -- (.85em,4.5ex);
\fill[black]
(-.6em,5ex) to[rounded corners=1.2ex]
(-.6em,2.5ex) to[rounded corners=1.2ex] (.6em,2.5ex)
-- (.6em,5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,5.5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,6ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,6.5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
to[rounded corners=1.2ex]
(.6em,8ex) to[rounded corners=1.2ex]
(-.6em,8ex) to cycle;
}},
}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip (-8.5,-6.5) rectangle (8.5,6.5);
\node[fill=cyan, draw=cyan, minimum width=17cm, minimum height=13cm](sky){};
\pic[rotate=40, local bounding box=microfono] at (4.5,-5) {microph};
\draw[black, line width=2pt] (microfono.-45) -- ++(-.2,+.2) ++(.2,-.2) -- ++(0,-2);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
我最初local bounding box
在 PGF 3.1.5 中通过无条件执行 来\pgf@path@size@hook
修复的这种用法\pgf@protocolsizes
。然而,这破坏了 的所有其他用法local bounding box
。不幸的是,我想不出另一种解决方案来让这种非常小众的情况发挥作用。不过,我可以提出一种local bounding box
在\clip
上下文中使用 的解决方法。只需添加overlay=false
到您想要获取其本地边界框的路径或范围即可。
\documentclass[border=0pt]{standalone}
\usepackage{tikz}
\tikzset{
pics/microph/.style={code={
\draw[black, line width=.2em, rounded corners=1.7ex]
(-.85em,4.5ex) -- (-.85em,2ex) -- (.85em,2ex) -- (.85em,4.5ex);
\fill[black]
(-.6em,5ex) to[rounded corners=1.2ex]
(-.6em,2.5ex) to[rounded corners=1.2ex] (.6em,2.5ex)
-- (.6em,5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,5.5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,6ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,6.5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
to[rounded corners=1.2ex]
(.6em,8ex) to[rounded corners=1.2ex]
(-.6em,8ex) to cycle;
}},
}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip (-8.5,-6.5) rectangle (8.5,6.5);
\node[fill=cyan, draw=cyan, minimum width=17cm, minimum height=13cm](sky){};
\pic[rotate=40, overlay=false, local bounding box=microfono] at (4.5,-5) {microph};
\draw[black, line width=2pt] (microfono.-45) -- ++(-.2,+.2) ++(.2,-.2) -- ++(0,-2);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
的描述\clip
包含这样一句话:“后续路径的大小对于图片大小并不重要。”所以我认为local bounding box
无法获得正确的尺寸。
一个简单的解决方法是绘制两次微操作:
\documentclass[border=0pt]{standalone}
\usepackage{tikz}
\tikzset{
pics/microph/.style={code={
\draw[black, line width=.2em, rounded corners=1.7ex]
(-.85em,4.5ex) -- (-.85em,2ex) -- (.85em,2ex) -- (.85em,4.5ex);
\fill[black]
(-.6em,5ex) to[rounded corners=1.2ex]
(-.6em,2.5ex) to[rounded corners=1.2ex] (.6em,2.5ex)
-- (.6em,5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,5.5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,6ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,6.5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
to[rounded corners=1.2ex]
(.6em,8ex) to[rounded corners=1.2ex]
(-.6em,8ex) to cycle;
}},
}
\begin{document}
\begin{tikzpicture}
%
\begin{scope}
\pic[rotate=40, local bounding box=microfono] at (4.5,-5) {microph};
\clip (-8.5,-6.5) rectangle (8.5,6.5);
\node[fill=cyan, draw=cyan, minimum width=17cm, minimum height=13cm](sky){};
\pic[rotate=40] at (4.5,-5) {microph};
\draw[line width=2pt] (microfono.-45) -- ++(-.2,+.2) ++(.2,-.2) -- ++(0,-2);
\end{scope}
\end{tikzpicture}
\end{document}
答案3
这里不需要局部边界框。您只需使用麦克风中的某个坐标即可。但是,众所周知,这pic
会搞砸名称前缀。这个错误只得到了部分修复,根据 Henri Menke 的说法,它将在 pgf 的下一个版本中完全修复。暂时,让我们使用@cfr 的解决方案用 重置名称前缀name prefix ..
。此解决方案是 pgf 手册 v3.1.4 在第 263 页推荐的解决方案
然后,您可以使用 pgfmanual 中的技巧为图片命名,并从外部引用其坐标之一。这还有一个好处,就是您不必猜测像 -45 这样的角度,如果您旋转麦克风,这个角度就会改变。
\documentclass[border=0pt]{standalone}
\usepackage{tikz}
\tikzset{
pics/microph/.style={code={
\draw[black, line width=.2em, rounded corners=1.7ex]
(-.85em,4.5ex) -- (-.85em,2ex) -- (.85em,2ex) -- (.85em,4.5ex);
\path (0,2ex) coordinate (-plug);
\fill[black,name prefix ..] % https://tex.stackexchange.com/a/194370
(-.6em,5ex) to[rounded corners=1.2ex]
(-.6em,2.5ex) to[rounded corners=1.2ex] (.6em,2.5ex)
-- (.6em,5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,5.5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,6ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
-- (.6em,6.5ex) to[rounded corners=.2ex] ++(-.85em,0) to[rounded corners=.2ex] ++(0,.35ex) -- ++(.85em,0)
to[rounded corners=1.2ex]
(.6em,8ex) to[rounded corners=1.2ex]
(-.6em,8ex) to cycle;
}},
}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip (-8.5,-6.5) rectangle (8.5,6.5);
\node[fill=cyan, draw=cyan, minimum width=17cm, minimum height=13cm](sky){};
\pic[rotate=40] (microfono) at (4.5,-5) {microph};
\draw[black, line width=2pt] (microfono-plug) -- ++(.2,-.2) -- ++(0,-2);
\end{scope}
\end{tikzpicture}
\end{document}
答案4
正如我在评论中提到的,问题出在你的代码上,pic {microph}
而不是 TikZ 手册中第一个pic {seagull}
定义的部分18 Pics: Small Pictures on Paths
。这清楚地表明存在一个漏洞在 的源代码中pic operation
。此错误导致 的to
代码中的操作pic
不兼容。
问题出在pic
操作中的一个错误,它不允许to
操作。通过将这些操作替换为--
,它就可以正常工作。
您不需要本地盒子,因为pic
允许您node
从外部使用,如手册prefix name
部分所示。所以我在电线连接的右侧添加了一个名为的坐标:麦克风底部中间。18.2 The Pic Syntax
(-milieu)
\draw[black, line width=.2em, rounded corners=1.7ex,pic actions]
(-.85em,4.5ex) -- (-.85em,2ex) -- (.85em,2ex)coordinate[midway](-milieu) -- (.85em,4.5ex);
\documentclass[border=0pt]{standalone}
\usepackage{tikz}
\tikzset{
pics/microph/.style={code={
\draw[black, line width=.2em, rounded corners=1.7ex,pic actions]
(-.85em,4.5ex) -- (-.85em,2ex) -- (.85em,2ex)coordinate[midway](-milieu) -- (.85em,4.5ex);
\fill[black,pic actions]
(-.6em,5ex)[rounded corners=1.2ex] --%
(-.6em,2.5ex)[rounded corners=1.2ex] --%to
(.6em,2.5ex)
-- (.6em,5ex)[rounded corners=.2ex] --%to
++(-.85em,0)[rounded corners=.2ex] --%to
++(0,.35ex) -- ++(.85em,0)
-- (.6em,5.5ex)[rounded corners=.2ex] --%to
++(-.85em,0)[rounded corners=.2ex] --%to
++(0,.35ex) -- ++(.85em,0)
-- (.6em,6ex)[rounded corners=.2ex] --%to
++(-.85em,0)[rounded corners=.2ex] --%to
++(0,.35ex) -- ++(.85em,0)
-- (.6em,6.5ex)[rounded corners=.2ex] --%to
++(-.85em,0)[rounded corners=.2ex] --%to
++(0,.35ex) -- ++(.85em,0)
[rounded corners=1.2ex]--%to
(.6em,8ex) [rounded corners=1.2ex]--%to
(-.6em,8ex) --%to
cycle;
}},
}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip (-8.5,-6.5) rectangle (8.5,6.5);
\node[fill=cyan, draw=cyan, minimum width=17cm, minimum height=13cm](sky){};
\pic[rotate=40] (microfono) at (4.5,-5) {microph};
\draw[black, line width=2pt] (microfono-milieu) -- ++(.2,-.2) -- ++(0,-2);
\end{scope}
\end{tikzpicture}
\end{document}