4

I've written the following code for setting values of a key as part of a package I'm working on, and for some odd reason the default doesn't seem to work. When the package this is part of is specified without any options, I'd expect reset-question to equal section and reset-example to equal none. Instead, both are set to 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}
  },
}

Commenting out the reset-all keys doesn't seem to have any effect on the default values of reset-question and reset-example either.

Here's a maximal working example to illustrate how this snippet will be used:

\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}

I apologise for the huge length of these examples; I can try to trim them down if that would be more useful. The full code for this package is in a GitHub repository.

4
  • I think I managed to fix this by adding a \keys_set:nn { teachingtools } { reset-all } line right at the end of the package forcing that option to have its default value, which then propagates through the others.
    – Robbie
    Nov 19, 2017 at 10:11
  • You know the difference between initial and default, don't you?
    – TeXnician
    Nov 19, 2017 at 11:24
  • I do now after finding initial in the manual. I really need to get into the habit of reading these manuals in full instead of just skimming to find out how to solve whatever task I'm working on next. Sometimes I think I'm too impatient to become a decent programmer.
    – Robbie
    Nov 19, 2017 at 11:40
  • @Robbie Unfortunately, I am guilty of the same habit, but reading interface3 in full would require super-human ability. Jul 28, 2019 at 8:48

1 Answer 1

5

I've "fixed" your code in the following way: I've added the initial statement. You need to understand the difference: .default defines what happens when you do \keys_set:nn {...} {mykey} (what mykey should default to). .initial defines what should be the value of mykey at the beginning (initialization).

To achieve an initial value you need to define reset-question and reset-example before defining reset-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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .