设置 LaTeX3 键的默认值

设置 LaTeX3 键的默认值

我编写了以下代码来设置我正在开发的包中某个键的值,但出于某种奇怪的原因,默认值似乎不起作用。当指定此包时没有任何选项,我期望等于和等于。reset-question相反sectionreset-example两者none都设置为none

\keys_define:nn { teachingtools } {
  reset-all .choice:,
  reset-all .default:n = false,
  reset-all / part .meta:n = {
    reset-question = part,
    reset-example = part
  },
  reset-all / chapter .meta:n = {
    reset-question = chapter,
    reset-example = chapter
  },
  reset-all / section .meta:n = {
    reset-question = section,
    reset-example = section
  },
  reset-all / subsection .meta:n = {
    reset-question = subsection,
    reset-example = subsection
  },
  reset-all / subsubsection .meta:n = {
    reset-question = subsubsection,
    reset-example = subsubsection
  },
  reset-all / paragraph .meta:n = {
    reset-question = paragraph,
    reset-example = paragraph
  },
  reset-all / subparagraph .meta:n = {
    reset-question = subparagraph,
    reset-example = subparagraph
  },
  reset-all / false .meta:n = {
    reset-question = section,
    reset-example = none
  }
}
\keys_define:nn { teachingtools } {
  reset-question .default:n = section,
  reset-question .choices:nn = {
    part, chapter, section, subsection, subsubsection, paragraph, subparagraph
  }
  {
    \counterwithin*{question}{\l_keys_choice_tl}
  },
  reset-question / none .code:n = {
    \counterwithout*{question}{section}
  },
}
\keys_define:nn { teachingtools } {
  reset-example .default:n = none,
  reset-example .choices:nn = {
    part, chapter, section, subsection, subsubsection, paragraph, subparagraph
  }
  {
    \counterwithin*{example}{\l_keys_choice_tl}
  },
  reset-example / none .code:n = {
    \counterwithout*{example}{section}
  },
}

注释掉这些键似乎对和reset-all的默认值也没有任何影响。reset-questionreset-example

下面是一个最大工作示例,用于说明如何使用此代码片段:

\documentclass{article}
\usepackage{xparse}
\usepackage{expl3}
\usepackage{l3keys2e}
\usepackage{chngcntr}

\setlength\parindent{0pt}

\ExplSyntaxOn

\keys_define:nn { teachingtools } {
  reset-all .choice:,
  reset-all .default:n = false,
  reset-all / part .meta:n = {
    reset-question = part,
    reset-example = part
  },
  reset-all / chapter .meta:n = {
    reset-question = chapter,
    reset-example = chapter
  },
  reset-all / section .meta:n = {
    reset-question = section,
    reset-example = section
  },
  reset-all / subsection .meta:n = {
    reset-question = subsection,
    reset-example = subsection
  },
  reset-all / subsubsection .meta:n = {
    reset-question = subsubsection,
    reset-example = subsubsection
  },
  reset-all / paragraph .meta:n = {
    reset-question = paragraph,
    reset-example = paragraph
  },
  reset-all / subparagraph .meta:n = {
    reset-question = subparagraph,
    reset-example = subparagraph
  },
  reset-all / false .meta:n = {
    reset-question = section,
    reset-example = none
  }
}
\keys_define:nn { teachingtools } {
  reset-question .default:n = section,
  reset-question .choices:nn = {
    part, chapter, section, subsection, subsubsection, paragraph, subparagraph
  }
  {
    \counterwithin*{question}{\l_keys_choice_tl}
  },
  reset-question / none .code:n = {
    \counterwithout*{question}{section}
  },
}
\keys_define:nn { teachingtools } {
  reset-example .default:n = none,
  reset-example .choices:nn = {
    part, chapter, section, subsection, subsubsection, paragraph, subparagraph
  }
  {
    \counterwithin*{example}{\l_keys_choice_tl}
  },
  reset-example / none .code:n = {
    \counterwithout*{example}{section}
  },
}

\newcounter{question}
\newcounter{example}

\NewDocumentEnvironment { question } { s } {
  \par
  \IfBooleanTF #1
    { { \bfseries Question.\par } }
    {
      \refstepcounter{question}
      {\bfseries Question~\thequestion.\par}
    }
  \begin{itshape}
}
{\end{itshape}\par}
\cs_new:cpn {question*} {\question*}
\cs_new_eq:cN {endquestion*} \endquestion
\NewDocumentEnvironment { example } { s } {
  \par
  \IfBooleanTF #1
    { { \bfseries Example.\par } }
    {
      \refstepcounter{example}
      {\bfseries Example~\theexample.\par}
    }
}
{\par}
\cs_new:cpn {example*} {\example*}
\cs_new_eq:cN {endexample*} \endexample
\NewDocumentCommand \ttsetup { m } {
  \keys_set:nn { teachingtools } {#1}
}
\ExplSyntaxOff

\begin{document}

\ttsetup{
  % reset-all = false,
  % reset-question = subsection,
  % reset-example = section,
}

\section{A section}

\begin{question}
The question in a section
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsection{A subsection}

\begin{question}
The question in a subsection
\end{question}

\begin{example*}
    This is an example of the \texttt{example*} environment.
\end{example*}

\begin{question}
Another question in a subsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsubsection{A subsubsection}

\begin{question*}
A starred question in a subsubsection
\end{question*}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}


\begin{question}
The question in a subsubsection
\end{question}

\begin{question}
Another question in a subsubsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsubsection{A subsubsection}

\begin{question}
The question in a subsubsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\begin{question}
Another question in a subsubsection
\end{question}

\subsection{Another subsection}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\begin{question}
Another question in a subsection
\end{question}

\begin{question}
What is this question?
\end{question}

\section{Another section}

\begin{question}
Another question in another section
\end{question}

\begin{question}
Another question in another section
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsection{A subsection}

\begin{question}
The question in a subsection
\end{question}

\begin{question}
Another question in a subsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\begin{question}
Another question in a subsection
\end{question}

\subsubsection{A subsubsection}

\begin{question}
The question in a subsubsection
\end{question}

\begin{question}
Another question in a subsubsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsubsection{A subsubsection}

\begin{question}
The question in a subsubsection
\end{question}

\begin{question}
Another question in a subsubsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\end{document}

我为这些示例的冗长而道歉;如果这样更有用,我可以尝试缩短它们。此包的完整代码位于 GitHub 存储库

答案1

我已经按照以下方式“修复”了您的代码:我添加了语句initial。您需要了解区别:.default定义执行操作时发生的情况\keys_set:nn {...} {mykey}mykey应默认为)。定义开始时.initial的值应该是什么(初始化)。mykey

为了实现某个initial值,您需要在定义之前定义reset-question和。reset-examplereset-all

\documentclass{article}
\usepackage{xparse}
\usepackage{expl3}
\usepackage{l3keys2e}
\usepackage{chngcntr}

\setlength\parindent{0pt}

\ExplSyntaxOn
\newcounter{question}
\newcounter{example}

\keys_define:nn { teachingtools } {
    reset-question .default:n = section,
  reset-question .choices:nn = {
    part, chapter, section, subsection, subsubsection, paragraph, subparagraph
  }
  {
    \counterwithin*{question}{\l_keys_choice_tl}
  },
  reset-question / none .code:n = {
    \counterwithout*{question}{section}
  },
  reset-example .default:n = none,
  reset-example .choices:nn = {
    part, chapter, section, subsection, subsubsection, paragraph, subparagraph
  }
  {
    \counterwithin*{example}{\l_keys_choice_tl}
  },
  reset-example / none .code:n = {
    \counterwithout*{example}{section}
  },
  reset-all .choice:,
  reset-all / part .meta:n = {
    reset-question = part,
    reset-example = part
  },
  reset-all / chapter .meta:n = {
    reset-question = chapter,
    reset-example = chapter
  },
  reset-all / section .meta:n = {
    reset-question = section,
    reset-example = section
  },
  reset-all / subsection .meta:n = {
    reset-question = subsection,
    reset-example = subsection
  },
  reset-all / subsubsection .meta:n = {
    reset-question = subsubsection,
    reset-example = subsubsection
  },
  reset-all / paragraph .meta:n = {
    reset-question = paragraph,
    reset-example = paragraph
  },
  reset-all / subparagraph .meta:n = {
    reset-question = subparagraph,
    reset-example = subparagraph
  },
  reset-all / false .meta:n = {
    reset-question = section,
    reset-example = none
  },
  reset-all .default:n = false,
  reset-all .initial:n = false,
}

\NewDocumentEnvironment { question } { s } {
  \par
  \IfBooleanTF #1
    { { \bfseries Question.\par } }
    {
      \refstepcounter{question}
      {\bfseries Question~\thequestion.\par}
    }
  \begin{itshape}
}
{\end{itshape}\par}
\cs_new:cpn {question*} {\question*}
\cs_new_eq:cN {endquestion*} \endquestion
\NewDocumentEnvironment { example } { s } {
  \par
  \IfBooleanTF #1
    { { \bfseries Example.\par } }
    {
      \refstepcounter{example}
      {\bfseries Example~\theexample.\par}
    }
}
{\par}
\cs_new:cpn {example*} {\example*}
\cs_new_eq:cN {endexample*} \endexample
\NewDocumentCommand \ttsetup { m } {
  \keys_set:nn { teachingtools } {#1}
}
\ExplSyntaxOff

\begin{document}

\ttsetup{
  % reset-all = false,
  % reset-question = subsection,
  % reset-example = section,
}

\section{A section}

\begin{question}
The question in a section
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsection{A subsection}

\begin{question}
The question in a subsection
\end{question}

\begin{example*}
    This is an example of the \texttt{example*} environment.
\end{example*}

\begin{question}
Another question in a subsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsubsection{A subsubsection}

\begin{question*}
A starred question in a subsubsection
\end{question*}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}


\begin{question}
The question in a subsubsection
\end{question}

\begin{question}
Another question in a subsubsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsubsection{A subsubsection}

\begin{question}
The question in a subsubsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\begin{question}
Another question in a subsubsection
\end{question}

\subsection{Another subsection}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\begin{question}
Another question in a subsection
\end{question}

\begin{question}
What is this question?
\end{question}

\section{Another section}

\begin{question}
Another question in another section
\end{question}

\begin{question}
Another question in another section
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsection{A subsection}

\begin{question}
The question in a subsection
\end{question}

\begin{question}
Another question in a subsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\begin{question}
Another question in a subsection
\end{question}

\subsubsection{A subsubsection}

\begin{question}
The question in a subsubsection
\end{question}

\begin{question}
Another question in a subsubsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\subsubsection{A subsubsection}

\begin{question}
The question in a subsubsection
\end{question}

\begin{question}
Another question in a subsubsection
\end{question}

\begin{example}
    This is an example of the \texttt{example} environment.
\end{example}

\end{document}

相关内容