4

I found this code from Creating a fraction graph in TikZ and would like to modify it so that it fills an entire page. The goal is to create a coloured sheet handout with the fraction strips in diagram that fills the page. The truth is, I don't understand the code that was used to make the diagram so I am unsure how to modify it to fill the page.

\documentclass{exam}
\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}
\centering
    \fracgraph{12}{2/cyan!50,3/red!40,4/brown!50,5/cyan!50,6/red!20,8/blue!30,10/magenta!30,12/green!40}
\end{document}

I see how there is an optional height but not sure what it does.

The goal is to create something like the following image of fraction strips. fraction strips image

1 Answer 1

8

Here is a somewhat simpler approach. \fracgraph takes two arguments, one optional. The required argument is a comma-separated list of positive integers and colors. The optional argument is the height of each row (default=1cm). The spacing between each row is 25% of the height. You can change that by adjusting the 1.25 factor in -1.25*\m*#1. So in the code below, if you wanted rows of height 1.5cm instead of 1cm, just use the optional argument:

\fracgraph[1.5cm]{2/cyan!50,3/red!40,4/brown!50,5/cyan!50,6/red!20,8/blue!30,10/magenta!30,12/green!40}

enter image description here

Here is the code:

\documentclass{article}

\usepackage{tikz}

\newcommand{\fracgraph}[2][1cm]{\begin{tikzpicture}
    \foreach \n/\c[count=\m] in {#2}{
        \foreach \k in {1,...,\n}{
            \node[draw, fill=\c, minimum height=#1, minimum width=\textwidth/\n] 
                at ({\textwidth/\n*(\k-.5)},-1.25*\m*#1){$\frac{1}{\n}$};}}
\end{tikzpicture}}

\begin{document}

{\centering
    \fracgraph{2/cyan!50,3/red!40,4/brown!50,5/cyan!50,6/red!20,8/blue!30,10/magenta!30,12/green!40}
}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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