Pgfkeys 选择键嵌套在另一个键中

Pgfkeys 选择键嵌套在另一个键中

我正在尝试创建一个嵌套有两个选择键的键,如下所示:

engagement={target designation=hostile target, stage=engage}

这是我迄今为止得到的 MWE:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
  \makeatletter
  \newcommand*{\testengagement}[1]{
    \tikzset{
    /MilSymb/.cd,
    engagement/.value required,
    /MilSymb/engagement/.cd,
    engagement symb/.store in=\MilSymb@EngagementSymb,
    stage text/.store in=\MilSymb@StageText,
    engagement style/.store in=\MilSymb@EngagementStyle,
    target designation/.is choice,
    target designation/none/.style={/MilSymb/engagement/.cd, engagement style/.style={/tikz/.cd, fill=blue}},
    target designation/hostile target/.style={/MilSymb/engagement/.cd, engagement style/.style={/tikz/.cd, fill=red}},
    stage/.is choice,
    stage/assign or cover/.style={/MilSymb/engagement/.cd, stage text=ASN},
    stage/engage/.style={/MilSymb/engagement/.cd, stage text=ENG},
    stage=assign or cover,
    target designation=none,
    /MilSymb/.cd,
    engagement/.style={/MilSymb/engagement/.cd, engagement symb={\fontsize{6}{0}\selectfont \draw[\MilSymb@EngagementStyle]($(M.north west) + (0,0.05)$) rectangle ($(M.north east) + (0,0.25)$) node [pos=.5] {\MilSymb@StageText};}},
    #1,
    }%
    \draw[local bounding box=M] (0,0) rectangle (1,1);
    \MilSymb@EngagementSymb
    }
    \makeatother
\begin{document}
  \begin{tikzpicture}
  \testengagement{engagement={target designation=hostile target, stage=engage}}
  \end{tikzpicture}
  \begin{tikzpicture}
  \draw[local bounding box=M] (0,0) rectangle (1,1);
  \fontsize{6}{0}\selectfont \draw[fill=red]($(M.north west) + (0,0.05)$) rectangle ($(M.north east) + (0,0.25)$) node [pos=.5] {ENG};
  \end{tikzpicture}
\end{document}

两张图像应该相同,但第一张图像没有显示正确的颜色或文本。

比较图

我还收到以下错误信息:

\pgfkeyscurrentkey ->\MilSymb@EngagementStyle 
                                              
l.32 ...t designation=hostile target, stage=engage}}
                                                  
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

答案1

您的样式的问题engagement在于,它不使用传递给它的值,而只是吞噬它。因此,您engagement = {...}只是设置样式中定义的内容(因此设置engagement symb),但您的target destinationstage被完全忽略。

此外,更改样式内的路径相当危险,因为样式之后路径不会重置,因此样式之后的所有后续键都将被解释为该路径。以下通过使用.code = \pgfqkeys...而不是.style\pgfqkeys将使用给定的路径,然后恢复先前的路径)解决了该问题。我还删除了您的许多其他.cd用法,而是在有问题的地方简单地使用完整的键路径。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\pgfkeys
  {
    /MilSymb/.cd,
    engagement/.code=
      \pgfqkeys{/MilSymb/engagement}
        {%
          engagement symb =
            {%
              \fontsize{6}{0}\selectfont
              % we need to expand the style
              \expandafter\draw\expandafter[\MilSymb@EngagementStyle]
                ($(M.north west) + (0,0.05)$)
                  rectangle
                ($(M.north east) + (0,0.25)$)
                  node [pos=.5] {\MilSymb@StageText};%
            },
          #1%
        },
    engagement/.value required,
    %
    /MilSymb/engagement/.cd,
    engagement symb/.store in=\MilSymb@EngagementSymb,
    stage text/.store in=\MilSymb@StageText,
    engagement style/.store in=\MilSymb@EngagementStyle,
    target designation/.is choice,
    target designation/none/.style=
      {/MilSymb/engagement/engagement style={fill=blue}},
    target designation/hostile target/.style=
      {/MilSymb/engagement/engagement style={fill=red}},
    stage/.is choice,
    stage/assign or cover/.style={/MilSymb/engagement/stage text=ASN},
    stage/engage/.style={/MilSymb/engagement/stage text=ENG},
    stage=assign or cover,
    target designation=none,
    % initial values (so that the macros are defined)
    engagement symb = {},
    stage text = {},
    engagement style = {},
  }

\newcommand*{\testengagement}[1]
  {%
    \pgfqkeys{/MilSymb}{#1}%
    \draw[local bounding box=M] (0,0) rectangle (1,1);
    \MilSymb@EngagementSymb
  }
\makeatother

\begin{document}
  \begin{tikzpicture}
  \testengagement{engagement={target designation=hostile target, stage=engage}}
  \end{tikzpicture}
  \begin{tikzpicture}
  \draw[local bounding box=M] (0,0) rectangle (1,1);
  \fontsize{6}{0}\selectfont \draw[fill=red]($(M.north west) + (0,0.05)$) rectangle ($(M.north east) + (0,0.25)$) node [pos=.5] {ENG};
  \end{tikzpicture}
\end{document}

在此处输入图片描述


每次使用时重新定义所有键的变体:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter

\newcommand*{\testengagement}[1]
  {%
    \pgfkeys
      {
        /MilSymb/engagement/.cd,
        engagement symb/.store in=\MilSymb@EngagementSymb,
        stage text/.store in=\MilSymb@StageText,
        engagement style/.store in=\MilSymb@EngagementStyle,
        target designation/.is choice,
        target designation/none/.style=
          {/MilSymb/engagement/engagement style={fill=blue}},
        target designation/hostile target/.style=
          {/MilSymb/engagement/engagement style={fill=red}},
        stage/.is choice,
        stage/assign or cover/.style={/MilSymb/engagement/stage text=ASN},
        stage/engage/.style={/MilSymb/engagement/stage text=ENG},
        stage=assign or cover,
        target designation=none,
        % initial values (so that the macros are defined)
        engagement symb = {},
        stage text = {},
        engagement style = {},
        %
        /MilSymb/.cd,
        engagement/.code=
          \pgfqkeys{/MilSymb/engagement}
            {%
              engagement symb =
                {%
                  \fontsize{6}{0}\selectfont
                  % we need to expand the style
                  \expandafter\draw\expandafter[\MilSymb@EngagementStyle]
                    ($(M.north west) + (0,0.05)$)
                      rectangle
                    ($(M.north east) + (0,0.25)$)
                      node [pos=.5] {\MilSymb@StageText};%
                },
              ##1%
            },
        engagement/.value required,
        %
        #1%
      }
    \draw[local bounding box=M] (0,0) rectangle (1,1);
    \MilSymb@EngagementSymb
  }
\makeatother

\begin{document}
  \begin{tikzpicture}
  \testengagement{engagement={target designation=hostile target, stage=engage}}
  \end{tikzpicture}
  \begin{tikzpicture}
  \draw[local bounding box=M] (0,0) rectangle (1,1);
  \fontsize{6}{0}\selectfont \draw[fill=red]($(M.north west) + (0,0.05)$) rectangle ($(M.north east) + (0,0.25)$) node [pos=.5] {ENG};
  \end{tikzpicture}
\end{document}

输出如上。

相关内容