我目前正在尝试为我的词汇表构建一个 \glsxtrnewsymbol 包装器命令,该命令使用键值来使其更有条理(例如,函数调用应如何查看附件)。
\addysmbol{
ref = input-voltage,
description = Input Voltage,
name = \ensuremath{U_{out}},
unit = \si{\volt},
}
到目前为止,我已经尝试使用 pgfkeys 和 keyval 来实现这一点,但都遇到了错误,即它对所有函数都使用了函数最后一次调用传递的参数。
\makeatletter
\RequirePackage{keyval}
\define@key{test}{unit}{%
\def\thisunit{#1}}
\define@key{test}{ref}{%
\edef\thisref{#1}}
\define@key{test}{description}{%
\edef\thisdescription{#1}}
\define@key{test}{name}{%
\edef\thisname{#1}}
\newcommand*{\addsymbol}[1]{%%
\setkeys{test}{#1}%
\glsxtrnewsymbol[description={\thisdescription},unit=\thisunit]{\thisref}{\thisname}%%
}
\makeatother
答案1
嵌套\PassFirstToSecond
/可能\Exchange
可以达到这样的效果:
\makeatletter
\RequirePackage{keyval}
\define@key{test}{unit}{\def\thisunit{#1}}%
\define@key{test}{ref}{\protected@edef\thisref{#1}}%
\define@key{test}{description}{\protected@edef\thisdescription{#1}}%
\define@key{test}{name}{\protected@edef\thisname{#1}}%
\newcommand\PassFirstToSecond[2]{#2{#1}}%
\newcommand\Exchange[2]{#2#1}%
\newcommand*{\addsymbol}[1]{%%
\begingrpup
\setkeys{test}{#1}%
\expandafter\PassFirstToSecond\expandafter{\thisname}{%
\expandafter\PassFirstToSecond\expandafter{\thisref}{%
\expandafter\Exchange\expandafter{\thisunit}{%
\expandafter\PassFirstToSecond\expandafter{\thidescription}{%
\endgroup\glsxtrnewsymbol[description=%
},unit=%
}]%
}%
}%
}
\makeatother
\expanded
如果你使用具有基本体和的TeX 引擎\unnexpanded
,则下面的方法可能会奏效:
\makeatletter
\RequirePackage{keyval}
\define@key{test}{unit}{\def\thisunit{#1}}%
\define@key{test}{ref}{\def\thisref{#1}}%
\define@key{test}{description}{\def\thisdescription{#1}}%
\define@key{test}{name}{\def\thisname{#1}}%
\newcommand*{\addsymbol}[1]{%%
\begingroup
\setkeys{test}{#1}%
\let\protect\@unexpandable@protect
\expanded{%
\endgroup
\noexpand\glsxtrnewsymbol
[description={\thisdescription},%
unit=\unexpanded\expandafter{\thisunit}]{\thisref}{\thisname}%%
}%
}
\makeatother
对于这两个示例,建议对宏\thisunit
、\thisref
和\thisdescription
设置默认定义\thisname
。