我如何使这个 xparse 命令在环境中工作?

我如何使这个 xparse 命令在环境中工作?

我有一个LaTeX3e为学生创建自动键练习列表的代码。我需要创建一个具有以下结构的练习列表(它是葡萄牙语的,但不要介意内容):

在此处输入图片描述

正如所注意到的,问题是通过枚举环境缩进的,其中数字停留在与实际问题有一定距离的不同“列”中。

问题是,由于某种原因,收集答案的命令在枚举环境中不起作用。

以下是我的问题的 MWE:

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

changepage我尝试过的一个解决方案是使用环境通过包缩进文档文本adjustwidth,但它也不起作用。

对于解决方案您有什么想法吗?我该如何按照描述中的编号列出我的问题?

答案1

首先也是最重要的一点,没有 LaTeX3ε(至少在可预见的未来没有)。LaTeX2ε 最初被认为是从 LaTeX 2.09 向 LaTeX3 迈出的一步,因此有了这个名字(参见这里),而 LaTeX3 尚未作为一种格式发布(它是预计 3019)。由于我们尚未计划 LaTeX4,因此我们没有 LaTeX3ε ;-)


玩笑归玩笑,您的代码无法正常工作,因为 LaTeX 环境形成了一个 TeX 组,因此当环境结束时,非全局分配将丢失。使用而不是(并将变量重命名为)解决了该问题:\seq_gput_right:cx\seq_put_right:cxseqg_...

在此处输入图片描述

除此之外,代码很棒!

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

相关内容