如何通过 /utils/exec(或允许代码执行的类似方法)设置用户定义(非全局或默认)样式的键的值?

如何通过 /utils/exec(或允许代码执行的类似方法)设置用户定义(非全局或默认)样式的键的值?

我需要通过该样式的来改变样式color的键的值。changeStyleColor/utils/exec

/utils/exec我可以通过设置全局键的值\tikzset{/tikz/myKey=myValue},也可以通过 设置位于用户定义目录中的键的值\tikzset{/tikz/myDirectory/myKey=myValue}。但是我如何设置属于用户定义样式的键的值呢?

\documentclass{standalone}
\usepackage{tikz}
\tikzset
  { % global color
    color=orange,
    changeGlobalColor/.style=
      { /utils/exec=
          { \tikzset{/tikz/color=red}
          }
      },
    changeDirectoryColor/.style=
      { /utils/exec=
          { \tikzset{/tikz/directory/color=green}
          }
      },
    directory/.cd,
    color/.initial=blue,
    /tikz/.cd,
    changeStyleColor/.style=
      { /utils/exec=
          { % how to change "pink" to something else?
          },
        color=pink
      }
  }
\begin{document}
  \begin{tikzpicture}
    % global color, orange (initial)
    \path node(n1){n1};
    % global color, red (changed from orange through /utils/exec)
    \path node(n2)at(n1.south)[changeGlobalColor]{n2};
    % directory color, green (changed from blue through /utils/exec)
    \path node(n3)at(n2.south)[changeDirectoryColor,color=\pgfkeysvalueof{/tikz/directory/color}]{n3};
    % style color, pink (initial); needs to be changed from pink to something else
    %  (through /utils/exec)
    \path node(n4)at(n3.south)[changeStyleColor]{n4};
  \end{tikzpicture}
\end{document}

答案1

您可以执行以下操作,但恕我直言,这非常违背此应用程序中 pgf 键的精神。

\documentclass{standalone}
\usepackage{tikz}
\tikzset
  { % global color
    color=orange,
    changeGlobalColor/.style=
      { /utils/exec=
          { \tikzset{/tikz/color=red}
          }
      },
    changeDirectoryColor/.style=
      { /utils/exec=
          { \tikzset{/tikz/directory/color=green}
          }
      },
    directory/.cd,
    color/.initial=blue,
    /tikz/.cd,
    changeStyleColor/.style=
      { /utils/exec=
          { \tikzset{/tikz/directory/color=#1}
          },
        color=\pgfkeysvalueof{/tikz/directory/color}
      }
  }
\begin{document}
  \begin{tikzpicture}
    % global color, orange (initial)
    \path node(n1){n1};
    % global color, red (changed from orange through /utils/exec)
    \path node(n2)at(n1.south)[changeGlobalColor]{n2};
    % directory color, green (changed from blue through /utils/exec)
    \path node(n3)at(n2.south)[changeDirectoryColor,color=\pgfkeysvalueof{/tikz/directory/color}]{n3};
    % style color, pink (initial); needs to be changed from pink to something else
    %  (through /utils/exec)
    \path node(n4)at(n3.south)[changeStyleColor]{n4};
    \path node(n5)at(n4.south)[changeStyleColor=pink]{n5};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容