我想使用foreach
构造在多部分节点之间画线。但我发现问题是多部分anchors
是用文本名称声明的:one, two, ...
而不是数字。
我尝试使用fmtcount
或numname
包中的转换函数,但尽管它们在节点文本中有效,但我无法将其用作锚点引用。这可能吗?
\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{shapes.multipart, positioning}
\usepackage{fmtcount}
\begin{document}
\begin{tikzpicture}[
my shape/.style={
rectangle split
, rectangle split parts=#1
, draw
, anchor=center
}
]
\node[my shape=3] (main){
\numberstringnum{1}
\nodepart{two}
\numberstringnum{2}
\nodepart{three}
\numberstringnum{3}};
\node[my shape=3, right=2cm of main] (second)
{
\numberstringnum{1}
\nodepart{two}
\numberstringnum{2}
\nodepart{three}
\numberstringnum{3}};
\draw (main.one east) -- (second.two west);
%following lines fail.
%\draw (main.\numberstringnum{2} east) -- (second.\numberstringnum{3} west);
%\foreach \i in {1,...,3}
% \draw (main.\numberstringnum{\i} east) -- (second.\numberstringnum{\i} west);
\end{tikzpicture}
\end{document}
答案1
宏可能不可扩展(我没有检查)但你基本上可以定义一个简单的开关,然后它就可以工作了
\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{shapes.multipart, positioning}
\def\numname#1{%
\ifcase#1zero\or one\or two\or three\or four\or five\or six\or seven\or eight\or nine\fi%
}
\begin{document}
\begin{tikzpicture}[
my shape/.style={rectangle split, rectangle split parts=#1 , draw, anchor=center}]
\node[my shape=3] (main){\numname{1}\nodepart{two}\numname{2}\nodepart{three}\numname{3}};
\node[my shape=3, right=2cm of main] (second)
{\numname{1}\nodepart{two}\numname{2}\nodepart{three}\numname{3}};
\draw (main.one east) -- (second.two west);
\draw (main.{\numname{2} east}) -- (second.\numname{3} west);
\foreach \i in {1,...,3}
\draw (main.\numname{\i} east) -- (second.\numname{\i} west);
\end{tikzpicture}
\end{document}