
我想將 a 添加label
到節點,但在節點“內部”。像這樣的東西:
手冊似乎表明這可能是可能的,儘管沒有指定實際的密鑰。
...標籤節點的錨點...以標籤節點「背向」主節點邊界的方式決定。選擇的錨點取決於所選邊界點的位置及其相對於主節點中心的位置以及是否設定了變換形狀選項。一般來說,選擇應該是你所期望的,但在困難的情況下你可能必須自己設定錨點。(強調補充)
但是當我嘗試這樣的例子時:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikz \node [circle,draw,label={[red,anchor=north]above:X}] {my circle};
\end{document}
我沒有看到標籤節點的位置有任何變化。仍然是「外面」。
有任何想法嗎?
答案1
這裡的問題是 TikZ 應用了一些自動化(即\tikz@auto@anchor
巨集),這將覆蓋您的 set anchor
。
我們可以劫持一個.code
在節點中最後應用的密鑰,該密鑰在內部用於標籤(是的,即使在我們自己的設定之後red, anchor=north
)並調用\tikz@auto@anchor
:
\tikzset{label anchor/.style={tikz@label@post/.append style={anchor=#1}}}
或者我們讓進行計算的宏放鬆一點(不需要額外的按鍵):
\tikzset{anchor/.append code=\let\tikz@auto@anchor\relax}
如果新的和改進的anchor
金鑰現在在全球範圍內更多地使用(例如\tikz[anchor=<dir>]
),它也會影響節點(並且目前有辦法將其恢復)。
我還提供了一個密鑰inside
(您甚至可以使用它兩次來反轉效果)。
程式碼
\documentclass[tikz]{standalone}
\usepackage{etoolbox}
\makeatletter
\tikzset{anchor/.append code=\let\tikz@auto@anchor\relax}
\tikzset{inside/.code=\preto\tikz@auto@anchor{\pgf@x-\pgf@x\pgf@y-\pgf@y}}
\makeatother
\begin{document}
\tikz\node [anchor=west, circle,draw,label={[red, anchor=north]above:X}] {my circle};
\tikz\node [anchor=west, circle,draw,label={[red, inside]below:X}] {my circle};
\end{document}
輸出
答案2
這是利用該append after command
選項的另一種方法。重點是,透過引用錨點\tikzlastnode.center
,人們可以相對於該位置添加標籤。
能夠完成此任務的新樣式是my label
:
\tikzset{my label/.style args={#1:#2}{
append after command={
(\tikzlastnode.center) node [#1] {#2}
}
}
}
嗯:
\documentclass[tikz,png,border=10pt]{standalone}
\usepackage{tikz}
\tikzset{my label/.style args={#1:#2}{
append after command={
(\tikzlastnode.center) node [#1] {#2}
}
}
}
\begin{document}
\tikz \node [circle,draw,my label={red,above=0.15cm:X}] {my circle};
\tikz \node [circle,draw,my label={red,below=0.15cm:X}] {my circle};
\end{document}
結果如下:
該樣式在第一個參數上接受任何 TikZ 選項,因此很容易自訂顏色和位置:實際上,最好稍微調整一下偏移量以不與文字重疊。
相反,如果人們知道標籤始終必須放置在“北邊框”下方,那麼最好的方法是將樣式更改my label
為:
\tikzset{my label/.style args={#1:#2}{
append after command={
(\tikzlastnode.north) node [#1] {#2}
}
}
}
這樣,只需設定:
\tikz\node [circle,draw,my label={red,below:X}] {my circle};
允許實現目標。
嗯:
\documentclass[tikz,png,border=10pt]{standalone}
\usepackage{tikz}
\tikzset{my label/.style args={#1:#2}{
append after command={
(\tikzlastnode.north) node [#1] {#2}
}
}
}
\begin{document}
\begin{tikzpicture}
\node [circle,draw,my label={red,below:X}] {my circle};
\node [circle,draw,my label={red,below=0.15cm:X},xshift=2cm] {my circle};
\end{tikzpicture}
\end{document}
結果:
修訂
正如評論中所述,如果應引入多個標籤,則前一種方法會受到影響。但是,如果我們不引入節點,而是引入帶有標籤的座標,那麼這是完全可行的。
代碼:
\documentclass[tikz,png,border=10pt]{standalone}
\usepackage{tikz}
\tikzset{my label/.style args={[#1]#2:#3}{
append after command={
(\tikzlastnode.center) coordinate[label={[label distance=0.1cm,#1]#2:#3}]
}
},
}
\begin{document}
\begin{tikzpicture}
\node [circle,
draw,
my label={[red]above:X},
my label={[green!70!blue]below:Y},
my label={[blue,yshift=0.3cm]above:Z}] {my circle};
\node [circle,
draw,
my label={[blue,yshift=-0.15cm]below:X},
my label={[green!70!blue]70:Z},
my label={[orange!80!red]130:Y},
xshift=2cm] {my circle};
\end{tikzpicture}
\end{document}
結果:
注意:透過在選項定義中使用label distance
,允許在只說以下內容時不重疊標籤文字:
\tikz\node [circle,draw,my label={[red]below:X}] {my circle};
答案3
您也可以移動標籤:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikz \node [circle,draw,label={[red,yshift=-0.5cm]above:X}] {my circle};
\tikz \node [circle,draw,label={[red,label distance=-0.5cm]above:X}] {my circle};
\end{document}