改进 TeX Live 2021 的 Tikz 代码

改进 TeX Live 2021 的 Tikz 代码

我想使用代码。在 TeX Live 2019 中,该alpha0选项可以正常工作,但在 TeX Live 2021 中对输出没有影响。有人可以改进该代码吗?

梅威瑟:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{fadings}    
\definecolor{strangegreen}{RGB}{15,111,83}
\tikzset{pics/wedge/.style={code={%
  \tikzset{wedge/.cd,#1}
  \def\kvw##1{\pgfkeysvalueof{/tikz/wedge/##1}}
  \pgfmathtruncatemacro{\itest}{3*(1+sign(sin(\kvw{alpha})))+1+sign(sin(\kvw{beta}))}
  \ifcase\itest
    %0: alpha>180,beta>180
       \draw[fill=\kvw{color},very thin]
                       (\kvw{alpha}:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(\kvw{alpha}:\kvw{beta}:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(\kvw{beta}:\kvw{alpha}:\kvw{radius});
   \or
    %1: alpha>180,beta=0,180                      
       \draw[fill=\kvw{color},very thin]
                       (\kvw{alpha}:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(\kvw{alpha}:\kvw{beta}:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(\kvw{beta}:\kvw{alpha}:\kvw{radius});
   \or
    %2: alpha>180,beta<180                  
       \draw[fill=\kvw{color},very thin]
                       (\kvw{alpha}:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(\kvw{alpha}:360:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(360:\kvw{alpha}:\kvw{radius});
   \or
    %3: alpha=0,180,beta>180                      
       \draw[fill=\kvw{color},very thin]
                       (180:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(180:\kvw{beta}:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(\kvw{beta}:180:\kvw{radius});
   \or
    %4: alpha=0,180,beta=0,180                  
       \draw[fill=\kvw{color},very thin]
                       (180:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(180:0:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(0:180:\kvw{radius});
   \or
    %5: alpha=0,180,beta=<180                    
   \or
    %6: alpha<180,beta=>180                    
       \draw[fill=\kvw{color},very thin]
                       (180:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(180:\kvw{beta}:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(\kvw{beta}:180:\kvw{radius});
   \or
    %7: alpha<180,beta=0,180
    \pgfmathtruncatemacro{\ibeta}{sign(cos(\kvw{beta}))}
    \ifnum\ibeta=1
        \draw[fill=\kvw{color},very thin] 
                        (180:\kvw{radius}) 
                        -- ++(0,-\kvw{h}) arc(180:360:\kvw{radius}) 
                        -- ++(0,\kvw{h})  arc(360:180:\kvw{radius});
    \fi
   \or
    %8: alpha<180,beta<180
    \pgfmathtruncatemacro{\ibeta}{sign(sin(\kvw{alpha})-sin(\kvw{beta}))}
    \ifnum\ibeta=1
       \draw[fill=\kvw{color},very thin]
                        (180:\kvw{radius}) 
                        -- ++(0,-\kvw{h}) arc(180:360:\kvw{radius}) 
                        -- ++(0,\kvw{h})  arc(360:180:\kvw{radius});
    \fi
  \fi
  \path[fill=\kvw{color},draw=black] (0,0)--
  (\kvw{alpha}:\kvw{radius})  arc(\kvw{alpha}:\kvw{beta}:\kvw{radius})
                                     --cycle;
}},
wedge/.cd,alpha/.initial=0,beta/.initial=0,%beta > alpha!
color/.initial=blue,
mix color/.initial=gray,radius/.initial=3cm,h/.initial=1cm,
/tikz/.cd,
pics/3d pie chart/.style={code={
  \def\kvw##1{\pgfkeysvalueof{/tikz/3d pie chart/##1}}
  \begin{scope}[yscale=\kvw{aspect},transform shape]
    \path[preaction={fill=black,opacity=.8,
           path fading=circle with fuzzy edge 20 percent}] 
           (0,-\kvw{h}-\kvw{radius}/4.5) 
           circle[radius=1.05*\kvw{radius}];
    \pgfmathsetmacro{\mysum}{0}   
    \foreach \XX/\ZZ  in {#1}  
    {\pgfmathsetmacro{\mysum}{\mysum+\XX}
     \xdef\mysum{\mysum}}
    \pgfmathsetmacro{\myangle}{\kvw{alpha0}}
    \foreach \XX/\ZZ [count=\YY starting from 0,remember=\myangle as \myangle] in {#1} 
    {\pgfmathsetmacro{\myangleB}{\myangle+\XX*(360/\mysum)}
     \pgfmathsetmacro{\mycolor}{{\kvw{colors}}[\YY]}
     \pic{wedge={alpha=\myangle,beta=\myangleB,color=\mycolor,
        radius/.expanded=\kvw{radius},
        h/.expanded=\kvw{h}
        }};
     \fill (\myangle/2+\myangleB/2:\kvw{radius}*\kvw{eccentricity})
      coordinate (\kvw{cname}-\YY) circle[radius=2pt];
     \pgfmathtruncatemacro{\mysign}{sign(cos(\myangle/2+\myangleB/2))} 
     \draw[thick] (\kvw{cname}-\YY)  -- 
      ++(\myangle/2+\myangleB/2:\kvw{armA}) -- ++ 
      (\mysign*3,0)
      \ifnum\mysign<0
        node[above right,transform shape=false]{\ZZ}
        node[below right,transform shape=false]{\XX\%}
      \else
        node[above left,transform shape=false]{\ZZ}
        node[below left,transform shape=false]{\XX\%}
      \fi;  
     \pgfmathsetmacro{\myangle}{\myangleB}
    }
    \shade[left color=black,middle color=white,right color=gray,opacity=0.4]
                          (180:\kvw{radius}) 
                          -- ++(0,-\kvw{h}) arc(180:360:\kvw{radius}) 
                          -- ++(0,\kvw{h})  arc(360:180:\kvw{radius});
  \end{scope}                      
}},
3d pie chart/.cd,
colors/.initial={"blue","red","orange","green","yellow"},
radius/.initial=3cm,h/.initial=1cm,alpha0/.initial=0,
aspect/.initial=0.6,eccentricity/.initial=0.7,cname/.initial=c,
armA/.initial=2cm,armB/.initial=3cm
}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
 \path[3d pie chart/.cd, alpha0=45] pic{3d pie chart={36/Nuts,64/Honey}};
\end{tikzpicture}
\end{document}

答案1

代码正在使用remember=\myangle as \myangle。使用相同的命令名看起来有点可疑(而且这也没有记录)。它可能是因为未记录的副作用(第二个命令未正确初始化)而起作用,但是https://github.com/pgf-tikz/pgf/issues/855这一点已经改变。

\documentclass{article}
\usepackage{tikz}
\begin{document}

\newcommand\myangle{45}
\foreach \x [remember=\myangle as \myangle] in{a,b}
{
  \x,\myangle
}

\foreach \x [remember=\myangle as \myanglelast] in{a,b}
{
  \x,\myangle
}
\end{document}

在 texlive 2019 中提供

在此处输入图片描述

以及 texlive 2021

在此处输入图片描述

这意味着你正在丢失你的值,因为它\myangle被改为 0。在我看来,最好重写代码,以便使用两个命令。但你也可以试试这个

\pgfmathsetmacro{\myangleSTART}{\kvw{alpha0}}
\foreach \XX/\ZZ [count=\YY starting from 0,remember=\myangle as \myangle (initially \myangleSTART)] in {#1}

答案2

您的 tex 文件在我的 TeX Live 2021 中运行良好,并生成以下图片: 在此处输入图片描述

相关内容