我对 pgfmath 数组有疑问:
- 如果在 pgfkeys 之外定义,它会起作用,
- 里面不起作用。
问题是什么 ?
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{tag/.style={align=center}}
\makeatletter
\pgfkeys{/DiagCirc/.cd,
value list/.store in=\Value@list,
tags/.store in=\T@gs,
tags=,
percent corr/.store in=\C@rrP,
percent corr=,
diagram/.code={%
% Calcul de la somme
\pgfmathsetmacro\S@m{0}
\foreach \i/\y in \Value@list {\xdef\S@m{\S@m+\i}}
\pgfmathsetmacro\S@m{\S@m}
\ifx\hfuzz\T@gs\hfuzz
\else
\foreach \V/\N [count=\j from 0] in \Value@list {%
% works
\pgfmathtruncatemacro\P{round(\V/\S@m*100)+\CorrP[\j]}
% doesn't work
%\pgfmathtruncatemacro\P{round(\V/\S@m*100)+\C@rrP[\j]}
\node[tag] at (\j,0) {\T@gs} ;
}
\fi%
} % fin du diagram code
}
\makeatother
\begin{document}
\begin{tikzpicture}
\def\CorrP{{-1,0,0,0,0}}
\path[/DiagCirc/.cd,
value list={1236/R+L+D,149/A,740/N+F,346/C,75/E},
percent corr={{-1,0,0,0,0}},
tags=\P\,\%,
diagram] ;
\end{tikzpicture}
\end{document}
答案1
这是因为键中的数组的括号被剥离了。相反,您可以在循环之前将其设为双括号数组,这样它仍然会被理解为有效的 PGF 数组。
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{tag/.style={align=center}}
\makeatletter
\pgfkeys{/DiagCirc/.cd,
value list/.store in=\Value@list,
tags/.store in=\T@gs,
tags=,
percent corr/.store in=\C@rrP,
percent corr=,
diagram/.code={%
% Calcul de la somme
\pgfmathsetmacro\S@m{0}%
\foreach \i/\y in \Value@list {\xdef\S@m{\S@m+\i}}%
\pgfmathsetmacro\S@m{\S@m}%
\ifx\hfuzz\T@gs\hfuzz%
\else%
\expandafter\def\expandafter\C@rrP\expandafter{\expandafter{\C@rrP}}%
\foreach \V/\N [count=\j from 0] in \Value@list {%
\pgfmathtruncatemacro\P{round(\V/\S@m*100)+\C@rrP[\j]}%
\node[tag] at (\j,0) {\T@gs};%
}%
\fi%
} % fin du diagram code
}
\makeatother
\begin{document}
\begin{tikzpicture}
\path[/DiagCirc/.cd,
value list={1236/R+L+D,149/A,740/N+F,346/C,75/E},
percent corr={-1,0,0,0,0},
tags=\P\,\%,
diagram];
\end{tikzpicture}
\end{document}