3

I am currently using pgf-pie package and I face a problem when I write the pgf-pie content using \newcommand instruction. The following MWE

\documentclass{standalone}
\usepackage{pgf-pie}
\usetikzlibrary{shadows}
\begin{document}

\newcommand{\testpie}{10/ A , 20/ B , 30/ C , 40/ D}

\begin{tikzpicture}

\begin{pie}[
  text = inside,
  scale font,
  style = drop shadow
]
{
  \testpie
}
\end{pie}
\end{tikzpicture}

\end{document}

generates the following error

! Package PGF Math Error: Unknown function `A' (in '10/ A , 20/ B , 30/ C , 40/D').

The final goal is to automatically generate the "fraction" sequence by reading a pgfplotstable.

Edit:

Going back to initial code I have something like that

\documentclass{standalone}

\usepackage{pgfplotstable}
\usepackage{pgf-pie}
\usepackage{xstring}

\pgfplotstableread[col sep=comma]{
  Year,TB,B,AB,P,NA
  2008/2009,15,38,15,0,31
  2009/2010,13,25,38,0,25
  2010/2011,17,50,8,17,8
}{\mention}

%% Feel free to improve the next lines
\newcommand{\pieplot}[1]{%
  \pgfplotstableforeachcolumnelement{Year}\of{#1}\as\cell{%
    \IfStrEq{\cell}{2008/2009}{%
      \pgfplotsforeachungrouped \x in {TB,B,AB,P,NA}{%
        \pgfplotstablegetelem{\pgfplotstablerow}{\x}\of{#1}
        \pgfmathtruncatemacro\val{\pgfplotsretval}
        \ifnum\val=0
        \else
        \val / \x
        \IfStrEq{\x}{NA}{}{,}
        \fi
      }
    }
  }
}

\begin{document}
  \pieplot{\mention}
\end{document}

which generates the right "fraction" sequence but fails when I use \pie command even with \mypie command.

3

Based on the rudimentary documentation and related to the code of pgf-pie the mandatory argument will be passed directly to a foreach construction. So you have to fully expand the argument first. Here a suggestion by providing a new command \mypie:

\newcommand\mypie[2][]{%
 \begingroup
  \edef\x{\endgroup\noexpand\pie[#1]{#2}}
 \x}

Here the complete code:

\documentclass{standalone}
\usepackage{pgf-pie}

\begin{document}

\newcommand{\testpie}{10/ A , 20/ B , 30/ C , 40/ D}

\newcommand\mypie[2][]{%
 \begingroup
  \edef\x{\endgroup\noexpand\pie[#1]{#2}}
 \x}


\begin{tikzpicture}
\mypie{\testpie}
\end{tikzpicture}

\end{document}

enter image description here

  • Your code is working fine. My problem now is that the \testpie is more complicated and makes use of \pgfplotstable commands. I will try to adapt your answer to my problem. – Xavier Garrido Sep 7 '12 at 19:12
  • I can not find a way to deal with pgfplotstable command. For example, if I add into the \testpie command, \pgfmathtruncatemacro\dummy{0} then the control sequence is undefined – Xavier Garrido Sep 7 '12 at 20:08
  • @XavierGarrido: Please edit your original question and show your issue – Marco Daniel Sep 7 '12 at 20:13

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.