Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
The command \testpie must be expanded before it passed as an argument. –  Marco Daniel Sep 7 '12 at 18:30
    
Welcome to TeX.sx! –  Marco Daniel Sep 7 '12 at 19:00

1 Answer 1

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

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.