8

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:

enter image description here

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.

azetina
  • 28,884
  • 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
  • 1
    I totally support your notion of using these kinds of visuals to help kids learn about fractions. I have 2 suggestions is that you make your fraction strips go to at least 3 wholes. In the short-run, that will slow things down. In the long-run, that will force them to compare/contrast fractional numbers with whole numbers, clarify mixed vs improper numbers, allow for more counting by fractions, etc. Also, the students are the ones who should be labeling your diagrams, not you! Feel free to use this. :) https://bit.ly/31URTZz – WeCanLearnAnything Oct 28 '19 at 18:37

1 Answers1

11

You can produce it as follows:

enter image description here

\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:

enter image description here

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}

Peter Grill
  • 223,288
  • 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
  • How can I modify the code if i want I do to draw separate rectangles? I mean, I would like to draw the four rectangles (white 1, blue 1/2, pink 1/3 and brown 1/4) separated by each other. Thank you @PeterGrill – ryuk Nov 01 '17 at 19:09
  • 1
    @ryuk: Adjusting the \Xincrement in each iteration should do it. If you run into difficulties, best to post a new question showing how you attempted to adpat the code above. – Peter Grill Nov 02 '17 at 23:31