[Tex/LaTex] Use floating point with calc

calculationsfloating point

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.

Best Answer

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