![expl3:在特定条件下启用的按键](https://linux22.com/image/467150/expl3%EF%BC%9A%E5%9C%A8%E7%89%B9%E5%AE%9A%E6%9D%A1%E4%BB%B6%E4%B8%8B%E5%90%AF%E7%94%A8%E7%9A%84%E6%8C%89%E9%94%AE.png)
在我的套餐中,有以下选项:
\keys_define:nn { crefthe-setting }
{
, variant .tl_set:N = \l__crefthe_variant_tl
, nom .meta:n = { variant = Nominativ }
, gen .meta:n = { variant = Genitiv }
, dat .meta:n = { variant = Dativ }
, akk .meta:n = { variant = Akkusativ }
...
}
但是,诸如 之类的快捷键nom
只能在\languagename
等于时启用ngerman
。如何实现这样的条件键?
下面是 MWE。
\documentclass{article}
\usepackage[english,ngerman]{babel}
\ExplSyntaxOn
\tl_new:N \l__crefthe_variant_tl
\keys_define:nn { crefthe-setting }
{
, variant .tl_set:N = \l__crefthe_variant_tl
, nom .meta:n = { variant = Nominativ }
, gen .meta:n = { variant = Genitiv }
, dat .meta:n = { variant = Dativ }
, akk .meta:n = { variant = Akkusativ }
, unknown .code:n = {}
}
\NewDocumentCommand \TestFunction { m }
{
\tl_set:Nn \l__crefthe_variant_tl {}
\keys_set:nn { crefthe-setting } { #1 }
\tl_use:N \l__crefthe_variant_tl
}
\ExplSyntaxOff
\begin{document}
\selectlanguage{english}
\TestFunction{nom} (\languagename) % should be empty
\selectlanguage{ngerman}
\TestFunction{nom} (\languagename)
\end{document}
答案1
当您尝试设置这些键时,将进行以下评估\languagename
(手册中有一个警告,由于 catcode 的怪异性建议使用该iflang
包,但我猜如果我们使用基于字符串的比较,catcodes 应该不是问题),并且当键以错误的语言使用时会引发警告(您可以将其更改为错误)。
\documentclass{article}
\usepackage[english,ngerman]{babel}
\ExplSyntaxOn
\msg_new:nnn { crefthe } { unavailable-key }
{ Key~ `#1'~ unavailable~ outside~ of~ language~ `#2'. }
\cs_new_protected:Npn \__crefthe_lang_key:nn #1#2
{
\str_if_eq:VnTF \languagename {#1}
{ \keys_set:nn { crefthe-setting } {#2} }
{
\msg_warning:nnxx { crefthe } { unavailable-key }
{ \l_keys_key_tl } {#1}
}
}
\tl_new:N \l__crefthe_variant_tl
\keys_define:nn { crefthe-setting }
{
, variant .tl_set:N = \l__crefthe_variant_tl
, nom .code:n = \__crefthe_lang_key:nn {ngerman} { variant = Nominativ }
, gen .code:n = \__crefthe_lang_key:nn {ngerman} { variant = Genitiv }
, dat .code:n = \__crefthe_lang_key:nn {ngerman} { variant = Dativ }
, akk .code:n = \__crefthe_lang_key:nn {ngerman} { variant = Akkusativ }
, unknown .code:n = {}
}
\NewDocumentCommand \TestFunction { m }
{
\tl_set:Nn \l__crefthe_variant_tl {}
\keys_set:nn { crefthe-setting } { #1 }
\tl_use:N \l__crefthe_variant_tl
}
\ExplSyntaxOff
\begin{document}
\selectlanguage{english}
\TestFunction{nom} (\languagename) % should be empty
\selectlanguage{ngerman}
\TestFunction{nom} (\languagename)
\end{document}
可能有更聪明的方法来做到这一点,但这应该可以。