在另一個路徑下定義的樣式無法在 \node 中運作

在另一個路徑下定義的樣式無法在 \node 中運作

\node我編寫了以下程式碼來測試in的使用tikz。我發現使用另一個路徑下定義的樣式(本例中為/a)會導致問題 - 請參閱 MWE 以了解詳細資訊。任何人都可以幫助我了解原因以及如何處理?

微量元素:

\documentclass[a4paper]{article}
\usepackage{tikz}
\begin{document}

\pgfkeys{/a/.search also={/tikz},
  /a/.cd,
  myshape/.style={fill=red,circle},
  size/.style={minimum size=#1*0.5cm},
  size/.default=1
}

% example No.1:
\tikz\node[fill=red,circle]{AAA}; %The option "circle" can be used dirctly in "\node".

% example No.2:
% Contrasted with example No.1, why can "circle" not be used in myshape/.style which leads to unsuccessful compile?
% \tikz[/tikz/.search also={/a}]\node[myshape]{AAA}; 

% example No.3:
% Why does the "size=3" not work?
\tikz[/tikz/.search also={/a}]\node[fill=red,size=3]{AAA}; 

\end{document}

答案1

你問為什麼circle有效。這個問題的答案實際上有點微妙,包含在以下程式碼區塊中tikz.code.tex

\pgfkeys{/tikz/.unknown/.code=%
  % Is it a pgf key?
  \let\tikz@key\pgfkeyscurrentname%
  \pgfkeys{/pgf/\tikz@key/.try={#1}}%
  \ifpgfkeyssuccess%
  \else%
    \expandafter\pgfutil@in@\expandafter!\expandafter{\tikz@key}%
    \ifpgfutil@in@%
      % this is a color!
      \expandafter\tikz@addoption\expandafter{\expandafter\tikz@compat@color@set\expandafter{\tikz@key}}%
      \edef\tikz@textcolor{\tikz@key}%
    \else%
      \pgfutil@doifcolorelse{\tikz@key}
      {%
        \expandafter\tikz@addoption\expandafter{\expandafter\tikz@compat@color@set\expandafter{\tikz@key}}%
        \edef\tikz@textcolor{\tikz@key}%
      }%
      {%
        % Ok, second chance: This might be an arrow specification:
        \expandafter\pgfutil@in@\expandafter-\expandafter{\tikz@key}%
        \ifpgfutil@in@%
          % Ah, an arrow spec!
          \expandafter\tikz@processarrows\expandafter{\tikz@key}%
        \else%
          % Ok, third chance: A shape!
          \expandafter\ifx\csname pgf@sh@s@\tikz@key\endcsname\relax%
            \pgfkeys{/errors/unknown key/.expand
              once=\expandafter{\expandafter/\expandafter t\expandafter i\expandafter k\expandafter z\expandafter/\tikz@key}{#1}}%
          \else%
            \edef\tikz@shape{\tikz@key}%
          \fi%
        \fi%
      }%
    \fi%
  \fi%
}%

正如你所看到的,蒂kZ嘗試了各種各樣的事情。這對用戶來說很方便,因為他們可以說circle代替shape=circlered代替color=red

您可以使您的代碼與/.try.但是,我不建議這樣做。相反,我建議只給予 TikZ 缺少的訊息,即shape=circle代替circle/pgf/minimum size代替minimum size

\documentclass[a4paper]{article}
\usepackage{tikz}
\begin{document}

\pgfkeys{/a/.search also={/tikz},
  /a/.cd,
  myshape/.style={fill=red,shape=circle},
  size/.style={/pgf/minimum size=#1*0.5cm},
  size/.default=1
}

% example No.1:
\tikz\node[fill=red,circle]{AAA}; %The option "circle" can be used dirctly in "\node".

% example No.2:
% Contrasted with example No.1, why can "circle" not be used in myshape/.style which leads to unsuccessful compile?
\tikz[/tikz/.search also={/a}]\node[myshape]{AAA}; 

% example No.3:
% Why does the "size=3" not work?
\tikz[/tikz/.search also={/a}]\node[fill=red,size=3]{AAA}; 

\end{document}

我推薦這個的原因是,如果你過度使用/.try諸如此類的東西,那麼哪個鍵具有優先權就會變得越來越不清楚。

相關內容