如何設定標籤相對於其圖釘的位置?

如何設定標籤相對於其圖釘的位置?

考慮以下 LaTeX 手稿,其中包含帶有附加固定標籤的空節點的 TikZ 圖片。

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \node [draw,pin=x] {};
\end{tikzpicture}
\end{document}

結果影像是

                                                                帶有固定標籤的節點

(這並不完全是您從上面的程式碼中獲得的圖像:為了方便讀者,我稍微增強了引腳的可見性。)

我知道如何調整引腳的角度和長度(這在 TikZ & PGF 手冊第 241 頁 3.0.1a 版本的第 17.10.3 節“引腳選項”中有描述),但是如何調整位置標籤相對於圖釘頭部的角度(角度、距離和標籤錨)?

答案1

從下面的程式碼中可以看出,所需的錨點(south在預設情況下)被放置在標籤的位置上,之後,引腳從節點的中心繪製到標籤的中心:

在此輸入影像描述

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
\node [draw, pin={[draw]x}, pin={[draw, anchor=west, red]x}, pin={[draw, anchor=south east, blue]x}] {};
\end{tikzpicture}
\end{document}

更新:

讓我們再試一次。

pin={[pin distance=1.2cm, draw, red]0:x},

將在距離main node.0錨點 1.2 公分處繪製一個標籤節點。標籤節點的錨點將為west。這個預設錨點是根據主節點的標籤位置決定的。

pin={[pin distance=1.2cm, draw, blue]74:x}

這個會將預設標籤的錨點 ( south west) 放置在距主節點 0.74 錨點起始的 74 度線 1.2 公分處。放置節點後,將在節點中心之間繪製引腳線。這條線不會遵循 74 度,也不會有 1.2 公分的長度。

pin={[name=pin, pin distance=1.2cm, draw, red, anchor=-37]74:\phantom{x}},

與上一個相同,但 .-37 標籤節點錨點放置在距離主節點 0.74 錨點 1.2 公分處。

在此輸入影像描述

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}

\draw[help lines] (-1,-1) grid (4,2);
\node [draw, 
    pin={[pin distance=1.2cm, draw, red]0:x},
    pin={[pin distance=1.2cm, draw, blue]74:x}] (a) {};

\draw[<->] (a.0)-- node[above]{1.2 cm} ++(0:1.2);
\draw[<->] (a.74)-- node[above]{1.2 cm} ++(74:1.2);

\begin{scope}[xshift = 3cm]
\node [draw, 
    pin={[name=pin, pin distance=1.2cm, draw, red, anchor=-37]74:\phantom{x}},
    pin={[pin distance=1.2cm, draw, blue]74:x}] (a) {};

    \draw[red] (pin.center)--(pin.-37);
\end{scope}
\end{tikzpicture}
\end{document}

答案2

你的問題是重複的,但是,我現在找不到它(還),所以請不要投票我的答案。但我將答案儲存在我的 LaTeX 範例集合中:

%%%% aligned-pin
\documentclass[tikz, border=5mm]{standalone}

\tikzset{aligned pin/.style args={[#1]#2:#3}% new sort of pin
    {pin={[%
           inner sep=0pt,%
           label={[%
                append after command={%
                node[%
                     inner sep=0pt,%
                     at=(\tikzlastnode.#2),% 
                     anchor=#1,%
                    ]{#3}%
                }%
            ]center:{}}%
         ]#2:{}}%
    }
        }

    \begin{document}
\begin{tikzpicture}
\draw[thick] (0,0) -- node[pos=0.8,% position of node
                           coordinate,                 % node is behavior as coordinate
                           aligned pin={[east]         % anchor of pin label
                                        120:           % direction of pin
                                        g factor=1.96} % text in pin label
                                  ] {} (3,2);
\end{tikzpicture}
    \end{document}

在此輸入影像描述

編輯:我發現原來的Qrrbrbirbel 的回答

相關內容