3

I have a LaTeX3e code for creating autokey exercise lists for my students. I need to create a list of exercises with the following structure (it's in Portuguese, but never mind the content):

enter image description here

As it's noticed, the questions are indented via enumerate environment, in which the numbers stay in a distinct "column", in some distance of the actual questions.

Thing is, the command for collecting answer keys, for some reason, doesn't work inside the enumerate environment.

Here's a MWE of my problem:

\documentclass[23pt, oneside]{book}


\usepackage{xparse}
\ExplSyntaxOn
% first define the user interface
\NewDocumentCommand{\adddtolist}{O{general}m}
 {
  \marinho_lists_add:nn { #1 } { #2 }
 }
\NewDocumentCommand{\listoutputformat}{+m}
 {
  \marinho_lists_output_format:n { #1 }
 }
\NewDocumentCommand{\outputlist}{m}
 {
  \marinho_lists_output:n { #1 }
 }

% define the internal functions
\cs_new_protected:Nn \marinho_lists_add:nn
 {
  % create the sequence if it doesn't exist
  \seq_if_exist:cF { l_marinho_lists_#1_seq }
   {
    \seq_new:c { l_marinho_lists_#1_seq }
   }
  % add the item in the form {<number>}{<text>}
  \seq_put_right:cx { l_marinho_lists_#1_seq }
   {% compute the number based on the sequence length
    { \int_to_arabic:n { \seq_count:c { l_marinho_lists_#1_seq } + 1 } }
    { \exp_not:n { #2 } }
   }
 }

\cs_new_protected:Nn \marinho_lists_output_format:n
 {% the private function holds the format
  \cs_set:Nn \__marinho_lists_output_current:nn { #1 }
 }
\marinho_lists_output_format:n { #1~--~#2 \par } % a default

\cs_new_protected:Nn \marinho_lists_output:n
 {% map the sequence using the current format
  \seq_map_function:cN { l_marinho_lists_#1_seq } \__marinho_lists_item:n
 }
\cs_new:Nn \__marinho_lists_item:n
 {% \__marinho_lists_item:n receives an argument in the form {<number>}{<text>}
  % which we pass to \__marinho_lists_output_current:nn as two arguments
  \__marinho_lists_output_current:nn #1
 }

\ExplSyntaxOff
\newcommand{\key}[1]{\adddtolist[keylist]{#1}}
\newcommand{\showkey}{\outputlist{keylist}}

\usepackage{changepage}
\begin{document}

1. Here's my first question.
\key{As you see, it appears on the list by the end.}

2. Here's my second question.
\key{Again, it appears on the list os keys.}

\begin{enumerate}
\item However, if I try to put the command inside this environment
\key{It doesn't appear on the list.}
\end{enumerate}

\listoutputformat{\textbf{Question #1}: #2\par}
\showkey

\end{document}

One solution I tried was to indent the document text with the changepage package, using the adjustwidth environment, but it didn't work either.

Any thoughts on some solution, on how could I list my questions with the number as described?

6

First and most important, there is no LaTeX3ε (not in the foreseeable future, at least). LaTeX2ε was initially supposed to be a step from LaTeX 2.09 towards LaTeX3, thus this name (see here), and LaTeX3 is not released as a format yet (it's scheduled to 3019). Since we're not planning LaTeX4 just yet, we don't have a LaTeX3ε ;-)


Jokes apart, your code didn't work because LaTeX environments form a TeX group, so non-global assignments are lost when the environments ends. Using \seq_gput_right:cx instead of \seq_put_right:cx (and renaming the seq variables to g_...) solves the problem:

enter image description here

Other than that, nice code!

\documentclass[23pt, oneside]{book}


\usepackage{xparse}
\ExplSyntaxOn
% first define the user interface
\NewDocumentCommand{\adddtolist}{O{general}m}
 {
  \marinho_lists_add:nn { #1 } { #2 }
 }
\NewDocumentCommand{\listoutputformat}{+m}
 {
  \marinho_lists_output_format:n { #1 }
 }
\NewDocumentCommand{\outputlist}{m}
 {
  \marinho_lists_output:n { #1 }
 }

% define the internal functions
\cs_new_protected:Nn \marinho_lists_add:nn
 {
  % create the sequence if it doesn't exist
  \seq_if_exist:cF { g_marinho_lists_#1_seq }
   {
    \seq_new:c { g_marinho_lists_#1_seq }
   }
  % add the item in the form {<number>}{<text>}
  \seq_gput_right:cx { g_marinho_lists_#1_seq }
   {% compute the number based on the sequence length
    { \int_to_arabic:n { \seq_count:c { g_marinho_lists_#1_seq } + 1 } }
    { \exp_not:n { #2 } }
   }
 }

\cs_new_protected:Nn \marinho_lists_output_format:n
 {% the private function holds the format
  \cs_set:Nn \__marinho_lists_output_current:nn { #1 }
 }
\marinho_lists_output_format:n { #1~--~#2 \par } % a default

\cs_new_protected:Nn \marinho_lists_output:n
 {% map the sequence using the current format
  \seq_map_function:cN { g_marinho_lists_#1_seq } \__marinho_lists_item:n
 }
\cs_new:Nn \__marinho_lists_item:n
 {% \__marinho_lists_item:n receives an argument in the form {<number>}{<text>}
  % which we pass to \__marinho_lists_output_current:nn as two arguments
  \__marinho_lists_output_current:nn #1
 }

\ExplSyntaxOff
\newcommand{\key}[1]{\adddtolist[keylist]{#1}}
\newcommand{\showkey}{\outputlist{keylist}}

\usepackage{changepage}
\begin{document}
\pagestyle{empty}

1. Here's my first question.
\key{As you see, it appears on the list by the end.}

2. Here's my second question.
\key{Again, it appears on the list os keys.}

\begin{enumerate}
\item However, if I try to put the command inside this environment
\key{It doesn't appear on the list.}
\end{enumerate}

\listoutputformat{\textbf{Question #1}: #2\par}
\showkey

\end{document}
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.