下面是一个基本示例,其中显示 a)“实例将在模板中设置一些键”,以及 b)“⟨参数⟩的键值列表适用于⟨新模板⟩,并且在创建实例时无法更改。”我无法理解我得到的结果。我原本以为两者都是“hello world!”,只是括号不同。而对于 b),我预计会出现一个警告,告知密钥受到限制。我错过了什么?
\documentclass{article}
\usepackage{xtemplate}
\ExplSyntaxOn
\DeclareObjectType{say}{1}
\DeclareTemplateInterface{say}{parent}{1}
{key : tokenlist}
\DeclareTemplateCode{say}{parent}{1}
{key = \l__say_key_tl}
{(#1~\tl_use:N\l__say_key_tl)}
% Manual:
% DeclareRestrictedTemplate{⟨object type⟩} {⟨parent template⟩} {⟨new template⟩}{⟨parameters⟩}
% Creates a copy of the⟨parent template⟩for the⟨object type⟩called⟨new template⟩.
%The key–value list of⟨parameters⟩applies in the⟨new template⟩and cannot be changed whencreating an instance.
\DeclareRestrictedTemplate{say}{parent}{child}
{key = world!}
\DeclareTemplateCode{say}{child}{1}
{key = \l__say_key_tl}
{\{#1~\tl_use:N\l__say_key_tl\}}
\ExplSyntaxOff
\begin{document}
% Manual:
% \DeclareInstance{⟨object type⟩} {⟨instance⟩} {⟨template⟩} {⟨parameters⟩}
% This function uses a⟨template⟩for an⟨object type⟩to create an⟨instance⟩.
% The⟨instance⟩will be set up using the⟨parameters⟩, which will set some of the⟨keys⟩in the⟨template⟩.
\ShowTemplateCode{say}{parent}
\DeclareInstance{say}{foo}{parent}{key=world!}
\UseInstance{say}{foo}{hello} % expected (hello~world)
\ShowInstanceValues{say}{foo}
%> \template code > say/parent=\protected\long macro:#1->(#1 \tl_use:N
%\l__say_key_tl ).
%<recently read> }
%l.36 \ShowTemplateCode{say}{parent}
%The instance 'foo' of object type 'say' has values:
%> key => world!
%> from template => parent.
%<recently read> }
%l.39 \ShowInstanceValues{say}{foo}
\ShowTemplateCode{say}{child}
\DeclareInstance{say}{bar}{child}{key=universe!}
\UseInstance{say}{bar}{hello} % expected {hello~world}
\ShowInstanceValues{say}{bar}
%> \template code > say/child=\protected\long macro:#1->\{#1 \tl_use:N
%\l__say_key_tl \}.
%<recently read> }
%l.41 \ShowTemplateCode{say}{child}
%LaTeX Font Info: Trying to load font information for OMS+cmr on input line 4
%3.
%(/usr/local/texlive/2020/texmf-dist/tex/latex/base/omscmr.fd
%File: omscmr.fd 2019/12/16 v2.5j Standard LaTeX font definitions
%)
%LaTeX Font Info: Font shape `OMS/cmr/m/n' in size <10> not available
%(Font) Font shape `OMS/cmsy/m/n' tried instead on input line 43.
%The instance 'bar' of object type 'say' has values:
%> key => universe!
%> from template => child.
%<recently read> }
%l.44 \ShowInstanceValues{say}{bar}
\end{document}
答案1
限制性模板的代码存在一些问题。这些是现已修复在即将发布的开发代码中。
正如评论中指出的那样,您还需要\AssignTemplateKeys
。使用固定代码
\DeclareTemplateCode{say}{child}{1}
{key = \l__say_key_tl}
{\AssignTemplateKeys\{#1~\tl_use:N\l__say_key_tl\}}
按预期工作。