
我想定義一個 TikZ 節點樣式,將節點形狀設定為rectangle split
並自動新增具有由樣式定義的內容的第一部分。通常的節點文字出現在第二部分。有沒有辦法做到這一點,或類似的事情?
期望的行為
代碼:
\node[achtung] at (1, 2) {Falling rocks};
輸出:
( Achtung | Falling rocks )
嘗試
我試過了
achtung/.append style={
execute at begin node={Achtung\nodepart{two}},
rectangle split,
rectangle split horizontal,
rectangle split parts=2
}
以及更複雜的版本,例如
achtung/.append style={
execute at begin node={Achtung\protect\nodepart{two}\protect\begingroup},
execute at end node={\protect\endgroup},
rectangle split,
rectangle split horizontal,
rectangle split parts=2,
every one node part/.style={
execute at begin node={},
execute at end node={}
},
every two node part/.style={
execute at begin node={},
execute at end node={}
}
}
它們都會導致"Extra }, or forgotten \\endgroup.\n\\pgfutil@reserved@c ->\\egroup \n"
錯誤。
使用編譯的版本\pgfnodeparttwobox
,但沒有給出所需的行為。
最小的不工作範例
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\tikzset{
achtung/.append style={
execute at begin node={Achtung\nodepart{two}}, % remove this line to compile successfully
rectangle split,
rectangle split horizontal,
rectangle split parts=2,
draw
}
}
\begin{document}
\begin{tikzpicture}
\node[achtung] {Falling rocks};
\end{tikzpicture}
\end{document}
答案1
這個鉤子execute at begin node
對於使用節點部分來說還為時過早。這裡確實沒有合適的鉤子可以使用。我們可以嘗試濫用將在開始時執行的其他宏之一 - 它們處理水平對齊和嵌套 TikZ 圖片(?) - ...
但是,我不會亂搞它們並造成不相容性,而是添加我們自己的鉤子print at begin node
,其作用類似於node contents
但不是替換它,而是簡單地在{
和之間添加前綴}
。
(這個鉤子將不是當 TikZ 圖片是節點內容的一部分時會被重置,但一開始就這樣做並不是一個好主意。
程式碼
\documentclass[tikz]{standalone}
%\documentclass{article}
%\usepackage{tikz}
\ExplSyntaxOn \makeatletter
\tl_replace_once:Nnn \tikz@do@fig
{ \ignorespaces }
{ \pgfkeysvalueof{/tikz/print~at~begin~node} \ignorespaces }
\makeatother \ExplSyntaxOff
\pgfkeyssetvalue{/tikz/print at begin node}{}
\usetikzlibrary{shapes.multipart}
\tikzset{
achtung/.append style={
print at begin node = Achtung \nodepart{two},
rectangle split, rectangle split horizontal, rectangle split parts=2, draw}}
\tikzset{
gnutcha/.append style={
print at begin node = \nodepart{two} Achtung \nodepart{one},
rectangle split, rectangle split horizontal, rectangle split parts=2, draw}}
\begin{document}
\tikz \node[achtung] {Falling rocks}
node[gnutcha] at (0, -1) {Falling rocks};
\tikz \node[achtung] {Falling rocks \nodepart{text} You're not the }
node[achtung] at (0, -1) {Falling rocks \nodepart{one} boss of me. };
\end{document}
輸出
答案2
使用node contents
您可以node contents={Achtung\nodepart{two}#1}
在樣式中使用,並將其用作achtung={Falling rocks}
.正如評論中所述,使用node contents
意味著你不能這樣做\node [..] at (x,y)...
,但\node at (x,y) [...]
確實有效。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\tikzset{
achtung/.append style={
rectangle split,
rectangle split horizontal,
rectangle split parts=2,
draw,
node contents={Achtung\nodepart{two}#1}
},
achtung/.default=
}
\begin{document}
\begin{tikzpicture}
\node[achtung={Falling rocks}];
\end{tikzpicture}
\end{document}
答案3
用命令?
編輯:使用 Alan Munn 的評論透過添加節點的位置作為參數以及D(){}
#2 參數的規範
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\NewDocumentCommand{\mynode}{ O{achtung} d() m }{
\IfNoValueTF{#2}
{\path node[#1,right]{#1\nodepart{two}#3};}
{\path (#2) node[#1,right]{#1\nodepart{two}#3};}
}
\tikzset{
achtung/.style={
rectangle split,
rectangle split horizontal,
rectangle split parts=2,
draw,
}
}
\begin{document}
\begin{tikzpicture}
\draw[help lines](0,0)grid(3,2);
\mynode[achtung] (2,1) {Falling rocks}
\mynode (0,0) {achtung is the default style}
\end{tikzpicture}
\end{document}