2

I would like to use foreach constructions to draw lines between multipart nodes. But I find the problem that multipart anchors are declared with a textual names: one, two, ... instead of numbers.

I've tried to use conversion functions from fmtcount or numname packages but although they work inside node's text, I'm not able to use for anchors reference. Is it possible?

\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}
Ignasi
  • 136,588

1 Answers1

3

Probably the macro is not expandable (I didn't check) but you can basically define a simple switch and then it works

\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}

enter image description here

percusse
  • 157,807
  • Nice trick. It will be enough for me. – Ignasi Apr 12 '18 at 10:23
  • @Ignasi I've found this as an expandable alternative https://tex.stackexchange.com/questions/279954/ which looks like the same thing with more sophistication – percusse Apr 12 '18 at 10:24