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.


\number\numexprhere, you can use directly #1 or #2. It's not like the problem with\foreach– Alain Matthes May 24 '12 at 19:59