%%
%% This is file `datapie.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% datatool.dtx  (with options: `datapie.sty,package')
%% 
%%  datatool.dtx
%%  Copyright 2018 Nicola Talbot
%% 
%%  This work may be distributed and/or modified under the
%%  conditions of the LaTeX Project Public License, either version 1.3
%%  of this license or (at your option) any later version.
%%  The latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%%  and version 1.3 or later is part of all distributions of LaTeX
%%  version 2005/12/01 or later.
%% 
%%  This work has the LPPL maintenance status `maintained'.
%% 
%%  The Current Maintainer of this work is Nicola Talbot.
%% 
%%  This work consists of the files datatool.dtx and datatool.ins and the derived files datatool-base.sty, datatool-fp.sty, datatool-pgfmath.sty, datatool.sty, datagidx.sty, databib.sty, databar.sty, datapie.sty, dataplot.sty, person.sty, databib.bst.
%% 
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{datapie}[2018/04/16 v2.30 (NLCT)]
\RequirePackage{xkeyval}
\newif\ifDTLcolorpiechart
\DTLcolorpiecharttrue
\DeclareOption{color}{\DTLcolorpiecharttrue}
\DeclareOption{gray}{\DTLcolorpiechartfalse}
\define@boolkey{datapie}[DTL]{rotateinner}[true]{}
\define@boolkey{datapie}[DTL]{rotateouter}[true]{}
\DTLrotateinnerfalse
\DTLrotateouterfalse
\DeclareOption{rotateinner}{\DTLrotateinnertrue}
\DeclareOption{norotateinner}{\DTLrotateinnerfalse}
\DeclareOption{rotateouter}{\DTLrotateoutertrue}
\DeclareOption{norotateouter}{\DTLrotateouterfalse}
\ProcessOptions
\RequirePackage{datatool}
\RequirePackage{tikz}
\newlength\DTLradius
\DTLradius=2cm
\newcommand*{\DTLinnerratio}{0.5}
\newcommand*{\DTLouterratio}{1.25}
\newcommand*\DTLcutawayratio{0.2}
\newcommand*{\DTLstartangle}{0}
\newlength\dtl@inneroffset
\dtl@inneroffset=\DTLinnerratio\DTLradius
\newlength\dtl@outeroffset
\dtl@outeroffset=\DTLouterratio\DTLradius
\newlength\dtl@cutawayoffset
\dtl@cutawayoffset=\DTLcutawayratio\DTLradius
\newcommand*{\dtl@piecutaways}{}
\def\dtl@innerlabel{\DTLpievariable}%
\def\dtl@outerlabel{}%
\newcounter{DTLpieroundvar}
\setcounter{DTLpieroundvar}{1}
\newcommand*{\DTLdisplayinnerlabel}[1]{#1}
\newcommand*{\DTLdisplayouterlabel}[1]{#1}
\newcommand*{\DTLpiepercent}{%
\ifnum\dtlforeachlevel=0\relax
  \PackageError{datapie}{Can't use
  \string\DTLpiepercent\space outside
  \string\DTLpiechart}{}%
\else
  \csname dtl@piepercent@\romannumeral\@dtl@seg\endcsname
\fi}
\newcommand*{\DTLpieatbegintikz}{}
\newcommand*{\DTLpieatendtikz}{}
\newcommand*{\DTLsetpiesegmentcolor}[2]{%
\expandafter\def\csname dtlpie@segcol\romannumeral#1\endcsname{#2}%
}
\newcommand*{\DTLgetpiesegmentcolor}[1]{%
\csname dtlpie@segcol\romannumeral#1\endcsname}
\newcommand*{\DTLdopiesegmentcolor}[1]{%
\expandafter\color\expandafter
{\csname dtlpie@segcol\romannumeral#1\endcsname}}
\newcommand*{\DTLdocurrentpiesegmentcolor}{%
\ifnum\dtlforeachlevel=0\relax
  \PackageError{datapie}{Can't use
  \string\DTLdocurrentpiesegmentcolor\space outside
  \string\DTLpiechart}{}%
\else
  \expandafter\DTLdopiesegmentcolor\expandafter{%
  \csname c@DTLrow\romannumeral\dtlforeachlevel\endcsname}%
\fi}
\newcommand*{\DTLpieoutlinecolor}{black}
\newlength\DTLpieoutlinewidth
\DTLpieoutlinewidth=0pt
\ifDTLcolorpiechart
 \DTLsetpiesegmentcolor{1}{red}
 \DTLsetpiesegmentcolor{2}{green}
 \DTLsetpiesegmentcolor{3}{blue}
 \DTLsetpiesegmentcolor{4}{yellow}
 \DTLsetpiesegmentcolor{5}{magenta}
 \DTLsetpiesegmentcolor{6}{cyan}
 \DTLsetpiesegmentcolor{7}{orange}
 \DTLsetpiesegmentcolor{8}{white}
\else
 \DTLsetpiesegmentcolor{1}{black!15}
 \DTLsetpiesegmentcolor{2}{black!25}
 \DTLsetpiesegmentcolor{3}{black!35}
 \DTLsetpiesegmentcolor{4}{black!45}
 \DTLsetpiesegmentcolor{5}{black!55}
 \DTLsetpiesegmentcolor{6}{black!65}
 \DTLsetpiesegmentcolor{7}{black!75}
 \DTLsetpiesegmentcolor{8}{black!85}
\fi
\define@key{datapie}{start}{\def\DTLstartangle{#1}}
\define@key{datapie}{radius}{\DTLradius=#1\relax
\dtl@inneroffset=\DTLinnerratio\DTLradius
\dtl@outeroffset=\DTLouterratio\DTLradius
\dtl@cutawayoffset=\DTLcutawayratio\DTLradius}
\define@key{datapie}{innerratio}{%
\def\DTLinnerratio{#1}%
\dtl@inneroffset=\DTLinnerratio\DTLradius}
\define@key{datapie}{outerratio}{%
\def\DTLouterratio{#1}%
\dtl@outeroffset=\DTLouterratio\DTLradius}
\define@key{datapie}{cutawayratio}{%
\def\DTLcutawayratio{#1}%
\dtl@cutawayoffset=\DTLcutawayratio\DTLradius}
\define@key{datapie}{inneroffset}{%
\dtl@inneroffset=#1}
\define@key{datapie}{outeroffset}{%
\dtl@outeroffset=#1}
\define@key{datapie}{cutawayoffset}{%
\dtl@cutawayoffset=#1}
\define@key{datapie}{cutaway}{%
\renewcommand*{\dtl@piecutaways}{#1}}
\define@key{datapie}{variable}{%
\def\DTLpievariable{#1}}
\define@key{datapie}{innerlabel}{%
\def\dtl@innerlabel{#1}}
\define@key{datapie}{outerlabel}{%
\def\dtl@outerlabel{#1}}
\newcommand*{\DTLpiechart}[4][\boolean{true}]{%
\bgroup
  \let\DTLpievariable=\relax
  \setkeys{datapie}{#2}%
  \ifx\DTLpievariable\relax
    \PackageError{datapie}%
    {\string\DTLpiechart\space missing variable}{}%
  \else
    \def\dtl@total{0}%
    \@sDTLforeach[#1]{#3}{#4}{%
      \let\dtl@oldtotal=\dtl@total
      \expandafter\DTLconverttodecimal\expandafter
        {\DTLpievariable}{\dtl@variable}%
      \dtladd{\dtl@total}{\dtl@variable}{\dtl@total}%
    }%
    \expandafter\DTLconverttodecimal\expandafter
      {\DTLstartangle}{\@dtl@start}%
    \@sDTLforeach[#1]{#3}{#4}{%
      \expandafter\DTLconverttodecimal\expandafter
        {\DTLpievariable}{\dtl@variable}%
      \dtl@computeangles
        {\csname c@DTLrow\romannumeral\dtlforeachlevel\endcsname}%
        {\dtl@variable}%
      \expandafter\@dtl@seg\expandafter=
        \csname c@DTLrow\romannumeral\dtlforeachlevel\endcsname%
      \dtlmul{\dtl@tmp}{\dtl@variable}{100}%
      \let\dtl@old=\dtl@tmp
      \dtldiv{\dtl@tmp}{\dtl@old}{\dtl@total}%
      \expandafter\dtlround
        \csname dtl@piepercent@\romannumeral\@dtl@seg\endcsname\dtl@tmp
        \c@DTLpieroundvar
    }%
    \@for\dtl@row:=\dtl@piecutaways\do{%
      \expandafter\@dtl@set@off\dtl@row-\relax
    }%
    \let\dtl@start=\DTLstartangle
    \begin{tikzpicture}
      \DTLpieatbegintikz
      \@sDTLforeach[#1]{#3}{#4}%
      {%
        \expandafter\@dtl@seg\expandafter=
        \csname c@DTLrow\romannumeral\dtlforeachlevel\endcsname%
        \edef\dtl@start{%
          \csname dtl@sang@\romannumeral\@dtl@seg\endcsname}%
        \edef\dtl@extent{%
          \csname dtl@angle@\romannumeral\@dtl@seg\endcsname}%
        \dtladd{\dtl@endangle}{\dtl@start}{\dtl@extent}%
        \edef\dtl@angle{%
           \csname dtl@cut@angle@\romannumeral\@dtl@seg\endcsname}%
        \let\dtl@old=\dtl@angle
        \dtl@truncatedecimal\dtl@angle
        \ifnum\dtl@angle>180\relax
          \dtlsub{\dtl@angle}{\dtl@old}{360}%
          \dtl@truncatedecimal\dtl@angle
        \fi
        \edef\dtl@cutlen{%
          \csname dtl@cut@len@\romannumeral\@dtl@seg\endcsname
        }%
        \edef\@dtl@shift{(\dtl@angle:\dtl@cutlen)}%
        \dtlmul{\dtl@angle}{\dtl@extent}{0.5}%
        \dtladd{\dtl@midangle}{\dtl@angle}{\dtl@start}%
        \begin{scope}[shift={\@dtl@shift}]%
        \fill[color=\DTLgetpiesegmentcolor\@dtl@seg] (0,0) --
        (\dtl@start:\DTLradius)
        arc (\dtl@start:\dtl@endangle:\DTLradius) -- cycle;
        \ifdim\DTLpieoutlinewidth>0pt\relax
          \draw[color=\DTLpieoutlinecolor,%
                line width=\DTLpieoutlinewidth]
          (0,0) -- (\dtl@start:\DTLradius)
          arc (\dtl@start:\dtl@endangle:\DTLradius) -- cycle;
        \fi
        \dtl@truncatedecimal\dtl@midangle
        \ifDTLrotateinner
          \dtlifnumopenbetween{\dtl@midangle}{90}{270}%
          {%
            \let\@dtl@next\@firstoftwo
          }%
          {%
             \dtlifnumlt{\dtl@midangle}{-90}%
             {\let\@dtl@next\@firstoftwo}%
             {\let\@dtl@next\@secondoftwo}%
          }%
          \@dtl@next
          {%
            \dtlsub{\dtl@labelangle}{\dtl@midangle}{180}%
            \dtl@truncatedecimal\dtl@labelangle
            \edef\dtl@innernodeopt{anchor=east,rotate=\dtl@labelangle}%
          }%
          {%
            \edef\dtl@innernodeopt{anchor=west,rotate=\dtl@midangle}%
          }%
        \else
          \edef\dtl@innernodeopt{anchor=center}%
        \fi
        \ifDTLrotateouter
           \dtlifnumopenbetween{\dtl@midangle}{90}{270}%
           {%
             \let\@dtl@next\@firstoftwo
           }%
           {%
              \dtlifnumlt{\dtl@midangle}{-90}%
              {\let\@dtl@next\@firstoftwo}%
              {\let\@dtl@next\@secondoftwo}%
           }%
           \@dtl@next
           {%
             \dtlsub{\dtl@labelangle}{\dtl@midangle}{180}%
             \dtl@truncatedecimal\dtl@labelangle
             \edef\dtl@outernodeopt{anchor=east,rotate=\dtl@labelangle}%
           }%
           {%
             \edef\dtl@outernodeopt{anchor=west,rotate=\dtl@midangle}%
           }%
        \else
          \dtlifnumeq{\dtl@midangle}{45}
          {%
            \let\@dtl@next\@firstoftwo
          }%
          {%
            \dtlifnumgt{\dtl@midangle}{315}
            {%
              \let\@dtl@next\@firstoftwo
            }%
            {%
              \dtlifnumopenbetween{\dtl@midangle}{-45}{45}%
              {%
                \let\@dtl@next\@firstoftwo
              }%
              {%
                \let\@dtl@next\@secondoftwo
              }%
            }%
          }%
          \@dtl@next
          {%
            \edef\dtl@outernodeopt{anchor=west}%
          }%
          {%
            \dtlifnumopenbetween{\dtl@midangle}{45}{135}%
            {%
              \let\@dtl@next\@firstoftwo
            }%
            {%
              \dtlifnumeq{\dtl@midangle}{135}%
              {%
                \let\@dtl@next\@firstoftwo
              }%
              {%
                \let\@dtl@next\@secondoftwo
              }%
            }%
            \@dtl@next
            {%
              \edef\dtl@outernodeopt{anchor=south}%
            }%
            {%
              \dtlifnumopenbetween{\dtl@midangle}{135}{225}%
              {%
                \let\@dtl@next\@firstoftwo
              }%
              {%
                \dtlifnumeq{\dtl@midangle}{225}%
                {%
                  \let\@dtl@next\@firstoftwo
                }%
                {%
                  \dtlifnumeq{\dtl@midangle}{-135}%
                  {%
                    \let\@dtl@next\@firstoftwo
                  }%
                  {%
                    \dtlifnumlt{\dtl@midangle}{-135}%
                    {%
                      \let\@dtl@next\@firstoftwo
                    }%
                    {%
                      \let\@dtl@next\@secondoftwo
                    }%
                  }%
                }%
              }%
              \@dtl@next
              {%
                \edef\dtl@outernodeopt{anchor=east}%
              }%
              {%
                \edef\dtl@outernodeopt{anchor=north}%
              }%
            }%
          }%
        \fi
        \edef\@dtl@dolabel{%
          \noexpand\draw (\dtl@midangle:\the\dtl@inneroffset)
             node[\dtl@innernodeopt]{%
           \noexpand\DTLdisplayinnerlabel{\noexpand\dtl@innerlabel}};
        }%
        \@dtl@dolabel
        \edef\@dtl@dolabel{%
          \noexpand\draw (\dtl@midangle:\the\dtl@outeroffset)
            node[\dtl@outernodeopt]{%
          \noexpand\DTLdisplayouterlabel{\noexpand\dtl@outerlabel}};
        }%
        \@dtl@dolabel
        \end{scope}%
      }%
      \DTLpieatendtikz
    \end{tikzpicture}%
  \fi
\egroup
}
\newcommand*{\dtl@computeangles}[2]{%
  \dtlifnumgt{\@dtl@start}{180}%
  {%
    \let\dtl@old=\@dtl@start
    \dtlsub{\@dtl@start}{\dtl@old}{360}%
  }%
  {}%
  \dtlifnumlt{\@dtl@start}{-180}%
  {%
    \let\dtl@old=\@dtl@start
    \dtladd{\@dtl@start}{\dtl@old}{360}%
  }%
  {}%
  \expandafter\edef\csname dtl@sang@\romannumeral#1\endcsname{%
     \@dtl@start}%
  \dtlmul{\dtl@angle}{360}{#2}%
  \let\dtl@old=\dtl@angle
  \dtldiv{\dtl@angle}{\dtl@old}{\dtl@total}%
  \expandafter\let\csname dtl@angle@\romannumeral#1\endcsname=\dtl@angle
  \let\dtl@old=\@dtl@start
  \dtladd{\@dtl@start}{\dtl@old}{\dtl@angle}%
  \expandafter\def\csname dtl@cut@angle@\romannumeral#1\endcsname{0}%
  \expandafter\def\csname dtl@cut@len@\romannumeral#1\endcsname{0cm}%
}
\def\@dtl@set@off#1-#2\relax{%
  \ifstrempty{#2}%
  {%
    \@@dtl@set@off{#1}%
  }%
  {%
    \@@dtl@set@offr#1-#2\relax
  }%
}
\newcommand*{\@@dtl@set@off}[1]{%
  \edef\dtl@old{\csname dtl@angle@\romannumeral#1\endcsname}%
  \dtlmul{\dtl@angle}{\dtl@old}{0.5}%
  \let\dtl@old=\dtl@angle
  \edef\dtl@sang{\csname dtl@sang@\romannumeral#1\endcsname}%
  \dtladd{\dtl@angle}{\dtl@old}{\dtl@sang}%
  \expandafter\edef\csname dtl@cut@angle@\romannumeral#1\endcsname{%
  \dtl@angle}%
  \expandafter\edef\csname dtl@cut@len@\romannumeral#1\endcsname{%
   \the\dtl@cutawayoffset}%
}
\newcount\@dtl@seg
\def\@@dtl@set@offr#1-#2-\relax{%
 \ifnum#1>#2\relax
   \PackageError{datapie}{Segment ranges must go in ascending order}{%
   Try #2-#1 instead of #1-#2}%
 \else
   \def\dtl@angle{0}%
   \@dtl@seg=#1\relax
   \whiledo{\not\(\@dtl@seg > #2\)}{%
    \let\dtl@old=\dtl@angle
    \edef\dtl@segang{\csname dtl@angle@\romannumeral\@dtl@seg\endcsname}%
    \dtladd{\dtl@angle}{\dtl@old}{\dtl@segang}%
    \advance\@dtl@seg by 1\relax
   }%
   \let\dtl@old=\dtl@angle
   \dtlmul{\dtl@angle}{\dtl@old}{0.5}%
   \edef\dtl@sang{\csname dtl@sang@\romannumeral#1\endcsname}%
   \let\dtl@old=\dtl@angle
   \dtladd{\dtl@angle}{\dtl@old}{\dtl@sang}%
   \@dtl@seg=#1\relax
   \whiledo{\not\(\@dtl@seg > #2\)}{%
   \expandafter
    \let\csname dtl@cut@angle@\romannumeral\@dtl@seg\endcsname
    =\dtl@angle
   \expandafter
    \edef\csname dtl@cut@len@\romannumeral\@dtl@seg\endcsname{%
    \the\dtl@cutawayoffset}
    \advance\@dtl@seg by 1\relax
   }%
 \fi
}
\endinput
%%
%% End of file `datapie.sty'.
