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

6

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
    Oct 23 '18 at 12:48
  • @KilianHof I figured it out, looking at the guts of the pgf-pie package Oct 23 '18 at 16:46

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.