\GlsAddKey
我正在尝试使用预设字段对 中的键创建进行编码\glsaddkey
。换句话说,我对 所做的操作bar
,我希望能够对任意键执行。
附言:我不明白\glsaddstoragekey
,所以我把它留在那里以防有人想评论。
\documentclass{article}
\usepackage{glossaries}
\usepackage{xparse}
\makenoidxglossaries
\glsaddstoragekey*{foo}% key
{NA}% default value
{\glsentryfoo}% command analogous to \glsentrytext
\glsaddkey*{bar}% key
{NA}% default value
{\glsentrybar}% command analogous to \glsentrytext
{\Glsentrybar}% command analogous to \Glsentrytext
{\glsbar}% command analogous to \glstext
{\Glsbar}% command analogous to \Glstext
{\GLSbar}% command analogous to \GLStext
\newglossaryentry{x}
{
name={X},
description={next letter ??},
% description={letter \glsfoo{x}}%,
bar={Y}
}
\newglossaryentry{y}
{
name={Y},
description={next letter \glsbar{y}},
% description={next letter ??},
bar={Z}
}
\ExplSyntaxOn
\NewDocumentCommand\GlsAddKey{m}
{
\glsaddkey*
{#1}% key
{NA}% default value
{\use:c{glsentry#1}}% command analogous to \glsentrytext
{\use:c{Glsentry#1}}% command analogous to \Glsentrytext
{\use:c{gls#1}}% command analogous to \glstext
{\use:c{Gls#1}}% command analogous to \Glstext
{\use:c{GLS#1}}% command analogous to \GLStext
}
\ExplSyntaxOff
% \GlsAddKey{qux} % LaTeX Error: Command \use:c already defined.
% \newglossaryentry{z}
% {
% name={Z},
% description={next letter \glsqux{z}},
%% description={next letter ??},
% qux={(none)}
% }
\begin{document}
%\gls{x}
%\glsfoo{x} % ERROR
\gls{y}
%\glsbar{y}
\printnoidxglossaries
\end{document}
答案1
当你这样做时\glsaddkey*{bar}
,glossaries
包会执行类似
\newcommand{\glsentrybar}{...}
对于由命令名称组成的每个参数。当然
\newcommand{\use:c{glsentryqux}}
将会以错误消息中显示的方式失败。您需要提供一个已经形成的令牌。一种快速(但不太好)的方法是提前进行必要的扩展。
\NewDocumentCommand\GlsAddKey{m}
{
\use:x
{
\exp_not:N \glsaddkey*
{#1}% key
{NA}% default value
{\exp_not:c{glsentry#1}}% command analogous to \glsentrytext
{\exp_not:c{Glsentry#1}}% command analogous to \Glsentrytext
{\exp_not:c{gls#1}}% command analogous to \glstext
{\exp_not:c{Gls#1}}% command analogous to \Glstext
{\exp_not:c{GLS#1}}% command analogous to \GLStext
}
}
\ExplSyntaxOff
更加expl3
时尚的版本:
\documentclass{article}
\usepackage{glossaries}
\usepackage{xparse}
\makenoidxglossaries
\glsaddstoragekey*{foo}% key
{NA}% default value
{\glsentryfoo}% command analogous to \glsentrytext
\glsaddkey*{bar}% key
{NA}% default value
{\glsentrybar}% command analogous to \glsentrytext
{\Glsentrybar}% command analogous to \Glsentrytext
{\glsbar}% command analogous to \glstext
{\Glsbar}% command analogous to \Glstext
{\GLSbar}% command analogous to \GLStext
\newglossaryentry{x}
{
name={X},
description={letter \glsentryfoo{x}}%,
bar={Y}
}
\newglossaryentry{y}
{
name={Y},
description={next letter \glsbar{y}},
bar={Z}
}
\ExplSyntaxOn
\NewDocumentCommand\GlsAddKey{O{NA}m}
{
\erwann_glsaddkey:nn { #1 } { #2 }
}
\cs_new_protected:Nn \erwann_glsaddkey:nn
{
\__erwann_glsaddkey:nnccccc
{ #2 } % key
{ #1 } % default value
{ glsentry#2 }
{ Glsentry#2 }
{ gls#2 }
{ Gls#2 }
{ GLS#2 }
}
\cs_new_protected:Nn \__erwann_glsaddkey:nnNNNNN
{
\glsaddkey* { #1 } { #2 } #3 #4 #5 #6 #7
}
\cs_generate_variant:Nn \__erwann_glsaddkey:nnNNNNN { nnccccc }
\ExplSyntaxOff
\GlsAddKey{qux} % LaTeX Error: Command \use:c already defined.
\newglossaryentry{z}
{
name={Z},
description={next letter \glsqux{z}},
qux={(none)}
}
\begin{document}
\gls{x}
%\glsfoo{x} % ERROR
\gls{y}
\glsbar{y}
\gls{z}
\glsqux{z}
\printnoidxglossaries
\end{document}