3

What is in design of my pic images wrong that its relative positioning change the position of (only) one internal node:

enter image description here

MWE:

\documentclass[tikz, border=3mm, many]{standalone}
\usetikzlibrary{arrows.meta, backgrounds, fit, scopes, positioning, quotes, shapes}

\begin{document}
\tikzset{
            node distance = 0mm,
every label/.append style = {label distance=1pt},
                LC/.style = {draw, thick, {Circle[open,fill=white]}-},
gonTx/.pic = {\node (t) [regular polygon, regular polygon sides=3,
                         draw, fill=white, text width=7mm, rotate=-90] {};
              \draw[LC,thin,shorten <=-3pt] (t.side 1)  -- ++ (-0.5,0)
                    coordinate (t1) --
                    node (r)[draw, fill= white, minimum height=4mm,
                             inner sep=1mm, label=right:$R_{G}$, midway] {}
                    (t.south -| t1) --
                node (v) [circle, draw, fill=white, inner sep=0pt, anchor=center,
                          minimum size=4mm, text=teal!60!black, midway] {$u$}
                                (t1 |- t.side 3) -- (t.side 3);
              \scoped[on background layer]
                \node (bb) [fill=gray!30, inner sep=2mm,
                            fit=(t.corner 1)(t.corner 2) (t.corner 3) ] {};%(z)
            \coordinate (-in)  at (t.south);
            \coordinate (-out) at (t.north);
              },% end of gonTx
}
% with out positioning
    \begin{tikzpicture}
\pic (T) {gonTx};  %
\draw[Straight Barb-] (T-in) to ["signal" ']  ++ (-1.5,0);
    \end{tikzpicture}

% with relative positioning to given coordinate
    \begin{tikzpicture}
\coordinate (b1) at (1,0);
\pic (T) [left=22mm of b1] {gonTx};  %
\draw[Straight Barb-] (T-in) to ["signal" ']  ++ (-1.5,0);
    \end{tikzpicture}
\end{document}
1
  • 1
    The implementation is probably a bit buggy, I think I've seen such problems with "inheritance" of styles in other cases as well. Nov 12 '16 at 1:13
4

No doubt somebody will provide a more technical explanation and it must be noted that the implementation of pics is extremely buggy. Knowing how to workaround bugs is essential for using them in any significant way.

However, I'm not certain that this is a bug as opposed to an inevitable result of their being pics rather than, say, nodes. A node has all kinds of features to help it end up in the right place without being explicitly told where to go. Nodes get placed according to a set of rules which mostly guess where they should be. In particular, the anchor of a node generally gets guessed correctly although obviously it must sometimes be specified explicitly. No algorithm is going to be able to get it right all the time.

pics are much less sophisticated. What is happening here is, I think, that the individual nodes' anchors are being chosen with regard to the placement of the pic as a whole, when there are no more proximal clues available to help make a better guess.

Because the pic is left=... of ..., the default anchor for nodes is east. This is what we see with the misplaced node. This doesn't happen with the other nodes in a noticeable way either because things are drawn relative to them ((t)), rather than their being drawn relative to other things, because their anchors are explicitly given ((v)) or because their location is determined entirely independently ((bb)). If we override the default anchor for (r), then it is no longer misplaced.

replaced node

Ideally, TikZ would use the positioning hints from the \path in preference to those from the placement of the surrounding pic. But I have no idea if this is realisable or not. In any case, pics are, as I say, really quite buggy, and this may be just another pic bug.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, backgrounds, fit, positioning, quotes, shapes.geometric}
\begin{document}
\tikzset{
  node distance = 0mm,
  every label/.append style = {label distance=1pt},
  CB/.style = {thick, {Circle[open,fill=white]}-{Bar[width=3.4mm,line width=2pt]}},
  LB/.style = {-{Bar[width=3.4mm,line width=2pt]}},
  LC/.style = {draw, thick, {Circle[open,fill=white]}-},
  Lr/.style = {red,  very thick},
  Lb/.style = {blue, very thick},
  gonTx/.pic = {%
    \node (t) [regular polygon, regular polygon sides=3, draw, fill=white, text width=7mm, rotate=-90] {};
    \draw [LC, thin, shorten <=-3pt] (t.side 1)  -- ++(-0.5,0) coordinate (t1) -- node (r) [draw, fill=red, minimum height=4mm, inner sep=1mm, label=right:$R_{G}$, midway, anchor=center] {} (t.south -| t1) -- node (v) [circle, draw, fill=white, inner sep=0pt, anchor=center, minimum size=4mm, text=teal!60!black, midway] {$u$} (t1 |- t.side 3) -- (t.side 3);
    \scoped[on background layer]
    \node (bb) [fill=gray!30, inner sep=2mm, fit=(t.corner 1)(t.corner 2) (t.corner 3) ] {};
    \coordinate (-in)  at (t.south);
    \coordinate (-out) at (t.north);
  },
}
% with out positioning
\begin{tikzpicture}
  \pic (T) {gonTx};
  \draw[Straight Barb-] (T-in) to ["signal" ']  ++ (-1.5,0);
\end{tikzpicture}

% with relative positioning to given coordinate
\begin{tikzpicture}
  \coordinate (b1) at (1,0);
  \pic (T) [left=22mm of b1] {gonTx};
  \draw[Straight Barb-] (T-in) to ["signal" ']  ++ (-1.5,0);
  \node at (b1) {b1};
\end{tikzpicture}
\end{document}
2
  • 1
    now I don't know, how I can forgot on anchor of resistor when I use it at voltage generator! I fill embarrassed ... Meanwhile I solve another problem (not present in question), why the pic is not vertically centered to coordinate. If I replace rotate=-90 with shape border rotate=-90, this is achieved.
    – Zarko
    Nov 12 '16 at 4:26
  • @Zarko It is much easier to see things like this in other people's code than it is your own ;).
    – cfr
    Nov 12 '16 at 13:47

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .