局部邊界框在範圍內不起作用

局部邊界框在範圍內不起作用

這段程式碼:

\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

我最初透過無條件執行in來修復local bounding boxPGF 3.1.5 中的這種用法。然而,這打破了 的所有其他用途。不幸的是,我想不出另一種解決方案來使這種公認的非常利基的情況發揮作用。儘管如此,我可以提出一種在上下文中使用的解決方法。只需新增至您想要取得其本機邊界框的路徑或範圍即可。\pgf@path@size@hook\pgf@protocolsizeslocal bounding boxlocal bounding box\clipoverlay=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

這裡不需要本地邊界框。您可以只使用麥克風中的一些座標。然而,眾所周知,pics 搞砸了名稱前綴。這個錯誤僅被部分修復,根據 Henri Menke 的說法,它將在 pgf 的下一個版本中完全修復。暫時讓我們使用@cfr的解決方案重設以 . 為前綴的名稱name prefix ..。此解決方案是 pgf 手冊 v3.1.4 第 14 頁推薦的解決方案。 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

正如我在評論中提到的,問題出在您的身上,而不是TikZ 手冊中pic {microph}第一個pic {seagull}定義的部分。18 Pics: Small Pictures on Paths這清楚地表明,有一個漏洞在 的原始碼中pic operation。這個bug使得to程式碼中的操作pic不相容。

問題來自於pic操作中的一個bug,不容許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}

相關內容