我无法将饼图添加到 cvitem

我无法将饼图添加到 cvitem

我有如下的饼图:

\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}

但当我将它们复制到时\cvitem{}{$here},我收到很多错误。

文件:'file:///c%3A/Users/erow/Documents/tex/cv1.tex' 严重性:'错误' 消息:'段落在 \cvitem 完成 \par l.74 之前结束
' 位于:'74,1' 来源:'LaTeX' 代码:'undefined'

文件:'file:///c%3A/Users/erow/Documents/tex/cv1.tex' 严重性:'错误' 消息:'未定义的控制序列 \pgffor@body ...else \global \advance \cyclecount by 1 \global \advance \ind... l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: 不能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 use在 \advance 后使用字母 b'。 \pgffor@body ...count by 1 \global \advance \ind by 1 \ifnum 3<\cyclecount ... l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: 未定义的控制序列。 \pgffor@body ...ce \ind by 1 \ifnum 3<\cyclecount \global \cyclecount =0 \gl... l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: 缺少数字,视为零。\global l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: 未定义的控制序列。\cyclelist [\the \ind ] l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: 未定义的控制序列。\cyclelist [\the \ind ] l.98 } ; c:/Users/erow/Documents/tex/cv1.tex:98: 不能在 \the 后使用“字符 ]”。 \cyclelist [\the \ind ]
' 位于: '98,1' 来源: 'LaTeX' 代码: 'undefined'

答案1

好的。我猜 MNWE 看起来像这样:

\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}

返回

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 

?

这里的问题是,你在参数中有一个段落中断,即\cvitem一个空白行。因此,最小的修复方法是

\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}

最小修复

但是,您真的不应该使用此代码。首先,它\color在大量使用颜色的环境中重新定义。重新定义只是局部的,但这仍然似乎令人怀疑。此外,它既充当局部变量,又充当接受参数的宏,这令人困惑。而且,定义不会抹去一些关键的东西这一点也\name并不明显。\global

此外,你不需要做任何这些危险的事情。以下做法应该更安全:

\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}

\cvitem并产生完全相同的结果(除了我在某处丢失了标签。

为了消除标签的挤压,您可能还喜欢类似以下内容的内容:

压扁的标签

\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}

相关内容