# Creating a fraction graph in TikZ

Ok, so I was helping a friend of mine with a fraction problem on how to represent fractions so that they are easy to compare for children. We were thinking of a ruler and separate the ruler into pieces to represent the fractions and stack the rulers one on top of the other. And so we did, then I started to create something similar in TikZ.

This is what I have so far:

\documentclass{article}
\usepackage{amsmath,tikz}
\begin{document}
\begin{tikzpicture}
\draw (-2,3) rectangle (3,2);
\node at (0.5,2.5) {1};
\draw (-2,2) rectangle (3,1);
\draw (0.5,2)--(0.5,1);
\node at (-0.75,1.5) {$\frac{1}{2}$};
\node at (1.75,1.5) {$\frac{1}{2}$};
\end{tikzpicture}
\end{document}


This yields:

I was thinking on what would be the best way to do this drawing automatically by probably saying \fracgraph{2} or maybe \fracgraph{5}{2} where the first number controls the size of the diagram and the second controls the levels as to which the fractions go to; in this case it would be up to half.

This is what I have so far which is not much. I have only achieved the expansion of the graph (the rectangle containing it) up to the desired size but things like placing the nodes and splitting the diagram further, no.

\documentclass[letterpaper]{article}
\usepackage{amsmath, tikz}

\newcommand{\fracgraph}[2]{%
\pgfmathsetmacro{\meanfrac}{(0+#1)/2}
\begin{tikzpicture}
\draw (0,0) rectangle (#1,\number\numexpr-#2\relax);
\node at (\meanfrac,-\number\numexpr #2/2\relax){#2};
\end{tikzpicture}
}
\begin{document}
\fracgraph{5}{2}
\end{document}


Any help into this abstract desire will be appreciated.

-
You don't need \number\numexpr here, you can use directly #1 or #2. It's not like the problem with \foreach – Alain Matthes May 24 '12 at 19:59
I was just testing your last approach, am still learning some new ways. I had done it with #1 and #2 but I had to practice it so that it sticks in my head for a while. – azetina May 24 '12 at 20:03

You can produce it as follows:

\documentclass[letterpaper]{article}
\usepackage{amsmath, tikz}
\usetikzlibrary{calc}

\newcommand{\fracgraph}[3][2]{%
% #1 = optional height
\begin{tikzpicture}
\pgfmathsetmacro{\Yheight}{0.5*#1}%
\pgfmathsetmacro{\Xincrement}{#2/#3}%

\draw (0,0) rectangle (#2,#1);
\node at ($(0.5*#2,0.75*#1)$) {1};
\draw ($(0,\Yheight)$) -- ($(#2,\Yheight)$);
\foreach \x in {2,...,#3} {%
\pgfmathsetmacro{\Xcoord}{(\x-1)*\Xincrement}%
\draw ($(\Xcoord,0)$) -- ($(\Xcoord,)$);
}%
\foreach \x in {1,...,#3} {%
\pgfmathsetmacro{\XcoordLabel}{(\x-0.5)*\Xincrement}%
\node at ($(\XcoordLabel,0.5*\Yheight)$) {$\frac{1}{#3}$};
}%
\end{tikzpicture}
}
\begin{document}
\fracgraph{5}{2}

\bigskip
\fracgraph{5}{3}

\bigskip
\fracgraph{5}{4}
\end{document}


If you want to have just one diagram then I would suggest changing the syntax to something like:

\fracgraph{5}{2/cyan!50,3/red!40,4/brown!50}


where the text following the slash indicates the fill color to be applied to yield:

## Code:

\documentclass[letterpaper]{article}
\usepackage{amsmath, tikz}
\usetikzlibrary{calc}

\newcounter{CountOfSections}
\newcommand{\fracgraph}[3][1]{%
% #1 = optional height
\begin{tikzpicture}
\draw (0,0) rectangle (#2,#1) node [midway] {1};

\setcounter{CountOfSections}{0}%
\foreach \Size/\Options in {#3} {%
\stepcounter{CountOfSections}%
\pgfmathsetmacro{\YCoord}{#1*\arabic{CountOfSections}}%
\draw  (0,-\YCoord) rectangle (#2,-\YCoord+#1);
\pgfmathsetmacro{\Xincrement}{#2/\Size}%
\foreach \x in {1,...,\Size} {%
\pgfmathsetmacro{\Xcoord}{\x*\Xincrement}%
\pgfmathsetmacro{\XcoordLabel}{(\x-0.5)*\Xincrement}%
\draw [fill=\Options]  ($(\Xcoord-\Xincrement,-\YCoord)$)  rectangle ($(\Xcoord,-\YCoord+#1)$);
\node at ($(\XcoordLabel,-\YCoord+0.5*#1)$) {$\frac{1}{\Size}$};
}%
}%
\end{tikzpicture}
}
\begin{document}
\fracgraph{5}{2/cyan!50,3/red!40,4/brown!50}
\end{document}

-
Can the split of the rectangle remain to indicate for \fracgraph{5}{4} let say showing 1 then half below, a third below half and so on? In your example, you only show one then half or a third and so on and not the intermediate splittings. – azetina May 24 '12 at 20:17
@azetina: Ok, have updated solution. – Peter Grill May 24 '12 at 20:43