Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
And how exactly should it look like? What's the desired output? –  Gonzalo Medina Aug 8 '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 8 '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 8 '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 8 '13 at 16:12

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer
    
Thanks @GonzaloMedina for your help. I wonder how to get text withing those portions. –  MYaseen208 Aug 8 '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 8 '13 at 17:01
    
Sorry for this confusion. Please see \nodepart in my original code. Again thanks for your all help. –  MYaseen208 Aug 8 '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 8 '13 at 17:13
    
Text should be visible always and all divisions should be constants. –  MYaseen208 Aug 8 '13 at 17:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.