为了在节点中设置子节点,我使用以下代码(由 Mark Wibrow 提供):
\documentclass[border=3mm,
tikz,
preview]{standalone}
\makeatletter
\def\tikzsavelastnodename#1{\let#1=\tikz@last@fig@name}
\makeatother
\begin{document}
\begin{tikzpicture}[
add text/.style args={#1:#2}{append after command =
{node[rounded corners, fill=blue!10] at (\tikzsavednodename.#1) [anchor=#1] {#2}}
},
box/.style={rectangle, draw, densely dotted, thick,
text width=3.5cm, minimum height=2.5cm,
align=center,
append after command = {\pgfextra{\tikzsavelastnodename\tikzsavednodename}},#1
},
]
\node[box,
label=above:label, % <--- work fine
add text=north:lorum,
add text=south west:ipsum,
add text=east:dolor,
% label=above:test % <--- gives error "no shape named is known"
] {main\\ content};
\end{tikzpicture}
\end{document}
这使:
如果主节点的标签add text
在节点选项之前,此代码可以正常工作。出于某些(重要)原因,我喜欢add text
在宏之后放置标签。有没有办法在节点定义中告诉必须在节点选项之间最后执行,因此节点选项中的和的add text
顺序不再重要?label
add text
答案1
你可以这样做,尽管我完全不清楚你为什么需要这样做,label
所以我完全不确定这是否是一个选择。
\documentclass[tikz,multi,border=10pt]{standalone}
% code for sub-nodes (in the non-tikzmark sense) from Mark Wibrow: https://groups.google.com/d/msg/comp.text.tex/yunrzHUfSUk/y3x-QWP7sJ0J
\makeatletter
\def\tikzsavelastnodename#1{\let#1=\tikz@last@fig@name}
\makeatother
\begin{document}
\begin{tikzpicture}
[
add text/.style args={#1:#2}{append after command =
{node [rounded corners, fill=blue!10] at (\tikzsavednodename.#1) [anchor=#1] {#2}}
},
my label/.style args={#1:#2:#3}{append after command =
{node [every label] at (\tikzsavednodename.#1) [anchor=#2] {#3}}
},
box/.style={rectangle, draw, densely dotted, thick,
text width=3.5cm, minimum height=2.5cm,
align=center,
append after command = {\pgfextra{\tikzsavelastnodename\tikzsavednodename}},
#1,
},
]
\node [box={draw=red},
label=above:label, % <--- work fine
add text=north:lorum,
add text=south west:ipsum,
add text=east:dolor,
my label=south:north:test % <--- also works
] {main\\ content};
\end{tikzpicture}
\end{document}