为什么图片的相对定位会改变一个图片元素的位置?

为什么图片的相对定位会改变一个图片元素的位置?

我的图像设计出了什么pic问题,它的相对定位改变了(仅)一个内部节点的位置:

在此处输入图片描述

梅威瑟:

\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

毫无疑问,有人会提供更技术性的解释,必须注意的是,pics 的实现是极其有缺陷。知道如何解决缺陷对于以任何重要的方式使用它们都是必不可少的。

但是,我不确定这是一个错误,还是它们是pics 而不是nodes 的必然结果。节点具有各种功能,可帮助它最终到达正确的位置,而无需明确告知要去哪里。节点的放置是根据一组规则进行的,这些规则大多是猜测它们应该在哪里。特别是,anchor节点的 通常会被正确猜测,尽管显然有时必须明确指定。没有一种算法能够始终正确。

pics 的复杂程度要低得多。我认为,这里发生的事情是,当没有更多近端线索可以帮助做出更好的猜测时,单个节点的anchor选择取决于整体的位置。pic

因为picleft=... of ...,所以节点的默认锚点是east。这就是我们在错误放置节点时看到的。其他节点不会以明显的方式发生这种情况,因为事物是相对于它们绘制的((t)),而不是相对于其他事物绘制的,因为它们的anchor是明确给定的((v)),或者因为它们的位置是完全独立确定的((bb))。如果我们覆盖 的默认锚点(r),那么它就不再是错误放置的。

替换节点

理想情况下,TiZ 会优先使用来自 的定位提示,\path而不是来自周围 的放置的提示pic。但我不知道这是否可以实现。无论如何,pic正如我所说, 确实有很多错误,这可能只是另一个pic错误。

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

相关内容