[Tex/LaTex] TikZ Fill for Fraction Diagram

fillbetweenfractionstikz-pgf

I'm trying to shade in one third of this circle. Obviously, it's not working. What did I do wrong?

I got no error messages for either of my attempts in the code below, but neither filled anything in. Both just gave this circle.

not yet one third

\documentclass[12pt,letterpaper]{article}

\usepackage{tikz}
\usepackage{xcolor}

\begin{document}

How to shade in $\frac{1}{3}$ of this?

\begin{tikzpicture}
\draw (0,0) circle (3cm);
\draw (90:3)--(0,0);
\draw (210:3)--(0,0);
\draw (330:3)--(0,0);
\fill[gray] arc[start angle=-30, end angle =90, radius=3cm];
\end{tikzpicture}

% In this attempt, I put the color=gray part in the same square brackets as all the other stuff.
\begin{tikzpicture}
\draw (0,0) circle (3cm);
\draw (90:3)--(0,0);
\draw (210:3)--(0,0);
\draw (330:3)--(0,0);
\fill arc[color=gray, start angle=-30, end angle =90, radius=3cm];
\end{tikzpicture}

\end{document}

Best Answer

Considering the intended application, the following may be a useful starting point...

\documentclass[tikz, border=5]{standalone}
\newcount\segmentsleft
\tikzset{pics/.cd,
  circle fraction/.style args={#1/#2}{code={%
\segmentsleft=#1\relax
\pgfmathloop
\ifnum\segmentsleft<1\else
\ifnum\segmentsleft<#2 \edef\n{\the\segmentsleft}\else\def\n{#2}\fi
\begin{scope}[shift={(\pgfmathcounter,0)}]
\foreach \i [evaluate={\a=360/#2*(\i-1)+90;}] in {1,...,\n}
  \fill[fill=gray] (0,0) -- (\a:3/8) arc (\a:\a+360/#2:3/8) -- cycle;
\draw circle [radius=3/8];
\ifnum#2>1
  \foreach \i [evaluate={\a=360/#2*(\i-1);}] in {1,...,#2}
    \draw (0,0) -- (90+\a:3/8);
\fi
\end{scope}
\advance\segmentsleft by-#2
\repeatpgfmathloop
  }}
}

\begin{document}
\begin{tikzpicture}
\foreach \numerator/\denominator [count=\y] 
  in {1/1, 1/3, 2/4, 3/5, 8/8, 4/1, 10/3, 20/6, 30/7, 40/15}{
  \node at (-1/2,-\y) {$\frac{\numerator}{\denominator}$};
  \pic  at (0, -\y) {circle fraction={\numerator/\denominator}};
}
\end{tikzpicture}

enter image description here

For more general shapes, one can assume that each division is the same shape (if it isn't it's going to get tricky). So, the minimum that is required is

  • code to shift to the position for the "containing" shape (e.g., circle)
  • code to shift to the appropriate position for the ith shape-division
  • code to determine how the ith shape-division is drawn
  • code to draw each shape-division (e.g., circular sector).

Here is a reasonably general solution illustrated with a triangle style:

\documentclass[tikz, border=5]{standalone}
\newcount\tikzfractiondenominator
\newcount\tikzfractionnumerator
\def\tikzfractionempty{}
\let\tikzfractionstyle=\tikzfractionempty
\newif\iftikzfractionfill
\tikzset{pics/.cd,
  fraction/.style={%
    code={%
      \tikzset{pics/fraction/.cd, #1}%
      \pgfmathparse{int(ceil(\tikzfractionnumerator/\tikzfractiondenominator))}%
      \let\tikzfractionshapetotal=\pgfmathresult
      \ifx\tikzfractionstyle\tikzfractionempty
      \else%
        \pgfmathloop
          \ifnum\tikzfractionnumerator<1
        \else
          \pgfmathsetmacro\tikzfractionproper{int(\tikzfractionnumerator?\tikzfractionnumerator:\tikzfractiondenominator)}%
          \foreach \tikzfractionsegmentnumber in {1,...,\tikzfractiondenominator}{%
            \ifnum\tikzfractionsegmentnumber>\tikzfractionproper\relax%
              \tikzfractionfillfalse%
            \else%
              \tikzfractionfilltrue%
            \fi%
            \let\tikzfractionshapenumber=\pgfmathcounter%
            \begin{scope}
              \tikzset{pics/fraction/\tikzfractionstyle/shape position/.try}%
              \tikzset{pics/fraction/\tikzfractionstyle/segment position/.try}%
              \tikzset{pics/fraction/\tikzfractionstyle/segment draw/.try}%
            \end{scope}
          }% 
          \advance\tikzfractionnumerator by-\tikzfractiondenominator%
        \repeatpgfmathloop%
      \fi%
    }
  },
  fraction/.cd,
    style/.store in=\tikzfractionstyle,
    numerator/.code=\pgfmathsetcount\tikzfractionnumerator{#1},
    denominator/.code=\pgfmathsetcount\tikzfractiondenominator{#1},
    fraction/.style args={#1/#2}{%
      /tikz/pics/fraction/.cd,
        numerator={#1}, denominator={#2}
    }
}
\tikzset{%
  /tikz/pics/fraction/triangles/.cd,
    shape position/.code={
      \pgfmathsetmacro\y{sqrt(\tikzfractiondenominator)}
      \tikzset{
        shift=(0:{(\tikzfractionshapenumber-1)*\y}),
        shift={(0,\y/4)},
      }
    },
    segment position/.code={
      \let\i=\tikzfractionsegmentnumber
      \pgfmathsetmacro\z{int(sqrt(\i-1))}
      \pgfmathsetmacro\q{\i-(\z)^2}
      \tikzset{
        shift={({sin(60) * (\q-\z) / 2}, {-\z*0.75 -mod(\q,2)*cos(60)/2})},
        rotate={mod(\q-1,2)*180}
      }
    },
    segment draw/.code={
      \iftikzfractionfill
        \tikzset{triangle fill/.style={blue!50!cyan!50}}
      \else
        \tikzset{triangle fill/.style={gray!20}}
      \fi
      \fill [triangle fill] (90:0.45) -- (210:0.45) -- (330:0.45) -- cycle;
    }
}
\begin{document}
\begin{tikzpicture}
\foreach \numerator/\denominator [count=\y]  in {1/1, 2/4, 13/9}{
\tikzset{shift=(270:\y*2)}
\pic {fraction={style=triangles, fraction={\numerator/\denominator}}};
\node at (-1,0)  {$\frac{\numerator}{\denominator}$};
}
\end{tikzpicture}
\end{document}

enter image description here

Reusing the fraction pic defined above (not shown below), it is then possible to be a bit more extravagant:

\tikzset{%
  /tikz/pics/fraction/petals/.cd,
    shape position/.code={
      \tikzset{
        shift=(360/\tikzfractionshapetotal*\tikzfractionshapenumber:2)
      }
    },
    segment position/.code={
      \tikzset{
        rotate=(360/\the\tikzfractiondenominator*\tikzfractionsegmentnumber)
      }
    },
    segment draw/.code={
      \iftikzfractionfill
        \tikzset{petal/.style={bottom color=purple, top color=pink}}
      \else
        \tikzset{petal/.style={bottom color=yellow!50, top color=orange!50}}
      \fi
      \pgfmathparse{180/\tikzfractiondenominator}%
      \let\r=\pgfmathresult
      \path [petal] (0:0) [rounded corners=1ex] -- 
        (-\r:0.5) -- (0:.75) -- (\r:0.5) -- cycle;
    }
}
\begin{tikzpicture}
\pic {fraction={style=petals, fraction={53/8}}};
\node {$\frac{53}{8}$};
\end{tikzpicture}

enter image description here