自定义 tikz 包无法编译

自定义 tikz 包无法编译

我想包括这个图书馆在我的文档中。单独使用它很好。但是当我包含时,我在 Ball.sty 包中收到一个错误,如下所示:

\ProvidesPackage{Ball}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This Block can draw small Ball
%Elementwise or reduction operations can be drawn with this
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\tikzset{Ball/.pic={\tikzset{/sphere/.cd,#1}        

\pgfmathsetmacro{\r}{\radius*\scale}

\shade[ball color=\fill,opacity=\opacity] (0,0,0) circle (\r);
\draw (0,0,0) circle [radius=\r] node[scale=4*\r] {\logo};

\coordinate (\name-anchor) at ( 0 , 0  , 0) ;
\coordinate (\name-east)   at ( \r, 0  , 0) ;
\coordinate (\name-west)   at (-\r, 0  , 0) ;
\coordinate (\name-north)  at ( 0 , \r , 0) ;
\coordinate (\name-south)  at ( 0 , -\r, 0) ;

\coordinate (\name-southwest)  at ( -0.70710678118 * \r , -0.70710678118 * \r, 0) ;
\coordinate (\name-northeast)  at ( 0.70710678118 * \r , 0.70710678118 * \r, 0) ;

\path (\name-south) + (0,-20pt) coordinate (caption-node) 
edge ["\textcolor{black}{\bf \caption}"'] (caption-node); %Ball caption

},
/sphere/.search also={/tikz},
/sphere/.cd,
radius/.store       in=\radius,
scale/.store        in=\scale,
caption/.store      in=\caption,
name/.store         in=\name,
fill/.store         in=\fill,
logo/.store         in=\logo,
opacity/.store      in=\opacity,
logo=$\Sigma$,
fill=green,
opacity=0.10,
scale=0.2,
radius=0.5,
caption=,
name=,
}

关键行是fill/.store in=\fill,fill=green,。出于某种原因,我得到了错误

Missing number, treated as zero.

<to be read again> 
                   g
l.55 \newskip\LTleft       \LTleft=\fill

A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

有些后续错误是基于第一个错误而产生的。

经过几个小时的调试,我完全不知道是什么导致了这个错误。也许其他人有猜测?

答案1

您不应该将与 Ti 一致的键存储在宏中Z 命令\fill。在我看来,最好不要在显式宏中存储任何内容,而是使用\pgfkeysvalueof{...}。因此,您可能希望将其用于Ball.sty

\ProvidesPackage{Ball}
\tikzset{Ball/.pic={\tikzset{/sphere/.cd,#1}        
\def\skv##1{\pgfkeysvalueof{/sphere/##1}}
\pgfmathsetmacro{\r}{\skv{radius}*\skv{scale}}

\shade[ball color=\skv{fill},opacity=\skv{opacity}] (0,0,0) circle (\r);
\draw (0,0,0) circle [radius=\r] node[scale=4*\r] {\skv{logo}};

\coordinate (\skv{name}-anchor) at ( 0 , 0  , 0) ;
\coordinate (\skv{name}-east)   at ( \r, 0  , 0) ;
\coordinate (\skv{name}-west)   at (-\r, 0  , 0) ;
\coordinate (\skv{name}-north)  at ( 0 , \r , 0) ;
\coordinate (\skv{name}-south)  at ( 0 , -\r, 0) ;

\coordinate (\skv{name}-southwest)  at ( -0.70710678118 * \r , -0.70710678118 * \r, 0) ;
\coordinate (\skv{name}-northeast)  at ( 0.70710678118 * \r , 0.70710678118 * \r, 0) ;

\path (\skv{name}-south) + (0,-20pt) coordinate (caption-node) 
edge ["\textcolor{black}{\bfseries\skv{caption}}"'] (caption-node); %Ball caption

},
/sphere/.search also={/tikz},
/sphere/.cd,
radius/.initial=0.5,
scale/.initial=0.2,
caption/.initial={},
name/.initial={},
fill/.initial=green,
logo/.initial={$\Sigma$},
opacity/.initial=0.1}
\endinput

正如你所见,它起作用了:

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{quotes}
\begin{document}
\tikzset{Ball/.pic={\tikzset{/sphere/.cd,#1}        
\def\skv##1{\pgfkeysvalueof{/sphere/##1}}
\pgfmathsetmacro{\r}{\skv{radius}*\skv{scale}}

\shade[ball color=\skv{fill},opacity=\skv{opacity}] (0,0,0) circle (\r);
\draw (0,0,0) circle [radius=\r] node[scale=4*\r] {\skv{logo}};

\coordinate (\skv{name}-anchor) at ( 0 , 0  , 0) ;
\coordinate (\skv{name}-east)   at ( \r, 0  , 0) ;
\coordinate (\skv{name}-west)   at (-\r, 0  , 0) ;
\coordinate (\skv{name}-north)  at ( 0 , \r , 0) ;
\coordinate (\skv{name}-south)  at ( 0 , -\r, 0) ;

\coordinate (\skv{name}-southwest)  at ( -0.70710678118 * \r , -0.70710678118 * \r, 0) ;
\coordinate (\skv{name}-northeast)  at ( 0.70710678118 * \r , 0.70710678118 * \r, 0) ;

\path (\skv{name}-south) + (0,-20pt) coordinate (caption-node) 
edge ["\textcolor{black}{\bfseries\skv{caption}}"'] (caption-node); %Ball caption

},
/sphere/.search also={/tikz},
/sphere/.cd,
radius/.initial=0.5,
scale/.initial=0.2,
caption/.initial={},
name/.initial={},
fill/.initial=green,
logo/.initial={$\Sigma$},
opacity/.initial=0.1}

\begin{tikzpicture}
 \path (0,0,0) pic{Ball} (3,0,0) pic{Ball={fill=blue,radius=4}}; 
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容