覆盖 pgf 键的默认值

覆盖 pgf 键的默认值

我定义了一个具有默认值的 pgf 键。在下面的代码中,我可以在创建的默认参数中覆盖其值,但无法使用宏更改该值pgfsetkeyvalue

\documentclass{article}
\usepackage{fontspec}

\usepackage{tikz}
\pgfkeys{
   /titleblock/.is family, /titleblock,
   titlesize/.default = 48,
   titlesize/.store in = \titlesize,
   titlesize
}

\newcommand{\titleblock}[1][]{%
   \pgfkeys{/titleblock/.cd, #1}%
    \node[align=left, inner sep=0mm, outer sep=0mm,
    font={\fontsize{\titlesize}{2\titlesize}\selectfont}]
    (title) 
    at (0,0)
    {This is my test title};
}


\begin{document}

\begin{tikzpicture}
  \titleblock
\end{tikzpicture}


 \begin{tikzpicture}
   \titleblock[titlesize=25pt]
 \end{tikzpicture}

\pgfkeyssetvalue{/titleblock/titlesize}{10}

\begin{tikzpicture}
  \titleblock
\end{tikzpicture}

\end{document}

有没有什么办法可以不用可选参数来改变这个值\titleblock

答案1

Key/titleblock/titlesize并不直接存储值,而是定义一个处理程序将其存储在 中\titlesize。因此,\pgfkeys和 友元可用于设置,如下所示\titleblock

\pgfkeys{/titleblock/titlesize=10}

或者使用设置命令:

\newcommand*{\titleblocksetup}{\pgfqkeys{/titleblock}}
...
\titleblocksetup{titlesize=10}

完整示例:

\documentclass{article}
\usepackage{fontspec}

\usepackage{tikz}
\pgfkeys{
   /titleblock/.is family, /titleblock,
   titlesize/.default = 48,
   titlesize/.store in = \titlesize,
   titlesize
}
\newcommand*{\titleblocksetup}{\pgfqkeys{/titleblock}}

\newcommand{\titleblock}[1][]{%
   \pgfkeys{/titleblock/.cd, #1}%
    \node[align=left, inner sep=0mm, outer sep=0mm,
    font={\fontsize{\titlesize}{2\titlesize}\selectfont}]
    (title)
    at (0,0)
    {This is my test title};
}

\begin{document}

\begin{tikzpicture}
  \titleblock
\end{tikzpicture}


 \begin{tikzpicture}
   \titleblock[titlesize=25pt]
 \end{tikzpicture}

\titleblocksetup{titlesize=10}

\begin{tikzpicture}
  \titleblock
\end{tikzpicture}

\end{document}

结果

答案2

正如 Heiko 所说,问题在于\pgfkeyssetvalue没有调用处理程序,因此\titlesize没有被设置\pgfkeyssetvalue{/titleblock/titlesize}{10}

当我使用时,pgfkeys我更喜欢将值存储在键中,而不是让键定义宏,因为这对我来说似乎效率低下。以下是我编写示例的方式:

\documentclass{article}

\usepackage{tikz}
\pgfkeys{/titleblock/.is family, /titleblock,
   titlesize/.initial = 25,% initial value of key
}

\newcommand{\titleblock}[1][]{%
   \pgfkeys{/titleblock, #1}%
   \pgfkeysgetvalue{/titleblock/titlesize}{\titlesize}% key val -> \titlesize
   \node[align=left, inner sep=0mm, outer sep=0mm,
         font={\fontsize{\titlesize}{2\titlesize}\selectfont}]
        (title) at (0,0) {This is my test title};
}

\begin{document}

    \begin{tikzpicture}
      \titleblock
    \end{tikzpicture}

    \begin{tikzpicture}
      \titleblock[titlesize=15]
    \end{tikzpicture}

    \pgfkeyssetvalue{/titleblock/titlesize}{10}% set key value

    \begin{tikzpicture}
      \titleblock
    \end{tikzpicture}

\end{document}

输出与 Heiko 的输出非常相似,但不那么漂亮:

在此处输入图片描述

\titleblock宏中,我已将当前键值提取到宏中\titlesize。这并不是必需的,但我不想写

font={\fontsize{\pgfkeysvalueof{/titleblock/titlesize}}%
               {2\pgfkeysvalueof{/titleblock/titlesize}}\selectfont}

最后,我实际上分别使用了 25、15 和 10 的字体大小。这只是因为我去掉了那条\usepackage{fontspec}线并使用了 pdflatex。

相关内容