我正在尝试创建像这样的饼图(关联),但是当我使用代码时,出现以下错误:
\pgfmathparse{(-\midangle<180?"west":"east")
} \edef\textanchor{\pgfmathresult}
\pgfmathsetmacro\labelshiftdir{1-2*(-\midangle>180)}
错误说:
不完整 \iffalse;第 269 行之后的所有文本均被忽略”
我发现错误是由于里面的条件,但我不知道如何以其他方式实现。我使用的代码如下:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage[spanish]{babel}
\usetikzlibrary{positioning,shadows,arrows, backgrounds}
\begin{document}
\def\innerradius{2cm}
\def\outerradius{4cm}
\newcommand{\wheelchart}[1]{
\pgfmathsetmacro{\totalnum}{0}
\foreach \value/\colour/\name in {#1} {
\pgfmathparse{\value+\totalnum}
\global\let\totalnum=\pgfmathresult
}
\begin{tikzpicture}
\pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
\pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}
%
\begin{scope}[rotate=90]
%
\pgfmathsetmacro{\cumnum}{0}
\foreach \value/\colour/\name in {#1} {
\pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}
\pgfmathsetmacro{\percentage}{\value/\totalnum*100}
\pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}
\pgfmathparse{(-\midangle<180?"west":"east")} \edef\textanchor{\pgfmathresult}
\pgfmathsetmacro\labelshiftdir{1-2*(-\midangle>180)}
\fill[\colour] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
(-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;
\draw [*-,thin] node [append after command={(\midangle:\midradius pt+5ex) -- (\midangle:\outerradius + 1ex) -- (\tikzlastnode)}] at (\midangle:\outerradius + 1ex) [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\textbf{\large\name: \pgfmathprintnumber{\percentage}\%}};
\draw node [text=white, font=\sffamily,align=center] at (\midangle:{\innerradius+\wheelwidth/2-1ex}) {\Large{\pgfmathprintnumber{\percentage}\%}};
\global\let\cumnum=\newcumnum
}
\end{scope}
\end{tikzpicture}
}
\wheelchart{35.4/orange/No votaron, 64.6/yellow/Votaron}
\end{document}
答案1
没有最小工作示例,很难知道你的问题出在哪里。但是,下面的代码做了一些接近你想要的事情:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\foreach \midangle in {0,10,20,...,360} {
\ifnum\midangle<180
\def\textanchor{west}\def\labelshiftdir{1}
\else
\def\textanchor{east}\def\labelshiftdir{-1}
\fi
angle=\midangle, anchor=\textanchor, shift=\labelshiftdir.
}
\end{document}
输出包含以下行:
顺便说一句,如果你真的想用\pgrmathparse
这个,那么
\pgfmathparse{(-\midangle<180?"west":"east")} \edef\textanchor{\pgfmathresult}
你应该使用
\pgfmathsetmacro\textanchor{(-\midangle<180?"west":"east")}
编辑
查看您扩展的代码和下面的注释,问题实际上似乎出在您在\foreach
循环中使用的变量名上:
\foreach \value/\colour/\name in {#1} { ... }
请注意,这\value
是“标准 LaTeX 命令”,我认为\name
也是。(颜色可能是安全的,因为美国腐败了英语语言。最好避免在这样的循环中使用现有的 LaTeX 变量/宏名。
我不确定这些变量名中的哪一个导致了您的问题,或者究竟是什么导致了使用 babel 时的冲突,但如果我更改这些变量名,则以下代码会编译:
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows, backgrounds}
\usepackage[spanish]{babel}
\def\innerradius{2cm}
\def\outerradius{4cm}
\newcommand{\wheelchart}[1]{
\def\totalnum{0}
\foreach \val/\col/\nam in {#1} {
\typeout{val=\val, col=\col, nam=\nam.}
\pgfmathparse{\val+\totalnum}
\typeout{\totalnum -> \pgfmathresult.}
\xdef\totalnum{\pgfmathresult}
}
\begin{tikzpicture}
\pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
\pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}
%
\begin{scope}[rotate=90]
%
\pgfmathsetmacro{\cumnum}{0}
\foreach \val/\col/\nam in {#1} {
\pgfmathsetmacro{\newcumnum}{\cumnum + \val/\totalnum*360}
\pgfmathsetmacro{\percentage}{\val/\totalnum*100}
\pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}
\pgfmathparse{(-\midangle<180?"west":"east")}
\edef\textanchor{\pgfmathresult}
\pgfmathsetmacro\labelshiftdir{1-2*(-\midangle>180)}
\fill[\col] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
(-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;
\draw [*-,thin] node [append after command={(\midangle:\midradius pt+5ex) -- (\midangle:\outerradius + 1ex) -- (\tikzlastnode)}] at (\midangle:\outerradius + 1ex) [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\textbf{\large\nam: \pgfmathprintnumber{\percentage}\%}};
\draw node [text=white, font=\sffamily,align=center] at (\midangle:{\innerradius+\wheelwidth/2-1ex}) {\Large{\pgfmathprintnumber{\percentage}\%}};
\global\let\cumnum=\newcumnum
}
\end{scope}
\end{tikzpicture}
}
\begin{document}
\wheelchart{35.4/orange/No votaron, 64.6/yellow/Votaron}
\end{document}