抱歉,下面我的最小示例的代码有点长。在此代码中,我定义了两个宏:第一个宏\SC
绘制半圆,第二个宏\ARC
绘制圆弧。第一个宏使用第二个宏。
\SC
使用一些选项,如radius
和。如果我想要一些来自 tikz 的键,diameter
我需要使用。/SCopt/.search also={/tikz}
\ARC
也使用了一些选项,例如through
和,angle
但这里仅through
使用。
我的英语不太好,所以我不明白 pfgmanual 中 pgfkeys 部分的微妙之处。我的问题是如何管理密钥和路径\pgfkeys{/ARCopt/.cd,...
。
如果我不使用
/ARCopt/.unknown/.code = {\let\searchname=\pgfkeyscurrentname
\pgfkeysalso{\searchname/.try=#1,
/tikz/\searchname/.retry=#1}},
我无法使用钥匙diameter
,我得到了
错误:我不知道密钥‘/tikz/diameter’。
这是正常的,这是第一个宏的键\SC
,但在这种情况下,我可以使用blue
而不是color=blue
使用/ARCopt/.search also={/tikz}
(感谢 Paul Gaborit)
问题是:是否有可能获得两种可能性:第二个宏中的未知键和来自 pgf/tikz 的键?
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\makeatletter
\def\SCR(#1,#2){% semi circle with radius defined by 2 pts
\coordinate (LastPT) at (barycentric cs:#2=1,#1=-2) ;
\coordinate (CenterPT) at (#1);
\coordinate (StartPT) at (#2);
}
%-----------------------------------------------------
\def\SCD(#1,#2){% semi circle with diameter defined by 2 pts
\coordinate (CenterPT) at (barycentric cs:#1=1,#2=1) ;
\coordinate (StartPT) at (#1);
\coordinate (LastPT) at (#2);}
%-----------------------------------------------------
\pgfkeys{/SCopt/.cd,
opt/.is choice,
opt/diameter/.code =\def\num{1},
opt/radius/.code =\def\num{0},
opt/.default =radius,
opt,
diameter/.style={opt=diameter},
radius/.style={opt=radius},
/SCopt/.search also={/tikz}
}
%-----------------------------------------------------
\def\SC{\@ifnextchar[{\SCb}{\SCb[]}}
\def\SCb[#1](#2){%
\pgfqkeys{/SCopt}{#1}
\ifcase\num%
\SCR(#2) % semi circle with radius defined by 2 pts
\or% 1
\SCD(#2) % semi circle with diameter defined by 2 pts
\fi
\ARC[#1](CenterPT,StartPT,LastPT)}%
%-----------------------------------------------------
\pgfkeys{/ARCopt/.cd,
type/.is choice,
type/through/.code = \def\numa{0},
type/angle/.code = \def\numa{1},
type/.default = through,
type,
through/.style = {type=through},% to avoid "type ="
angle/.style = {type=angle},
/ARCopt/.unknown/.code = {\let\searchname=\pgfkeyscurrentname
\pgfkeysalso{\searchname/.try=#1,
/tikz/\searchname/.retry=#1}},
%/ARCopt/.search also={/tikz},
}
%-----------------------------------------------------
\def\ARC{\@ifnextchar[{\ARCb}{\ARCb[]}}
\def\ARCb[#1](#2){%
\pgfqkeys{/ARCopt}{#1}
\ifcase\numa%
\ARCT[#1](#2) % arc with only pts
\or% 1
\ARCA[#1](#2) % arc with 2 pts and one angle
\fi}
\def\ARCT[#1](#2,#3,#4){%
\pgfpointdiff{\pgfpointanchor{#2}{center}}{\pgfpointanchor{#3}{center}}
\pgfmathparse{veclen(\pgf@x,\pgf@y)}
\let\ra\pgfmathresult %it's the radius
\pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}{%
\pgfpointanchor{#3}{center}} %first angle
\let\fa\pgfmathresult
\pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}{%
\pgfpointanchor{#4}{center}} %last angle
\let\la\pgfmathresult
\draw[shift = {(#2)},/ARCopt/.cd,#1] (\fa:\ra pt) arc (\fa:\la:\ra pt);}
\def\ARCA[#1](#2,#3,#4){%
% not defined
}
%-----------------------------------------------------
\makeatother
\begin{document}
\begin{tikzpicture}[point/.style={circle,minimum size=4pt,
inner sep=0pt,outer sep=0pt,fill=black}]
\path coordinate[point] (A) at (0,0)
coordinate[point] (B) at (6,0)
coordinate[point] (C) at (4,0);
\SC[color=red,thick](A,B)
\SC[blue,thick](A,C)
\SC[diameter](A,B)
%\SC[diameter](B,C)
\end{tikzpicture}
\end{document}
答案1
也许我原来的回答还不够清楚。
您可以更换
/ARCopt/.unknown/.code = {\let\searchname=\pgfkeyscurrentname
\pgfkeysalso{\searchname/.try=#1,
/tikz/\searchname/.retry=#1}},
经过
/ARCopt/.search also = {/SCopt,/tikz},
由于在它的定义中/SCopt
也搜索/tikz
,因此应该等同于说
/ARCopt/.search also = {/SCopt},
但后来color=red
失败了。我不知道为什么。
另一方面,如果你删除
/SCopt/.search also={/tikz},
然后 TeX 会抱怨color
未知的密钥。这确实不受欢迎。