# pgf-pie : Dimension too large

I used the package pgf-pie to generate pie chart. The option sum=auto compute automatically the sum of all value. The following MWE give Dimension too large because this reason.

\documentclass{standalone}
\usepackage{pgf-pie}
\begin{document}
\begin{tikzpicture}
\pie[sum=auto]{16384/foo}
\end{tikzpicture}
\end{document}


I naively tried to use fpu in the following snippet of pgf-pie.sty :

% handle sum
\ifthenelse{\equal{\pgfpie@sum}{auto}}
{
% sum all input
\xdef\pgfpie@sum{0}
\foreach \p/\t in {#2}
{
\pgfmathparse{\pgfpie@sum + \p}
\xdef\pgfpie@sum{\pgfmathresult}
}
}
{}


but adding \RequirePackage{pgfplots} give me more errors.

How to adjust correctly pgf-pie.sty so that it can support larger value?

Unfortunately, it is not possible to enter large numbers directly. The fpu library can't be used for two reasons. First, the number format is incompatible with \ifnum (internally used by pgf-pie), because even with \pgfkeys{/pgf/fpu/output format=fixed} something like \pgfmathparse{int(...)} will give a result with a decimal point (e.g. 0.0). And second, pgf internally assigns given lengths to dimen registers, which will lead to dimension too large errors for big numbers.

But it is possible to scale the numbers, using the fpu library, a new key (scale numbers), a little macro (\pgfpie@scalenumber) and some patching. With this, you still have to enter small numbers (their sum must not exceed 16383), but you can give a scaling factor, so the pie chart contains big numbers. Additionally, you have to set the precision to control decimal places. I used \pgfmathprintnumber here to get better formatting for the numbers, which also makes it possible, to get scientific notation, if you want.

The code:

\documentclass[border=2mm]{standalone}
\usepackage{etoolbox}
\usepackage{pgf-pie}
\usetikzlibrary{fpu}
\makeatletter
% new key
\pgfkeys{/scale numbers/.store in=\pgfpie@scale,
/scale numbers=1}
% macro for scaling
\newcommand*{\pgfpie@scalenumber}[1]{%
\begingroup
\pgfkeys{/pgf/fpu}% enable fpu only locally
\pgfmathparse{#1*\pgfpie@scale}%
%\pgfmathroundtozerofill{\pgfmathresult}%
\pgfmathprintnumber{\pgfmathresult}%
\endgroup
}
% patching the internal commands
\patchcmd{\pgfpie@slice}{\beforenumber#3}{\beforenumber\pgfpie@scalenumber{#3}}{}{}
\patchcmd{\pgfpie@slice}{\beforenumber#3}{\beforenumber\pgfpie@scalenumber{#3}}{}{}
\patchcmd{\pgfpie@square}{\beforenumber#3}{\beforenumber\pgfpie@scalenumber{#3}}{}{}
\patchcmd{\pgfpie@square}{\beforenumber#3}{\beforenumber\pgfpie@scalenumber{#3}}{}{}
\patchcmd{\pgfpie@cloud}{\beforenumber#3}{\beforenumber\pgfpie@scalenumber{#3}}{}{}
\patchcmd{\pgfpie@cloud}{\beforenumber#3}{\beforenumber\pgfpie@scalenumber{#3}}{}{}
\makeatother
\begin{document}
\begin{tikzpicture}
% set up number formatting
\pgfkeys{/pgf/number format/.cd,
fixed,fixed zerofill,precision=0,
set thousands separator={\,}}

\pie[sum=auto,scale numbers=1000]{1234.567/foo,1234.567/bar,1234.567/baz}
\end{tikzpicture}
\end{document}


The result:

• Thanks for your solution! What is the limit of scale numbers? For instance, \pie[sum=auto,scale numbers=100000000000]{0.92384797234000005/foo} give me 92384796000 while 92384797234 is expected. – Firmin Martin May 26 '18 at 12:36
• The fpu library can handle numbers up to 10^324, but it can only handle about 7 - 8 digits and the last one may show rounding errors. Thus, the value for scale numbers can be much higher, but you would only get more 0s at the end (e.g. a value of 100000000000000 would result in 92384796000000). Personally, with numbers that big, I would use scientific notation, because IMO it's easier to read. – Mike May 27 '18 at 2:33