3

This is my MWE (not that Minimum, I know):

\documentclass{article}
\usepackage{tikz}

\definecolorseries{colorser}{rgb}{last}{blue}{green}

\def\piechart#1#2#3#4#5{%1:data,2:numberOfElements,3:colors,4:outerRadius,5:labels
    \resetcolorseries[#2]{#3}
    \edef\startangle{90}
    \foreach [
        remember=\endangle as \startangle,
        evaluate=\i as \endangle using {\startangle-(\csname#1\endcsname[\i-1]/100*360)},
        evaluate=\halfangle using {(\endangle-\startangle)/2+\startangle},
    ] \i in {1,...,#2} {%
        \fill[{#3!![\i]}] (0,0) --++(\startangle:#4) arc (\startangle:\endangle:#4);
        \draw[white, line width=0.75mm](0,0)--++(\startangle:#4+0.1pt);
        % \node at (\halfangle:#4) {\pgfmathparse{#5[\i]}\pgfmathresult};% Error: "Package PGF Math Error: Unknown function.."
        \node at (\halfangle:#4) {\csname#5\endcsname[\i]};
    }
    \draw[white,line width=0.75mm](0,0)--++(\startangle:#4);
    \fill[white](0,0)circle [radius=#4*0.4];%
}

\begin{document}

\begin{tikzpicture}
    \newcommand\shares{{50,30,15,5}}
    \newcommand\labels{{"a","b","c","d"}}
    \piechart{shares}{4}{colorser}{2cm}{labels}
\end{tikzpicture}

\end{document}

I'm still trying to bring labels to my pie chart. This is what it's looking like at the moment:

pie chart with wrong labels

As you probably guess, I want the labels to be:

  • a at the blue part
  • ...
  • d at the green part

I also tried the line I commented out in the MWE, but that results in the error:

Package PGF Math Error: Unknown function `labels' (in 'labels[1]').

I first thought the string list would be the problem, but since my linked question didn't help me, I'm again clueless. Thanks for the help again!!

caligula
  • 563

1 Answers1

3

You need to evaluate the expression but arrays are numbered with start index 0.

\node at (\halfangle:#4) {\pgfmathparse{\csname#5\endcsname[\i-1]}\pgfmathresult};

Also avoid using macro names starting with \begin and \end.

enter image description here

percusse
  • 157,807
  • Thanks again! Turns out, I still can't bring it into my main project... I'm about to bang my head on my computer till one of both cracks :( – caligula Jun 07 '15 at 21:30
  • @caligula I did it before. The result is uniformly random if you are an IKEA customer. Maybe you can ask a question about your project use of this macro. – percusse Jun 07 '15 at 21:33
  • Finally I got it done: Problem was that I had "\usepackage[ngerman]{babel}" and the double quotation marks weren't recognized correctly... – caligula Jun 08 '15 at 06:04