根据另一组密钥定义一组密钥

根据另一组密钥定义一组密钥

如果我有 2 个宏,一个在另一个里面,而最上面的宏有更多的参数,我希望能够将最上面的宏的键设置为底部宏的扩展,类似于此(不起作用):

\pgfkeys{/phili/testA/.cd, 
  a/.store in = \a,
  b/.store in = \b,
  a           = a,
  b           = b,
}

\pgfkeys{/phili/testB/.cd, 
  \pgfkeys{/phili/testA/.cd, #1}
  c/.store in = \c,
  c           = c,
}

有没有办法做到这一点pgfkeys

这是一个完整的例子:

\documentclass{article}

\usepackage{pgfkeys}

\pgfkeys{/phili/testA/.cd, 
  a/.store in = \a,
  b/.store in = \b,
  a           = a,
  b           = b,
}
\pgfkeys{/phili/testB/.cd,
  a/.store in = \a,
  b/.store in = \b,
  c/.store in = \c,
  a           = a,
  b           = b,
  c           = c,
}

\newcommand{\testMacroA}[1][]{
  \pgfkeys{/phili/testA/.cd, #1}
  \a\b
}
\newcommand{\testMacroB}[1][]{
  \pgfkeys{/phili/testB/.cd, #1}
  \testMacroA[a = a, b = b] % If I use `\a` and `\b`, it gets stuck
  % I think it works if I just use `\testMacroA`, without arguments
  \c
}

\begin{document}
  \testMacroA[a = 1, b = 2]
  \testMacroB[a = 1, b = 2, c = 3]
\end{document}

答案1

(编辑:\a和已经在 LaTeX 2ε 内核中定义。因此我们改用、和。)\b\c\MyA\MyB\MyC

您可以使用 key-handler使为 key-path 定义/phili/testA但未为 key-path 定义的键/phili/testB也可通过 key-path 使用:/phili/testB.search also

(pgfkeys 是 TikZ/PGF 包的一部分,可以在综合 TeX 档案网络 (CTAN)- 手册位于http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf. pgfkeys 在“第 VII 部分实用程序”的“87 密钥管理”一章中进行了描述。.search also在“87.4.7 转发处理程序”一节中进行了记录。)

\documentclass{article}

\usepackage{pgfkeys}

\pgfkeys{%
  /phili/testA/.cd,  
    a/.store in = \MyA,  
    b/.store in = \MyB, 
  /phili/testB/.search also={/phili/testA},
  /phili/testB/.cd,  
    c/.store in = \MyC,
  % Define generic defaults:
    a = a,  
    b = b, 
    c = c,
}

\newcommand{\testMacroA}[1][]{%
  \begingroup
  \pgfkeys{/phili/testA/.cd,#1}%
  \MyA
  \MyB
  \endgroup
}
\newcommand{\testMacroB}[1][]{%
  \begingroup
  \pgfkeys{/phili/testB/.cd, #1}%
  % As \pgfkeys just (re)defined the macros \a, \b and \c
  % now run \testMacroA without in its optional argument
  % specifying those keys that (re)define one of those macros;
  % be aware that the things done by \testMacroA are done within
  % an extra group:
  \testMacroA
  \MyC
  \endgroup
}

\begin{document}
  \testMacroA

  \testMacroA[a = 1, b = 2]

  \testMacroB[a = 1, b = 2, c = 3]

  % This yiels an error as the key `c` is not defined
  % in the path `/phili/testA`:
  % \testMacroA[a = 1, b = 2, c = 3]
\end{document}

在此处输入图片描述


如果您希望为每个宏中的每个键指定默认值,并在产生对的调用时使用\testMacroA指定的值覆盖的默认值,您可以这样做。像这样:\testMacroB\testMacroB\testMacroA

\documentclass{article}

\usepackage{pgfkeys}

\pgfkeys{%
  /phili/testA/.cd,  
    a/.store in = \MyA,  
    b/.store in = \MyB, 
  /phili/testB/.search also={/phili/testA},
  /phili/testB/.cd,  
    c/.store in = \MyC,
  % Define generic defaults:
    a = (GenericDefaultA),
    b = (GenericDefaultB),
    c = (GenericDefaultC),
}

\newcommand{\testMacroA}[1][]{%
  \begingroup
  \pgfkeys{/phili/testA/.cd, a=(testMacroADefaultA), b=(testMacroADefaultB), #1}%
  \MyA
  \MyB
  \endgroup
}
\newcommand{\testMacroB}[1][]{%
  \begingroup
  \pgfkeys{/phili/testB/.cd, a=(testMacroBDefaultA), b=(testMacroBDefaultB), c=(testMacroBDefaultC), #1}%
  % Providing an optional argument with \testMacroA is needed for overriding
  % \testMacroA's defaults with what is specified with \testMacroB - be that
  % \testMacroB's defaults or s.th. that comes from providing an optional
  % argument with the call to \testMacroB; also some expansion-trickery
  % is needed:
  \expanded{%
    \unexpanded{\testMacroA}[{a={\unexpanded\expandafter{\MyA}}, b={\unexpanded\expandafter{\MyB}}}]%
  }%
  \MyC
  \endgroup
}

\begin{document}
  \testMacroA

  \testMacroA[a = 1]

  \testMacroA[b = 2]

  \testMacroA[a = 1, b = 2]

  \testMacroB

  \testMacroB[a = 1]

  \testMacroB[b = 2]

  \testMacroB[c = 3]

  \testMacroB[a = 1, b = 2]

  \testMacroB[a = 1, c = 3]

  \testMacroB[b = 2, c = 3]

  \testMacroB[a = 1, b = 2, c = 3]

  % This yiels an error as the key `c` is not defined
  % in the path `/phili/testA`:
  % \testMacroA[a = 1, b = 2, c = 3]
\end{document}

在此处输入图片描述

相关内容