2

I am having same color for chrome and safari slices however for safari i have added yellow to cyclelist

\begin{figure}[H]
\begin{center}
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"brown","blue","red","green","yellow"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      46.6/Chrome,
      24.6/Internet Explorer,
      20.4/Firefox,
      4.0/Other,
      5.1/Safari
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum3<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}
\end{center}
\caption{***} 
\end{figure}
1
  • Welcome to TeX.SX! Presumably your question is why the colours for chrome and safari are the same??? Can you please post a full minimal working example. It should start with a \documentclass command, have a minimal preamble and then \begin{document}...\end{document}. The code should compile and be as small as possible to demonstrate your problem. This makes it much easier for people to help you --- and much more likely that they will!
    – user30471
    Commented May 20, 2016 at 11:43

1 Answer 1

2

The line that says

\ifnum3<\cyclecount 

is counting only 4 elements, because since array indexing starts from 0, we have 0,1,2,3. But you have 5 elements in your chart, so it should reach 4. Therefore writing

\ifnum4<\cyclecount 

should fix the problem.

However, this forces you to edit this number every time you change the number of elements in the \foreach statement. So you can either write a number higher than the max index of your list, or — in my opinion — a better solution would be to add a count to the foreach and use that as a variable.

So in the end you should have:

\foreach \percent/\name [count=\countx] in {%
    ...
\ifnum\countx<\cyclecount

which will automatically adapt to any number of elements, since the last "run" of the loop will use the index of the last element.

2
  • By the way, this is unrelated to the answer, I suggest you use \def with caution, because if you happen to define a variable/command with an existing name, \def will overwrite it. Prefer using \newcommand instead. If I decide to use it, I choose the name appropriately (to lower the risk of redefining existing commands). See also: What is the difference between \def and \newcommand?
    – Alenanno
    Commented May 20, 2016 at 11:59
  • Also, I just noticed that your percentages are wrong, in total they give 100.7.
    – Alenanno
    Commented May 20, 2016 at 14:43

You must log in to answer this question.

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