7

I want to make a rectangle and then want to split it. My MWE is below:

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.multipart}
\begin{document}
\begin{tikzpicture}[
    my shape/.style={
          rectangle split
        , rectangle split parts=#1
        , draw
        , anchor=center
        }
    ]
\node [my shape=1, rectangle split horizontal] at (0, 0) (R1A1) {};
\node[above of=R1A1]{Test};
\node [my shape=5, rectangle split horizontal] at (0, 0)
    {a \nodepart{two}b \nodepart{three}c \nodepart{four}d \nodepart{five}e};
\end{tikzpicture}    
\end{document}

This code is not working as desired. Any help will be highly appreciated. Thanks

MYaseen208
  • 8,587
  • And how exactly should it look like? What's the desired output? – Gonzalo Medina Aug 08 '13 at 15:58
  • @GonzaloMedina: I want to have a rectangle and then want to split it into parts. With my code the first rectangle is small in size. Any guideline how to get the rectangle of same size as with the last command. – MYaseen208 Aug 08 '13 at 16:00
  • Sorry, but I am still confused. Why don't you use rectangle split (as you're already doing in your code)? – Gonzalo Medina Aug 08 '13 at 16:08
  • Sorry @GonzaloMedina for confusion. Actually I want to use animation for the split of many rectangles. Therefore in first step I want to have a rectangle and then splitting the same rectangle into parts. I hope this will help to make clarification. Thanks – MYaseen208 Aug 08 '13 at 16:12

2 Answers2

4

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

Gonzalo Medina
  • 505,128
  • Thanks @GonzaloMedina for your help. I wonder how to get text withing those portions. – MYaseen208 Aug 08 '13 at 16:59
  • @MYaseen208 you should have mentioned that since the beginning. What kind of text? How should the text appear in the animation? – Gonzalo Medina Aug 08 '13 at 17:01
  • Sorry for this confusion. Please see \nodepart in my original code. Again thanks for your all help. – MYaseen208 Aug 08 '13 at 17:04
  • @MYaseen208 so you want to place some text on each part. Should the texts always be visible or should they appear step-wise as the divisions are drawn? Should the width of all divisions be constants or should they adapt to the text width? – Gonzalo Medina Aug 08 '13 at 17:13
  • Text should be visible always and all divisions should be constants. – MYaseen208 Aug 08 '13 at 17:14
  • @MYaseen208 please see my updated answer; is it something like that what you need? – Gonzalo Medina Aug 08 '13 at 17:29
3

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

Ignasi
  • 136,588