我正在尝试设置可以隐式设置其他选择键的选择键。在“父”键和“从属”键中,选择集具有相同的值。
下面的代码或多或少满足了我的要求;如果我传递选项reset-question = section
,那么每次开始新部分时,问题环境的计数器都会重置。但是,如果我给问题添加前缀(带question-prefix = section
),它应该始终重置该级别的键,而不管键的值是什么reset-question
(即,如果已定义,则覆盖该键)。
\documentclass{article}
\usepackage{xparse}
\usepackage{expl3}
\usepackage{l3keys2e}
\usepackage{chngcntr}
\setlength\parindent{0pt}
\begin{document}
\ExplSyntaxOn
\newcounter{question}
\keys_define:nn { teachingtools } {
reset-question .default:n = section,
reset-question .choices:nn = {
part, chapter, section, subsection, subsubsection, paragraph, subparagraph
}
{
\counterwithin*{question}{ \tl_to_str:N \l_keys_choice_tl }
},
reset-question / none .code:n = {
\counterwithout*{question}{section}
},
}
\tl_new:N \l_@@_question_prefix_tl
\keys_define:nn { teachingtools } {
question-prefix .choice:,
question-prefix / part .code:n = {
\tl_set:Nn \l_@@_question_prefix_tl { \arabic{part} }
\keys_set:nn { teachingtools } { reset-question = part }
},
question-prefix / chapter .code:n = {
\tl_set:Nn \l_@@_question_prefix_tl { \arabic{chapter} }
\keys_set:nn { teachingtools } { reset-question = chapter }
},
question-prefix / section .code:n = {
\tl_set:Nn \l_@@_question_prefix_tl { \arabic{section} }
\keys_set:nn { teachingtools } { reset-question = section }
},
question-prefix / subsection .code:n = {
\tl_set:Nn \l_@@_question_prefix_tl { \arabic{subsection} }
\keys_set:nn { teachingtools } { reset-question = subsection }
},
question-prefix / subsubsection .code:n = {
\tl_set:Nn \l_@@_question_prefix_tl { \arabic{subsubsection} }
\keys_set:nn { teachingtools } { reset-question = subsubsection }
},
question-prefix / paragraph .code:n = {
\tl_set:Nn \l_@@_question_prefix_tl { \arabic{paragraph} }
\keys_set:nn { teachingtools } { reset-question = paragraph }
},
question-prefix / subparagraph .code:n = {
\tl_set:Nn \l_@@_question_prefix_tl { \arabic{subparagraph} }
\keys_set:nn { teachingtools } { reset-question = subparagraph }
},
question-prefix / none .code:n = {
\tl_gclear:N \l_@@_question_prefix_tl
},
question-prefix .default:n = none,
question-prefix .initial:n = none,
}
\tl_put_right:Nn \l_@@_question_title_tl { \l_@@_question_prefix_tl }
\NewDocumentCommand \ttsetup { m } {
\keys_set:nn { teachingtools } {#1}
}
\ExplSyntaxOff
\ttsetup{
% reset-all = false,
% reset-question = subsection,
question-prefix = section
}
\end{document}
question-prefix
尝试按照下一个代码示例来简化定义是行不通的。
\documentclass{article}
\usepackage{xparse}
\usepackage{expl3}
\usepackage{l3keys2e}
\usepackage{chngcntr}
\setlength\parindent{0pt}
\begin{document}
\ExplSyntaxOn
\newcounter{question}
\keys_define:nn { teachingtools } {
reset-question .default:n = section,
reset-question .choices:nn = {
part, chapter, section, subsection, subsubsection, paragraph, subparagraph
}
{
\counterwithin*{question}{ \tl_to_str:N \l_keys_choice_tl }
},
reset-question / none .code:n = {
\counterwithout*{question}{section}
},
}
\tl_new:N \l_@@_question_prefix_tl
\keys_define:nn { teachingtools } {
question-prefix .choices:nn = {
part, chapter, section, subsection, subsubsection, paragraph, subparagraph
}
{
\tl_set:Nn \l_@@_question_prefix_tl { \arabic{ \tl_to_str:N \l_keys_choice_tl } }
\keys_set:nn { teachingtools } { reset-question = \tl_to_str:N \l_keys_choice_tl }
},
question-prefix / none .code:n = {
\tl_gclear:N \l_@@_question_prefix_tl
},
question-prefix .default:n = none,
question-prefix .initial:n = none,
}
\tl_put_right:Nn \l_@@_question_title_tl { \l_@@_question_prefix_tl }
\NewDocumentCommand \ttsetup { m } {
\keys_set:nn { teachingtools } {#1}
}
\ExplSyntaxOff
\ttsetup{
% reset-all = false,
% reset-question = subsection,
question-prefix = section
}
\end{document}
相反,我收到以下错误消息:
|'''''''''''''''''''''''''''''''''''''''''''''''
| The key 'teachingtools/reset-question' only accepts predefined values, and
| 'section' is not one of these.
|...............................................
这没有意义,因为section
是 的有效选择reset-section
,所以有些东西没有以正确的格式传递。我在question-prefix .choices:nn
代码块中尝试了各种方法,包括将选择值转换为字符串、获取其整数值以及直接传递令牌列表。我还没有尝试将reset-question
和question-prefix
键分配给组。
为什么这不起作用? 是否可以清理或优化第一部分中的代码?