5

This code:

\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);

works outside the scope but not inside (the line is not displayed). Why?

\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}

enter image description here

CarLaTeX
  • 62,716

4 Answers4

5

the description of \clip contains the sentence "the size of subsequent paths will not be important for the picture size." So I assume that local bounding box can't get the correct size.

A simple work around is to draw the micro twice:

\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}

enter image description here

Ulrike Fischer
  • 327,261
  • 1
    You are correct. The problem here is local bounding box which hooks into \pgf@protocolsizes but only \ifpgf@relevantforpicturesize which seems wrong to me. The hook should be executed unconditionally. – Henri Menke Dec 01 '19 at 03:01
  • The microphone wire marks a break at the point where it is bent. – AndréC Dec 01 '19 at 07:31
5

You do not need a local bounding box here. You can just use some coordinate in the microphone. However, it is known that pics screw up the name prefixes. This bug has been only fixed partially, and according to Henri Menke it will be fixed completely in the next version of pgf. For the time being, let us use @cfr's solution to reset the name prefixing with name prefix ... This solution is what the pgf manual v3.1.4 recommends on p. 263

enter image description here

Then you can use the tricks from the pgfmanual to give a name to the pic, and to refer to one of its coordinates from outside. This has the additional benefit that you do not have to guess an angle like -45, which will change if you rotate the microphone.

\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}

enter image description here

  • 1
    This has nothing to do with name prefix. Your “fix” was just a coincidence because you removed local bounding box. – Henri Menke Dec 01 '19 at 03:04
  • @HenriMenke I am aware of this, and, as written in the answer, my, or more precisely cfr's, fix made the local bounding box unnecessary. It allows one to access the coordinates from outside. My understanding is that the local bounding box is only a tool that was introduced because of the fix it is not possible to do so. –  Dec 01 '19 at 03:20
  • I see. That name prefix thing is fixed on the latest master https://github.com/pgf-tikz/pgf/commit/88951be592b558b94b14a97aaffe9df6c1ce61ee. I hope I can release 3.1.5 before Christmas. – Henri Menke Dec 01 '19 at 03:24
  • @HenriMenke Sounds great! (I wish I was more proficient with GitHub such that I could try out things.) –  Dec 01 '19 at 03:26
  • That is why I provide the TeX Live repository which always brings you the latest master automatically with your TeX Live update: https://github.com/pgf-tikz/pgf#installation – Henri Menke Dec 01 '19 at 03:27
  • @HenriMenke I saw this but it is not clear to me if this will destroy working TeX Live installation, which is why I did not dare to touch it. (I am also using LaTeX for my daily work, and would hate to see it broken.) –  Dec 01 '19 at 03:29
  • 1
    The “nuke the complete TeX Live installation” switch is currently disabled ;-). The only thing it can break is TikZ, of course, but I try hard to not do that. If it does break you should open a bug report. I think I react relatively fast. – Henri Menke Dec 01 '19 at 03:33
  • @HenriMenke OK, but then, if you try to answer questions, you may end up not being able to reproduce a bug because in the developer version it is already fixed. Is that true? I know that several users are able to run code on different TeX installations, something that I never tried to do. –  Dec 01 '19 at 03:35
  • If your whole purpose of using TikZ is to find workarounds for bugs that have been fixed already, then yes, you will not be able to reproduce them. – Henri Menke Dec 01 '19 at 03:56
  • @HenriMenke My question is more if one can selectively switch on and off the developer version when one compiles. –  Dec 01 '19 at 03:57
  • 1
    Then you should look at https://github.com/pgf-tikz/pgf#development. With the first three command you can switch to the development version in your active shell session. However, that does not update automatically. You will have to go to the directory where you cloned and perform git pull origin master when you want to update. See also https://tex.stackexchange.com/questions/448263/is-there-a-way-to-create-tex-live-or-latex-virtual-environment – Henri Menke Dec 01 '19 at 03:59
  • @HenriMenke Given your own answer it would be a really great idea if you removed your first comment above. The bottom-line seems to be that there is no complete fix, so there is really no reason to criticize an answer that makes the use of the local bounding box unnecessary. IMHO this remains the by far simplest and sustainable solution to the problem. –  May 23 '20 at 20:21
4

As I mentioned in my comments the problem arises with your pic {microph} and not with the first pic {seagull} defined on section 18 Pics: Small Pictures on Paths of the TikZ manual. This clearly shows that there is a bug in the source code of the pic operation. This bug makes the to operations in the code of a pic incompatible.

The problem comes from a bug in the pic operation that does not tolerate to operations. By replacing these operations with --, it works.

You do not need a local box because the pic allow you to place node accessible from the outside with the prefix name as shown on section 18.2 The Pic Syntax of the manual. So I added a coordinate called the (-milieu) to the right where the wire connects: in the middle of the bottom of the microphone.

\draw[black, line width=.2em, rounded corners=1.7ex,pic actions] 
            (-.85em,4.5ex) -- (-.85em,2ex) -- (.85em,2ex)coordinate[midway](-milieu) -- (.85em,4.5ex);

screenshot

\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}
AndréC
  • 24,137
  • @CarLaTeX Thank you. Do you want to report this bug to Henri Menke so he can fix it? – AndréC Nov 30 '19 at 18:17
  • I'll try to do it, thanks – CarLaTeX Nov 30 '19 at 18:39
  • That is a coincidence because you removed local bounding box. The to operation works just fine in pic. – Henri Menke Dec 01 '19 at 03:03
  • @HenriMenke Indead, the manual say on page 259 Unlike nodes, pics cannot be referenced later on. You can reference nodes that are inside a pic, but not “the pic itself”. In particular, you cannot draw lines between pics the way you can draw them between nodes. In general, whenever it makes sense that some drawing could conceivably be connected to other node-like-things, then a node is better than a pic. So, the use of a local bounding box is in itself wrong. – AndréC Dec 01 '19 at 03:45
1

I had originally fixed this use of local bounding box in PGF 3.1.5 by executing the \pgf@path@size@hook in \pgf@protocolsizes unconditionally. However, this broke all other uses of local bounding box. Unfortunately, I could not think of another solution to make this admittedly very niche situation work. Nevertheless, I can propose a workaround for using local bounding box in a \clip context. Simply add overlay=false to the path or scope that you want to get the local bounding box of.

\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}
Henri Menke
  • 109,596