# tikz/pgf-pie: wrap Pie Chart in \newcommand

Trying to represent rational numbers using pie charts in a document as this one:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgf-pie}

\begin{document}

\begin{tabular}{|p{4cm}|p{4cm}|p{4cm}|}

$\frac{1}{2}$ & $\frac{2}{2}$ \\

% 1/2
\begin{tikzpicture}

\pie[rotate=90, radius=2, sum=auto, color={white,blue}, /tikz/nodes={opacity=0,overlay}] {1 , 1 }

\end{tikzpicture}

&

% 2/2
\begin{tikzpicture}

\pie[rotate=90, radius=2, sum=auto, color={blue,blue}, /tikz/nodes={opacity=0,overlay}] {1 , 1 }

\end{tikzpicture}

\\

$\frac{1}{3}$ & $\frac{2}{3}$ & $\frac{3}{3}$ \\

% 1/3
\begin{tikzpicture}

\pie[rotate=90, radius=2, sum=auto, color={white,white,blue}, /tikz/nodes={opacity=0,overlay}] {1 , 1, 1 }

\end{tikzpicture}

&

% 2/3
\begin{tikzpicture}

\pie[rotate=90, radius=2, sum=auto, color={white,blue,blue}, /tikz/nodes={opacity=0,overlay}] {1 , 1, 1 }

\end{tikzpicture}

&

% 3/3
\begin{tikzpicture}

\pie[rotate=90, radius=2, sum=auto, color={blue,blue,blue}, /tikz/nodes={opacity=0,overlay}] {1 , 1, 1 }

\end{tikzpicture}

\end{tabular}

\end{document}


the document fulfills more or less what is expected. However, it can be improved with a newcommand that creates each pie chart. The new command should have two arguments, corresponding to the denominator and numerator. Something like:

\newcommand{\rationalpie}[2]{
\begin{tikzpicture}

\pie[rotate=90, radius=2, sum=auto, color={white,blue,blue}, /tikz/nodes={opacity=0,overlay}] {1 , 1, 1 }

\end{tikzpicture}
}


that could be used as:

\rationalpie{2}{3}


The problem is: how to define this newcommand ? The fixed list {1,1,1} must be replaced by so many "1" as the denominator argument and the list {white,blue,blue} must be replaced by some many elements as denominator with "blue" repeated numerator times.

Here is an implementation using token lists and the forloop package.

\documentclass{article}
\usepackage{pgf-pie}
\usepackage{forloop}
\newtoks\colors
\newtoks\parts
\newcounter{parts}
\newcommand\rationalpiex[2]%
{\begin{tikzpicture}
\pie[rotate=90, radius=2, sum=auto, color={#1}, /tikz/nodes={opacity=0,overlay}] {#2}
\end{tikzpicture}%
}
\newcommand\rationalpie[2]%
{\colors{}\parts{}%
\forloop[-1]{parts}{#2}{\value{parts} > 0}%
{\parts\expandafter{\the\parts1}%
\ifthenelse{\value{parts} > #1}%
{\colors\expandafter{\the\colors white}}%
{\colors\expandafter{\the\colors blue}}%
\ifthenelse{\value{parts} > 1}%
{\parts\expandafter{\the\parts,}%
\colors\expandafter{\the\colors,}%
}%
{}%
}%
\edef\rationalpiexx{\noexpand\rationalpiex{\the\colors}{\the\parts}}%
\rationalpiexx
}

\begin{document}
\begin{tabular}{|p{4cm}|p{4cm}|p{4cm}|}
$\frac12$      & $\frac22$                      \\
\rationalpie12 & \rationalpie22                 \\
$\frac13$      & $\frac23$      & $\frac33$     \\
\rationalpie13 & \rationalpie23 & \rationalpie33
\end{tabular}
\end{document}

• Thanks for the answer. However, taken into account the complexity of the syntax, I've decided to use any scripting language (scala, python, ...) and generate with it the .tex file. Feb 26, 2017 at 10:52

Here is an implementation that does not use any additional packages.

Once you created the lists properly, for instance by

\gdef\card{1}
\foreach \i in {1,...,\numexpr#2-1\relax} {\xdef\card{\card,1}}
\ifnum#1=#2
\gdef\select{blue}
\foreach \i in {1,...,\numexpr#2-1\relax} {\xdef\select{\select,blue}}
\else
\gdef\select{white}
\foreach \i in {1,...,\numexpr#1\relax} {\xdef\select{\select,blue}}
\foreach \i in {1,...,\numexpr#2-#1-1\relax} {\xdef\select{\select,white}}
\fi


where #1 is the numerator and #2 the denominator, it is basically an expansion problem which can be solved by the old trick

\begingroup\edef\x{\endgroup
\noexpand\expandafter\noexpand\pie\piearguments}\x


assuming that

\def\piearguments{[rotate=90, radius=2, sum=auto, color={\select}, /tikz/nodes={opacity=0,overlay}]{\card}}


Complete example:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgf-pie}

\newcommand*\rationalpie[2]{%
\gdef\card{1}
\foreach \i in {1,...,\numexpr#2-1\relax} {\xdef\card{\card,1}}
\ifnum#1=#2
\gdef\select{blue}
\foreach \i in {1,...,\numexpr#2-1\relax} {\xdef\select{\select,blue}}
\else
\gdef\select{white}
\foreach \i in {1,...,\numexpr#1\relax} {\xdef\select{\select,blue}}
\foreach \i in {1,...,\numexpr#2-#1-1\relax} {\xdef\select{\select,white}}
\fi
\begin{tikzpicture}
\begingroup\edef\x{\endgroup
\noexpand\expandafter\noexpand\pie\piearguments}\x
\end{tikzpicture}
}

\begin{document}

\begin{tabular}{|p{4cm}|p{4cm}|p{4cm}|}

$\frac{1}{2}$ & $\frac{2}{2}$ \\

% 1/2
\rationalpie{1}{2}

&

% 2/2
\rationalpie{2}{2}

\\

$\frac{1}{3}$ & $\frac{2}{3}$ & $\frac{3}{3}$ \\

% 1/3
\rationalpie{1}{3}

&

% 2/3
\rationalpie{2}{3}

&

% 3/3
\rationalpie{3}{3}

\end{tabular}

\end{document}