3

I am wondering if it is possible to create a pie chart with fractions instead of the default numbers inside the pie pieces (for one piece or for all pieces)? Thank you for your help.

Code:

\documentclass[fleqn,12pt, a4paper]{scrartcl}

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


\begin{document}
\begin{tikzpicture}

%%I want 1/6 instead of 1 inside the pieces:
\pie[sum=auto]
{1/,1/,1/,1/,1/,1/}

\end{tikzpicture}
\end{document}

enter image description here

2 Answers 2

5

EDITED to provide true \frac capability, all using the native keys provided by the pgf-pie package. Three versions are shown:

  1. \frac

  2. \displaystyle\frac, and

  3. #1/#2

First thing, is the fraction has to be provided in braces as {a/b}, so that tikz can evaluate it for calculating the pie-slice size. Then, in order to get the label as a \frac, one uses before number and after number keys to basically unpack and evaluate the fraction not as a math calculation, but rather as math typesetting.

I figured out this solution by looking at the guts of the pgf-pie package and realized that the macros \beforenumber and \afternumber were part of the typesetting. I, therefore, had to figure out how to define the keys to expand what I needed expanded, in order to evaluate \beforenumber\p\afternumber as, for example, \frac{1}{6}, when \p is defined as {1/6}.

The MWE:

\documentclass[fleqn,12pt, a4paper]{scrartcl}

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

\def\myfrac#1{\expandafter\expandafter\expandafter\expandafter\expandafter%
  \expandafter\expandafter\myfracaux\expandafter\expandafter\expandafter#1}
\def\myfracaux#1/#2\relax{$\piestyle\frac{#1}{#2}$}
\def\piestyle{\textstyle}
\begin{document}

\begin{tikzpicture}
%%I want 1/6 instead of 1 inside the pieces:
\pie[sum=auto, before number=\myfrac, after number=\relax]
{{1/2}/,{3/16}/,{1/4}/,{1/16}/}
\end{tikzpicture}
%
\def\piestyle{\displaystyle}
\begin{tikzpicture}
%%I want 1/6 instead of 1 inside the pieces:
\pie[sum=auto, before number=\myfrac, after number=\relax]
{{1/6}/,{1/6}/,{1/6}/,{1/6}/,{1/6}/,{1/6}/}
\end{tikzpicture}

\begin{tikzpicture}
%%I want 1/6 instead of 1 inside the pieces:
\pie[sum=auto]
{{1/6}/,{1/6}/,{1/6}/,{1/6}/,{1/6}/,{1/6}/}
\end{tikzpicture}
\end{document}

enter image description here

2
  • Thanks for the quick answer, it helped a lot. I tried to make the fraction looks more mathematically with "\frac{1}{2}", but it does not work with pgf-pie. Is there another way to label the pieces with \frac{}{}? Thank you for answering.
    – Kilian Hof
    Commented Oct 23, 2018 at 12:48
  • @KilianHof I figured it out, looking at the guts of the pgf-pie package Commented Oct 23, 2018 at 16:46
0

The wheelchart package, which I wrote, can be used.

In the first example below, the numerator is the first variable (\WCvarA) and the denominator is the second variable (\WCvarB). The value is set with the key value=\WCvarA/\WCvarB. The fraction is displayed with the key wheel data={$\frac{\WCvarA}{\WCvarB}$}.

In the second example below, the numerator is the first variable (\WCvarA) and the denominator is the sum of all these values, given by \WCtotalnum. The value is the initial setting value=\WCvarA. The fraction is displayed with the key wheel data.

enter image description here

\documentclass[border=6pt]{standalone}
\usepackage{wheelchart}
\begin{document}
\begin{tikzpicture}
\wheelchart[
  data=,
  pie,
  slices style=\WCvarC,
  value=\WCvarA/\WCvarB,
  wheel data={$\frac{\WCvarA}{\WCvarB}$}
]{%
  1/2/blue,
  3/16/cyan,
  1/4/yellow,
  1/16/orange%
}
\wheelchart[
  at={(7,0)},
  data=,
  pie,
  wheel data={$\frac{\WCvarA}{\WCtotalnum}$}
]{%
  1/blue,
  3/cyan,
  1/yellow,
  1/orange%
}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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