仅通过阅读手册,我不太清楚和 PGF 键类型之间的细微差别.initial
。.default
有人能用一个简单的例子来解释和演示这些差别吗?
答案1
它们有两种不同的用途:使用 定义的键/.initial=<value or string>
是值存储键,其初始值设置为<value or string>
。
如果没有提供参数,关键字/.default
定义将使用什么值作为定义的键的参数。/.code=<code>
两者类似,但并不等同。一个键可以同时具有一个值(可以使用 进行查询)\pgfkeysvalueof{/key}
和一个代码(当您使用 调用该键时将运行该代码)\pgfkeys{/key}
。
如果没有.code
定义,则可以使用.initial
或 ,.default
其行为几乎相同:使用\pgfkeys{/key/.initial=value}
或设置值\pgfkeys{/key/.default=value}
,使用 查询值\pgfkeys{/key}
。但是,如果您已定义.code
,以及使用 的值.initial
,则要查询需要调用 的值\pgfkeysvalueof{/key}
。
\documentclass{article}
\usepackage{pgfkeys}
\begin{document}
%% Case 1: We want a key to store a value
% Just passing a value to an undefined key fails:
%\pgfkeys{firstkey=red}
% It first needs to be initialised
\pgfkeys{firstkey/.initial=red}
% Then we can get the value
\pgfkeys{firstkey}
% After the key has been initialised, we can change the stored value using simple assignments
\pgfkeys{firstkey=blue}
\pgfkeys{firstkey}
%% Case 2: We want a key to execute code using the argument
\pgfkeys{secondkey/.code=Your argument: \textbf{#1}}
\pgfkeys{secondkey=Some words}
% If we don't use an argument, it's assumed to be empty
\pgfkeys{secondkey}
% We can provide a default value to be used if no argument is provided:
\pgfkeys{secondkey/.default=Nothing}
\pgfkeys{secondkey}
% Let's define a .code key that just returns the argument...
\pgfkeys{/thirdkey/.code=#1}
% ...and give it an initial value...
\pgfkeys{/thirdkey/.initial=green}
% ...and a default argument
\pgfkeys{/thirdkey/.default=red}
% If we query the value, we get "green"
\pgfkeysvalueof{/thirdkey}
% If we run the code, we get "red"
\pgfkeys{/thirdkey}
\end{document}
答案2
虽然这并没有特别为已经提供的答案添加任何内容,但下面的例子帮助我更直观地理解了差异:
\documentclass{article}
\usepackage{pgfkeys}
\setlength{\parindent}{0mm}
\begin{document}
%% Create the keys, default values, etc.
\pgfkeys{%
/mythingy/.is family, /mythingy,
usecolor/.default = green,
usecolor/.code = {You have chosen to use the color #1.},
usecolor/.initial = purple,
}
%% Create a command that will use the keys
\newcommand{\mythingy}[1][]{%
\pgfkeys{/mythingy, #1} %% The usecolor/.code is executed
\pgfkeysvalueof{/mythingy/usecolor} %% Retrieves only the VALUE of the usecolor key
}
%% Use the command along with the keys in various different manners
\textbf{Attempt 1}:\\
%% Here, the usecolor/.code is executed using "blue" as the argument
\mythingy[usecolor=blue]
\vspace{\baselineskip}
\textbf{Attempt 2}:\\
%% Here, the usecolor/.code is execute using the /.default argument value "green"
\mythingy[usecolor]
\vspace{\baselineskip}
\textbf{Attempt 3}:\\
%% Here, the usecolor key is initialized to the /.initial value "purple"
%% However, the usecolor/.code is not executed !!!
\mythingy
\end{document}
输出为:
为了理解这种行为,理解以下概念的区别非常重要:键的值和与该键关联的代码的参数。
我可以想象这种区别以以下方式使用(参见下面的代码),尽管这个例子很怪诞(在我看来),因为它应该使用一个不同的第二个键radius
来实际控制半径:
\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\setlength{\parindent}{0mm}
\begin{document}
\pgfkeys{%
/mypainter/.is family, /mypainter,
drawcircle/.default = true,
drawcircle/.code = {%
\ifstrequal{#1}{true}{
\begin{tikzpicture}
\draw (0,0) circle[radius=\pgfkeysvalueof{/mypainter/drawcircle}];
\end{tikzpicture}
}{}
},
drawcircle/.initial = 1cm,
}
\newcommand{\mypainter}[1][]{%
Executing {\ttfamily drawcircle/.code}: \pgfkeys{/mypainter, #1}
\par
Value of key {\ttfamily drawcircle}: \pgfkeysvalueof{/mypainter/drawcircle}
}
\textbf{Attempt 1}:\\
\mypainter[drawcircle=true]
\vspace{3\baselineskip}
\textbf{Attempt 2}:\\
\mypainter[drawcircle]
\vspace{3\baselineskip}
\textbf{Attempt 3}:\\
%% The order of the "keys" is important here (try switching the two)
\mypainter[drawcircle/.initial=2cm, drawcircle]
\vspace{3\baselineskip}
\textbf{Attempt 4}:\\
%% Notice the drawcircle/.initial now remains set to 2cm !!!
\mypainter
\vspace{3\baselineskip}
\textbf{Attempt 5}:\\
\mypainter[drawcircle=false]
\end{document}
最后一段代码的输出是: