属性可重用性

属性可重用性

我正在尝试创建一个命令,该命令允许我为游戏创建多个项目。我的主要问题来自该命令的可重用性。事实上,其中一些命令有超过 9 个参数,这让我转向了key_define。我也查看了,property但我很难找到有关如何充分使用它的可用文档,所以我改用键值。

总体而言,我的代码所做的就是创建许多快捷方式,以便以后可以将其放入我的文档中。假设我创建了这两个字符:

\createCharacter{Barry}<main>[lastname=Allen, prof=scientist]{39}

\createCharacter*{Jessy}<side>[lastname=Quick, nickname=Jess, prof=professional runner]{24}

当我调用时,\mainFName我得到了“Barry”和\sideFName“Jessy”,正如预期的那样,但是当我调用时,\sideLName我得到了“Quick”,\mainLName我得到的也是“Quick”。所以发生的事情是,它没有采用创建时的值,而是重新评估了它的“当前值”(即最后创建的字符)。

因此,以下是我想要更正并使其工作的代码,而不必为每个角色创建所有 tl_new,最终创建每个物品、魔法和其他可能有用的内容。如果我能够用\createCharacter类似的东西来完成\tl_new:c {\l_#4_firstname_tl},那就太完美了。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       CHARACTER MAKER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ExplSyntaxOn

% Define Special
\tl_new:N  \l_character_firstname_tl
\tl_new:N  \l_character_lastname_tl
\tl_new:N  \l_character_title_tl
\tl_new:N  \l_character_profession_tl
\tl_new:N  \l_character_noblesse_tl
\tl_new:N  \l_character_class_tl
\tl_new:N  \l_character_nick_tl

\int_new:N \l_character_age_int


\keys_define:nn { Character/Identity } {
%   firsname .tl_set:N = \l_character_firstname_tl,
    lastname .tl_set:N = \l_character_lastname_tl,
    title    .tl_set:N = \l_character_title_tl,
    prof     .tl_set:N = \l_character_profession_tl,
    noble    .tl_set:N = \l_character_noblesse_tl,
    class    .tl_set:N = \l_character_class_tl,
    race     .tl_set:N = \l_character_race_tl,
    nickname .tl_set:N = \l_character_nick_tl,
%   age      .int_set:N = \l_character_age_int
%   , unknown  .code:n = {}
}

\NewDocumentCommand{\createCharacter}{s t- m D<>{#3} o m}{

    \IfValueT{#5}{\keys_set:nn{Character/Identity}{#5}}

    \exp_args:Nc \NewDocumentCommand { #4 FName } {}{ #3 }
    \exp_args:Nc \NewDocumentCommand { #4 LName } {}{ \l_character_lastname_tl }
    \exp_args:Nc \NewDocumentCommand { #4 Polite } {}{ \use:c { #4 Title } ~ \use:c { #4 LName } }
    \exp_args:Nc \NewDocumentCommand { #4 Sign } {}{ \use:c { #4 FPolite } , ~ \l_character_noblesse_tl }
    \exp_args:Nc \NewDocumentCommand { #4 Age } {}{ #6 }
    \exp_args:Nc \NewDocumentCommand { #4 Prof } {}{ \l_character_profession_tl }
    \exp_args:Nc \NewDocumentCommand { #4 Class } {}{ \l_character_class_tl }
    \exp_args:Nc \NewDocumentCommand { #4 Race } {}{ \l_character_race_tl }
    \exp_args:Nc \NewDocumentCommand { #4 NName } {}{ \l_character_nick_tl }

    %% more code here but irrelevent for the question %%

}


\ExplSyntaxOff

答案1

我不会使用\NewDocumentCommand这些宏。问题是你存储的是变量,而不是它们的值。

\documentclass{article}
\usepackage{xparse}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       CHARACTER MAKER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ExplSyntaxOn

% Define Special
\tl_new:N  \l_character_firstname_tl
\tl_new:N  \l_character_lastname_tl
\tl_new:N  \l_character_title_tl
\tl_new:N  \l_character_profession_tl
\tl_new:N  \l_character_noblesse_tl
\tl_new:N  \l_character_class_tl
\tl_new:N  \l_character_nick_tl

\int_new:N \l_character_age_int


\keys_define:nn { Character/Identity } {
%   firstname .tl_set:N = \l_character_firstname_tl,
    lastname .tl_set:N = \l_character_lastname_tl,
    title    .tl_set:N = \l_character_title_tl,
    prof     .tl_set:N = \l_character_profession_tl,
    noble    .tl_set:N = \l_character_noblesse_tl,
    class    .tl_set:N = \l_character_class_tl,
    race     .tl_set:N = \l_character_race_tl,
    nickname .tl_set:N = \l_character_nick_tl,
%   age      .int_set:N = \l_character_age_int
%   , unknown  .code:n = {}
}

\NewDocumentCommand{\createCharacter}{s t- m D<>{#3} o m}
 {
  % we don't want that the unset keys are carried over from the previous ones
  \group_begin:
    \IfValueT{#5}{\keys_set:nn{Character/Identity}{#5}}

    \cs_new_protected:cpn { #4 FName }  { #3 }
    \cs_new_protected:cpx { #4 LName }  { \exp_not:V \l_character_lastname_tl }
    \cs_new_protected:cpx { #4 Title }  { \exp_not:V \l_character_title_tl }
    \cs_new_protected:cpn { #4 Polite } { \use:c { #4 Title } ~ \use:c { #4 LName } }
    \cs_new_protected:cpx { #4 Sign }   { \exp_not:c { #4 FPolite } , ~ \exp_not:V \l_character_noblesse_tl }
    \cs_new_protected:cpn { #4 Age }    { #6 }
    \cs_new_protected:cpx { #4 Prof }   { \exp_not:V \l_character_profession_tl }
    \cs_new_protected:cpx { #4 Class }  { \exp_not:V \l_character_class_tl }
    \cs_new_protected:cpx { #4 Race }   { \exp_not:V \l_character_race_tl }
    \cs_new_protected:cpx { #4 NName }  { \exp_not:V \l_character_nick_tl }
  \group_end:

    %% more code here but irrelevent for the question %%

}

\ExplSyntaxOff

\createCharacter{Barry}<main>[lastname=Allen, prof=scientist]{39}
\createCharacter*{Jessy}<side>[lastname=Quick, nickname=Jess, prof=professional runner]{24}

\begin{document}

\mainFName

\sideFName

\sideLName

\mainLName

\end{document}

您需要传递变量的内容。组也很重要,否则角色的未设置值将从上一个角色继承。好的,除非您设置全部每一个特点。

在此处输入图片描述

相关内容