TikZ: Drawing a rectangle and then splitting it

Here's one option, that you can then convert to an animated GIF:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}

\newlength\CellWd
\setlength\CellWd{1.5cm}

\newcommand\DivRec[3]{%
\node<+->[draw,text width=6\CellWd,minimum height=30pt] (#3) {};
\foreach \a/\texto in {#2}
{\draw<+-> let 
  \p1=(#3.south west),
  \p2=( $ (#3.north east) - (#3.north west) $ ),
  \n1={veclen(\x2,\y2)/#1}
  in (\x1+\a*\n1,0|-#3.north) -- (\x1+\a*\n1,0|-#3.south);
\path let 
  \p1=(#3.south west),
  \p2=( $ (#3.north east) - (#3.north west) $ ),
  \n1={veclen(\x2,\y2)/#1}
  in  node[xshift=-\n1/2] at (\x1+\a*\n1,0|-#3.center) {\texto};
  }
}

\begin{document}

\begin{frame}
\centering
\begin{tikzpicture}
\DivRec{6}{1/m,2/z,3/a,4/d,5/l,6/v}{rect}
\end{tikzpicture}    
\end{frame}

\end{document}

The syntax is

\DivRec{<number of divisions>}{<part/text>}{<name of node>}

where <part/text> ias a comma separated list of the form 1/text1,2/text2,.... The length \CellWd together with the firat argument of \DivRec control the width for each subdivision of the rectangle and the total width (=<number of divisions>*\CellWd).

I used the code above and ImageMagick with

convert -delay 80 -density 300 test.pdf test.gif

to produce

enter image description here


This is an alternative solution.

It uses a rectangle split like in OP's code but avoids to initially draw the split lines with rectangle split draw splits=false.

Once the node is drawn, each split line is added with an iterative command into a path picture option in node's style.

This solution has been possible thank you to percusse who provided the function \numname to convert numbers to text and marmot who wrote \GetCurrentNodeName macro based in Jake's solution to know current node's name.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\newcommand\numname[1]{%
  \ifcase#1zero\or one\or two\or 
  three\or four\or five\or six\or 
  seven\or eight\or nine\fi%
}

\makeatletter
\newcommand{\GetCurrentNodeName}{\tikz@fig@name}
\makeatother

\tikzset{
    my shape/.style={
        rectangle split,
        rectangle split horizontal,
        rectangle split part align=base,
        rectangle split parts = #1,
        draw,
        minimum height=1cm,
        text width=1cm,
        align=center,
        anchor=center,
        rectangle split draw splits=false,
        path picture={
            \foreach \i [count=\ni] in {2,...,#1}
                \draw<+-> (\GetCurrentNodeName.\numname{\ni} split north)--
                (\GetCurrentNodeName.\numname{\ni} split south);
        }
    }
}

\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}[
    ]
\node<+->[my shape=5] (mynode) {
    a
    \nodepart{two}
    b
    \nodepart{three}
    c
    \nodepart{four}
    d
    \nodepart{five}
    e
    };
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

Tags:

Tikz Pgf