4

I have a pie chart as below:

\documentclass[14pt,a4paper,san]{moderncv}   % possible options include font size ('10pt', '11pt' and '12pt'), paper size ('a4paper', 'letterpaper', 'a5paper', 'legalpaper', 'executivepaper' and 'landscape') and font family ('sans' and 'roman')

\moderncvstyle{classic}                        % 选项参数是 ‘casual’, ‘classic’, ‘oldstyle’ 和 ’banking’
\moderncvcolor{blue}                          % 选项参数是 ‘blue’ (默认)、‘orange’、‘green’、‘red’、‘purple’ 和 ‘grey’

\name{a}{a}
\title{s}
\address{}{s}
\email{}
\phone[mobile]{1212}
\begin{document}

\cvitem{}{
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      8/C++,8/Linux,7/Arduino,6/Python,6/Erlang,5/H5
    } {
      \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*1.8*2.5:\radius) -- cycle;

        \node at (\angle+0.5*\percent*1.8*2.5:0.7*\radius) {\name};
        %\node[pin=\angle+0.5*\percent*3.6:\name]
        %  at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*1.8*2.5}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}}

\clearpage
\end{document}

But when I copy them into \cvitem{}{$here}. I got lots of error.

file: 'file:///c%3A/Users/erow/Documents/tex/cv1.tex' severity: '错误' message: 'Paragraph ended before \cvitem was complete \par l.74
' at: '74,1' source: 'LaTeX' code: 'undefined'

file: 'file:///c%3A/Users/erow/Documents/tex/cv1.tex' severity: '错误' message: 'Undefined control sequence \pgffor@body ...else \global \advance \cyclecount by 1 \global \advance \ind... l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: You can't use the letter b' after \advance. \pgffor@body ...lse \global \advance \cyclecount b y 1 \global \advance \ind ... l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: Undefined control sequence. \pgffor@body ...ecount by 1 \global \advance \ind by 1 \ifnum 3<\cyclecount ... l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: You can't usethe letter b' after \advance. \pgffor@body ...count by 1 \global \advance \ind b y 1 \ifnum 3<\cyclecount ... l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: Undefined control sequence. \pgffor@body ...ce \ind by 1 \ifnum 3<\cyclecount \global \cyclecount =0 \gl... l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: Missing number, treated as zero. \global l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: Undefined control sequence. \cyclelist [\the \ind ] l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: Undefined control sequence. \cyclelist [\the \ind ] l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: You can't use `the character ]' after \the. \cyclelist [\the \ind ]
' at: '98,1' source: 'LaTeX' code: 'undefined'

  • The example is all right. And the only thing I want is moving it to cvitem. Or some other way to make it in center. It looks ugly.囧 – erow Mar 7 '18 at 3:10
  • It need moderncv package. You can install it in pakcage manager. – erow Mar 7 '18 at 3:24
  • Problem is a a blank line.o(╯□╰)o – erow Mar 7 '18 at 3:36
  • Thanks for updating :-). This is now really clear and useful: very easy for people to reproduce the problem. (You are still welcome, by the way - but I've managed to lose the greeting in deleting my no-longer-needed comments asking for a useful example.) – cfr Mar 7 '18 at 4:07
1

OK. I guess that an MNWE looks like this:

\documentclass[14pt,a4paper,san]{moderncv}
\moderncvstyle{classic}
\moderncvcolor{blue}
\name{a}{a}
\title{s}
\address{}{s}
\email{}
\phone[mobile]{1212}
\begin{document}

\cvitem{}{%
  \def\angle{0}
  \def\radius{3}
  \def\cyclelist{{"orange","blue","red","green"}}
  \newcount\cyclecount \cyclecount=-1
  \newcount\ind \ind=-1
  \begin{tikzpicture}[nodes = {font=\sffamily}]
    \foreach \percent/\name in {
      8/C++,8/Linux,7/Arduino,6/Python,6/Erlang,5/H5
    } {
      \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*1.8*2.5:\radius) -- cycle;

      \node at (\angle+0.5*\percent*1.8*2.5:0.7*\radius) {\name};
      \pgfmathparse{\angle+\percent*1.8*2.5}  % Advance angle
      \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
  \end{tikzpicture}%
}

\clearpage
\end{document}

which returns

Runaway argument?
{\def \angle {0} \def \radius {3} \def \cyclelist {{"orange","blue","\ETC.
! Paragraph ended before \\cvitem was complete.
<to be read again> 
                   \par 
l.45 

?

The problem here is that you have a paragraph break in the argument to \cvitem i.e. a blank line. So, a minimal fix would be

\documentclass[14pt,a4paper,san]{moderncv}
\moderncvstyle{classic}
\moderncvcolor{blue}
\name{a}{a}
\title{s}
\address{}{s}
\email{}
\phone[mobile]{1212}
\begin{document}
\cvitem{pie chart}{%
  \def\angle{0}
  \def\radius{3}
  \def\cyclelist{{"orange","blue","red","green"}}
  \newcount\cyclecount \cyclecount=-1
  \newcount\ind \ind=-1
  \begin{tikzpicture}[nodes = {font=\sffamily}]
    \foreach \percent/\name in {
      8/C++,8/Linux,7/Arduino,6/Python,6/Erlang,5/H5
    } {
      \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*1.8*2.5:\radius) -- cycle;
      \node at (\angle+0.5*\percent*1.8*2.5:0.7*\radius) {\name};
      \pgfmathparse{\angle+\percent*1.8*2.5}  % Advance angle
      \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
  \end{tikzpicture}%
}

\clearpage
\end{document}

minimal fix

However, you really shouldn't use this code. For one thing it redefines \color in an environment which uses colours a lot. The redefinition is only local, but that still seems dubious. Moreover \name is serving as both a local variable and a macro taking arguments, which is confusing. And it is not obvious that the \global definitions are not going to wipe out something crucial, either.

Moreover, you don't need to do any of these dangerous things. The following should be considerably safer:

\documentclass[14pt,a4paper,san]{moderncv}
\moderncvstyle{classic}
\moderncvcolor{blue}
\name{a}{a}
\title{s}
\address{}{s}
\email{}
\phone[mobile]{1212}
\begin{document}

\cvitem{}{%
  \begin{tikzpicture}[font=\sffamily]
    % these are local - much less likely to cause problems
    \def\radius{3}
    \def\cyclelist{{"orange","blue","red","green"}}
    \foreach \percent/\pname [evaluate=\percent as \pangle using ((4.5*\percent)+\panglelast)) , remember=\pangle as \panglelast (initially 0), count=\cyclecount from 0] in 
    {
      8/C++,8/Linux,7/Arduino,6/Python,6/Erlang,5/H5
    } {
      \pgfmathsetmacro\mycolor{\cyclelist[(int(mod(\cyclecount,4)))]}
      % Draw angle and set labels
      \draw [fill={\mycolor!50}, draw={\mycolor}] (0,0) -- (\panglelast:\radius) arc (\panglelast:\pangle:\radius) -- cycle;
      \node at ({(\pangle+\panglelast)/2}:0.7*\radius) {\pname};
    };
  \end{tikzpicture}%
}

\end{document}

and produces exactly the same result (except that I've lost the label of the \cvitem somewhere along the line.

To de-squish the labels, you might also prefer something which looks more like the following:

de-squished labels

\documentclass[14pt,a4paper,san]{moderncv}
\moderncvstyle{classic}
\moderncvcolor{blue}
\name{a}{a}
\title{s}
\address{}{s}
\email{}
\phone[mobile]{1212}
\begin{document}

\cvitem{}{%
  \begin{tikzpicture}[font=\sffamily]
    % these are local - much less likely to cause problems
    \def\radius{3}
    \def\cyclelist{{"orange","blue","red","green"}}
    \foreach \percent/\pname [evaluate=\percent as \pangle using ((4.5*\percent)+\panglelast)) , remember=\pangle as \panglelast (initially 0), count=\cyclecount from 0] in 
    {
      8/C++,8/Linux,7/Arduino,6/Python,6/Erlang,5/H5
    } {
      \pgfmathsetmacro\mycolor{\cyclelist[(int(mod(\cyclecount,4)))]}
      % Draw angle and set labels
      \draw [fill={\mycolor!50}, draw={\mycolor}] (0,0) -- (\panglelast:\radius) arc (\panglelast:\pangle:\radius) -- cycle;
      \path (0,0) -- ({(\pangle+\panglelast)/2}:\radius) node [midway, sloped] {\pname};
    };
  \end{tikzpicture}%
}

\end{document}
  • God!Does a blank line have special meaning? – erow Mar 7 '18 at 3:34
  • @erow A blank line is a paragraph break. Are you new to LaTeX? This is the way you start a new paragraph in LaTeX (or in any TeX, for that matter). Please also see my edits above. That \edef\color and the \globals really aren't wise and can, I think, be easily avoided. (Or do you really need to include empty values for \percent?) – cfr Mar 7 '18 at 4:01
  • I started latex two days ago. And I don't know what isempty values for \percent – erow Mar 7 '18 at 4:11
  • @erow Your original code allows you to add something like /Perl to your list without error. But I don't know why you'd want this in a pie chart: either Perl needs a slice and should get a number or it doesn't and should be dropped. My code assumes you use value-pairs which makes sense and will give an error if you don't. I don't know why you'd need empty values, so, if you don't either, you probably don't need them. But, after all, you posted the code. I therefore mentioned it because i assume you had some reason for writing it that way. – cfr Mar 7 '18 at 4:16
  • You want to know the meaning of pair "8/C++" ? I use the percent to change size, and don't want to show it. – erow Mar 7 '18 at 4:25

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.