0

I'm trying to set up choice keys that can implicitly set other choice keys. In both the "parent" and "dependent" keys, the set of choices have the same values.

The code below does more-or-less what I want; if I pass the option reset-question = section, then the counter for the question environment will be reset each time a new section is started. However, if I give the question a prefix (with question-prefix = section), it should always reset the key at that level, regardless of the value of the reset-question key (i.e. overriding that key, if it is defined).

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

Attempting to simplify the defintion of question-prefix along the lines of the next code sample never works.

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

Instead, I get the following error message:

|'''''''''''''''''''''''''''''''''''''''''''''''
| The key 'teachingtools/reset-question' only accepts predefined values, and
| 'section' is not one of these.
|...............................................

This doesn't make sense, as section is a valid choice for reset-section, so something is not being passed in the right format. I've tried various things in the question-prefix .choices:nn code block, including converting the choice value to a string, getting its integer value, and passing the token list directly. I haven't tried assigning the reset-question and question-prefix keys to groups yet.

Why doesn't this work? Is it possible to clean up or optimise the code in the first part?

  • The second block produces that error if you replace the question-prefix key definition in the first block with its contents. – Robbie Nov 21 '17 at 4:46
  • 2
    Not unless I add all the other stuff to make a proper MWE. The problem is you're converting the thing to a string and then expecting it to behave normally. Don't do that and it will probably work. However, I can't test without usable code, so implementation is left as an exercise for the reader. – cfr Nov 21 '17 at 4:51
  • Have you tried removing those string conversions? Did that work out? – TeXnician Dec 17 '17 at 15:10
  • Btw: \keys_set:nx { teachingtools } { reset-question = \l_keys_choice_tl } seems to help (remember to generate the variant). But without a proper testable MWE it's hard to say whether this really solves your problems. – TeXnician Dec 17 '17 at 15:19
  • @TeXnician this works exactly as desired. Does the first MWE I provided in the question not work? – Robbie Dec 19 '17 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.