7

I wanted to use a pie chart in my document and tried this code example: http://www.texample.net/tikz/examples/pie-chart/

Now this seems to work perfectly with integer values but as for floating point I get errors.

Using this code

\begin{tikzpicture}[scale=3]

\newcounter{a}
\newcounter{b}
\foreach \p/\t in {1.5/type Windows, 10/type Linux}
  {
    \setcounter{a}{\value{b}}
    \addtocounter{b}{\p}
    \slice{\thea/100*360}
          {\theb/100*360}
          {\p\%}{\t}
}

\end{tikzpicture}

I get this error:

Package calc Error: `.' invalid at this point. See the calc package documentation for explanation. Type H for immediate help. ... l.17 }

So I took a look into the documentation but could not find anything about this. I have also found this question here but using \real{1.5} and also just {1.5} did not help.

  • 2
    Well, \p contains 1.5 (in the first stage of the loop) and you can't add a floating point to a counter register – user31729 Dec 28 '15 at 15:58
  • You can use or adapt one of the answers to Pie chart with values as angles not percent – Ignasi Dec 28 '15 at 17:37
  • In other words, use \newlength{a} and \addtolength{\a}{\p}. No idea where \slice came from. – John Kormylo Dec 28 '15 at 17:49
  • It would be easier to help if you added a correct minimal working example (MWE) – Rmano Dec 28 '15 at 17:53
5

Counters only accept integer values.

You can use PGF tools or expl3:

\documentclass{article}

\usepackage{tikz}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\declarefpvar}{m}
 {
  \fp_new:c { g_pie_#1_fp }
 }
\NewDocumentCommand{\setfpvar}{mm}
 {
  \fp_gset:cn { g_pie_#1_fp } { #2 }
 }
\DeclareExpandableDocumentCommand{\usefpvar}{m}
 {
  \fp_use:c { g_pie_#1_fp }
 }
\ExplSyntaxOff

\newcommand{\slice}[4]{%
  \pgfmathsetmacro\midangle{0.5*#1+0.5*#2}
  % 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
  \pgfmathsetmacro\temp{min((#2-#1-10)/110*(-0.3),0)}
  \pgfmathsetmacro\innerpos{max(\temp,-0.5) + 0.8}
  \node at (\midangle:\innerpos) {#3};
}

\declarefpvar{a}
\declarefpvar{b}

\begin{document}
\begin{tikzpicture}[scale=3]

\foreach \p/\t in {1.5/type Windows, 10/type Linux}
  {
   \setfpvar{a}{\usefpvar{b}}
   \setfpvar{b}{\p+\usefpvar{b}}
    \slice{\usefpvar{a}/100*360}
          {\usefpvar{b}/100*360}
          {\tiny\p\%}{\t}
  }

\end{tikzpicture}

\end{document}

enter image description here

  • Thank you! But could you explain the first 2 lines in the foreach loop? – Someguy Jan 3 '16 at 18:13
  • 1
    @Someguy Consider \setfpvar and \usefpvar as the analogs of \setcounter and \value. – egreg Jan 3 '16 at 18:52
  • Yes I got that far too, but I am having trouble understanding why they obtain the values of p and t, and just what the order of these commands does exactly. I want to understand it so I can extend this method for more variables/slices. – Someguy Jan 3 '16 at 19:42
  • 1
    @Someguy The instruction \declarefpvar is the analog of \newcounter; in \setfpvar you can use operations with the standard notations, as in the case of \setfpvar{b}{\p+\usefpvar{b}} that adds the value stored in \p to the value of the b variable and stores the sum in the variable a. – egreg Jan 3 '16 at 20:17

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.