Estilos definidos em outro caminho não funcionam em \node

Estilos definidos em outro caminho não funcionam em \node

Eu faço o seguinte código para testar o uso do \nodein tikz. Acho que usar estilos definidos em outro caminho (/a neste exemplo) causará problemas - consulte o MWE para obter detalhes. Alguém pode me ajudar com a causa e como lidar com isso?

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}

Responder1

Você está perguntando por que circlefunciona. A resposta para isso é um pouco delicada e está contida no seguinte bloco de código de 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%
}%

Como você pode ver, TikZ experimenta todo tipo de coisas. Isso é conveniente para os usuários porque eles podem dizer circleem vez de shape=circlee redem vez de color=red.

Você poderia fazer seu código funcionar com /.try. No entanto, eu não recomendo isso. Em vez disso, recomendo apenas dar TikZ as informações que faltam, ou seja, dizer shape=circleem vez de circlee /pgf/minimum sizeem vez de 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}

A razão pela qual estou recomendando isso é que se você exagerar nas /.trycoisas e assim por diante, fica cada vez menos claro qual chave tem prioridade.

informação relacionada