4

with this MWE

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

\newcounter{CountOfSections}
\newcommand{\fracgraph}[3][1]{%
    % #1 = optional height
    \begin{tikzpicture}
        \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
\begin{document}

\fracgraph{12}{7/pink,10/gray!50}

\end{document}

I can create something like this:enter image description here

I found this code online and it is very handy. But I wish to adopt it a little, so that I can draw - for example - 6/7 and 8/10. The other recangles I wish to be white. How can I do that?

Well, I actually came closer to the solution, with adopting a few lines. I changed \foreach \Size/\Options in {#3} into \foreach \Number/\Size/\Options in {#3} and I added \draw [fill=\Options] (0,-\YCoord) rectangle (#2*\Number/\Size,-\YCoord+#1);

My new MWE is

\usepackage{amsmath, tikz}
\usetikzlibrary{calc}

\newcounter{CountOfSections}
\newcommand{\fracgraph}[3][1]{%
    % #1 = optional height
    \begin{tikzpicture}
        \setcounter{CountOfSections}{0}%
        \foreach \Number/\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] (0,-\YCoord) rectangle (#2*\Number/\Size,-\YCoord+#1);
                \draw [fill=white]  ($(\Xcoord-\Xincrement,-\YCoord)$)  rectangle ($(\Xcoord,-\YCoord+#1)$);
                \node at ($(\XcoordLabel,-\YCoord+0.5*#1)$) {$\frac{1}{\Size}$};
                
            }%
        }%
    \end{tikzpicture}
}

%-----------------------------------------------------------
% begin document
\begin{document}

\fracgraph{12}{6/7/pink,8/10/gray!50}

\end{document} 

and it produces:

enter image description here

I wanna see the fractions on the entire pictures. How can I do that?

2
  • 1
    Do you want to show 6/7 and 8/10 at the center of pink and gray box?
    – Tom
    Nov 22, 2023 at 15:31
  • @Tom That would be nice, but I would prefer, if the vertical lines would stay and in every box 1/7 or 1/10 is printed. I wish to have the color in the background Nov 22, 2023 at 15:33

2 Answers 2

4

The following variation of the code can do what you want. I streamlined the code a bit. The command \fracgraph now takes as second argument a list of values that consist of thee parts separated by slashes (instead of only two as before). The second part marks the last segment that should be filled.

To switch between the different values passed to fill, I make use PGF's ifthenelse function.

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

\newcounter{CountOfSections}
\newcommand{\fracgraph}[3][1]{%
    % #1 = optional height
    \begin{tikzpicture}
        \foreach \Size/\Stop/\Options [count=\c] in {#3} {
            \pgfmathsetmacro{\YCoord}{#1*\c}
            \pgfmathsetmacro{\Xincrement}{#2/\Size}
            \foreach \x in {1,...,\Size} {
                \pgfmathsetmacro{\Xcoord}{\x*\Xincrement}
                \pgfmathsetmacro{\XcoordLabel}{(\x-0.5)*\Xincrement}
                \pgfmathsetmacro{\XCoordFill}{ifthenelse(\x > \Stop, "none", "\Options")}
                \draw[fill=\XCoordFill] 
                    ($(\Xcoord-\Xincrement,-\YCoord)$) rectangle ($(\Xcoord,-\YCoord+#1)$);
                \node at ($(\XcoordLabel,-\YCoord+0.5*#1)$) {$\frac{1}{\Size}$};
            }%
        }%
    \end{tikzpicture}
}

%-----------------------------------------------------------
% begin document
\begin{document}

\fracgraph{12}{7/3/pink,10/9/gray!50}

\end{document}

enter image description here

3
  • Thank you, that is just awesome :) Nov 22, 2023 at 15:48
  • Qrrbrbirlbel's answer streamlines the code even more. Choose your favorite =) Nov 22, 2023 at 15:49
  • Yes, I saw it, but with your answer it was easier for me to introduce a small white line between the 2 fractions ;) Nov 22, 2023 at 16:08
7

With the help of the TeX primitive \ifnum we can make this basically a oneliner.

The size of the diagram is controlled by the values to x and y where – ignoring the line width –

  • x = 12cm specifies the width of the diagram to be 12cm and
  • y = 1cm specifies the height of one row.

Instead of a LaTeX counter CountOfSections we can just use the inbuilt PGFFor option count = \Row.

The nodes are placed halfway between the corner of each rectangle.

A little more work is necessary if \Style, i.e. the third variable of your list entries isn't just a color but should also change other settings.

Code

\documentclass[tikz]{standalone}
\usepackage{amsmath}
\newcommand*\fracgraph[2][]{%
  \tikz[x=12cm,y=1cm,#1]
    \foreach[count=\Row] \Number/\Size/\Style in {#2}
      \foreach \NUMBER in {0, ..., \pgfinteval{\Size-1}}%
 % or \foreach[count=\NUMBER from 0]\throwaway in {1, ..., \Size}
        \draw[fill=\ifnum\NUMBER<\Number\Style\else none\fi] (\NUMBER/\Size, -\Row)
          rectangle node {$\tfrac{1}{\Size}$} +(1/\Size,-1);%
}
\begin{document}
\fracgraph{6/7/pink, 8/10/gray!50}
\end{document}

Output

enter image description here

1
  • Thank you, that is awesome :) and also so much shorter :) Nov 22, 2023 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .