2

When compiling following code, I the compiler tells me that a command is already defined (the name is \c@b...).

\documentclass{article}

\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}

\usepackage{verbatim}

\newcommand{\piepart}[5]
{
    \draw[thick,fill=#4] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

    \pgfmathparse{0.5*#1+0.5*#2}
    \let\midangle\pgfmathresult
    \pgfmathtruncatemacro{\trunc}{\midangle}
    \ifthenelse{\trunc < 90}{
    \draw (\midangle:1) node[above right]{#3};
    }
    {
        \ifthenelse{\trunc < 180}{
        \draw (\midangle:1) node[above left]{#3};
        }
        {
            \ifthenelse{\trunc < 270}{
            \draw (\midangle:1) node[below left]{#3};
            }
            {
                \draw (\midangle:1) node[below right]{#3};
            }
        }
    }
    \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
    \let\temp\pgfmathresult
    \pgfmathparse{max(\temp,-0.5) + 0.8}
    \let\innerpos\pgfmathresult
    \node at (\midangle:0.8) {#5};

}

\newcommand{\piechart}[1]
{
\newcounter{a}
\newcounter{b}
\foreach \p/\mcolor/\mname in #1
    {
        \setcounter{a}{\value{b}}
        \addtocounter{b}{\p}
        \piepart{\thea/100*360}
                    {\theb/100*360}
                    {\mname}{\mcolor}{\p\%}
    }
}

\begin{document}

\begin{tikzpicture}[scale=1.5]
\tikzstyle{every node}=[font=\tiny]
\piechart{{75/gray/A, 18/green/B, 7/red/C}}                 
\end{tikzpicture}
\begin{tikzpicture}[scale=1.5]
\tikzstyle{every node}=[font=\tiny]
\piechart{{75/gray/A, 18/green/B, 7/red/C}}                 
\end{tikzpicture}

\end{document}

If somebody knows what creates the error, or how to solve it, could they please tell me.

Thanks

1
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Thank you for providing the example! Can you put into your post exactly what the error message is? Thanks :-)
    – darthbith
    Apr 16, 2015 at 12:17

1 Answer 1

9

There are two \newcounter calls inside the macro \pichart. When the macro is called the second time, then the counters are already defined and you get the error messages about already defined commands \c@a and \c@b, which are the internal command representation of the counters.

Move the \newcounter calls outside and reset the counters inside the macro:

\newcounter{a}
\newcounter{b}
\newcommand*{\piechart}[1]{%
  \setcounter{a}{0}%
  \setcounter{b}{0}%
  ...
}

Full example for completeness (and with larger fonts):

\documentclass{article}

\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}

\usepackage{verbatim}

\newcommand{\piepart}[5]
{
    \draw[thick,fill=#4] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

    \pgfmathparse{0.5*#1+0.5*#2}
    \let\midangle\pgfmathresult
    \pgfmathtruncatemacro{\trunc}{\midangle}
    \ifthenelse{\trunc < 90}{
    \draw (\midangle:1) node[above right]{#3};
    }
    {
        \ifthenelse{\trunc < 180}{
        \draw (\midangle:1) node[above left]{#3};
        }
        {
            \ifthenelse{\trunc < 270}{
            \draw (\midangle:1) node[below left]{#3};
            }
            {
                \draw (\midangle:1) node[below right]{#3};
            }
        }
    }
    \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
    \let\temp\pgfmathresult
    \pgfmathparse{max(\temp,-0.5) + 0.8}
    \let\innerpos\pgfmathresult
    \node at (\midangle:0.8) {#5};

}

\newcounter{a}
\newcounter{b}
\newcommand{\piechart}[1]
{
    \setcounter{a}{0}%
    \setcounter{b}{0}%
    \foreach \p/\mcolor/\mname in #1
    {
        \setcounter{a}{\value{b}}
        \addtocounter{b}{\p}
        \piepart{\thea/100*360}
                    {\theb/100*360}
                    {\mname}{\mcolor}{\p\%}
    }
}

\begin{document}

\begin{tikzpicture}[scale=1.5]
    \tikzstyle{every node}=[font=\footnotesize]
    \piechart{{75/gray/A, 18/green/B, 7/red/C}}                 
\end{tikzpicture}
\begin{tikzpicture}[scale=1.5]
    \tikzstyle{every node}=[font=\footnotesize]
    \piechart{{75/gray/A, 18/green/B, 7/red/C}}                 
\end{tikzpicture}

\end{document}

Result

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .