# Pie charts with LaTeX TikZ

Define a new command to insert a pie slice:

\newcommand{\slice}[4]{
\pgfmathparse{0.5*#1+0.5*#2}
\let\midangle\pgfmathresult

% slice
\draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

% outer label
\node[label=\midangle:#4] at (\midangle:1) {};

% inner label
\pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
\let\temp\pgfmathresult
\pgfmathparse{max(\temp,-0.5) + 0.8}
\let\innerpos\pgfmathresult
\node at (\midangle:\innerpos) {#3};
}


Then define the slices in the order you want to have them and with the percentages and labels. You can start at a different point in the circle by setting the counter ‘d’ to a different value before the loop, e.g. \setcounter{d}{25}.

\begin{tikzpicture}[scale=3]
\newcounter{c}
\newcounter{d}
\foreach \p/\t in {66/, 17/Equative, 10/Difference, 7/}
{
\setcounter{c}{\value{d}}
\slice{\thec/100*360}
{\thed/100*360}
{ \small \p\%}{\t}
}
\node[label=0.5:Ranked] at (1,0.6) {};
\node[label=0.5:Superlative] at (1,-0.3) {};
\end{tikzpicture}


I didn’t like the automatic placement of two labels, that is why I gave ‘Ranked’ and ‘Superlative’ an empty label in the loop and placed them by hand later on.

The original is from Texample, uploaded by Robert Vollmert.

This entry was posted in LaTeX and tagged , , , by swk. Bookmark the permalink.