Como corrigir interferência entre NewDocumentEnvironment e pacote enumitem

Como corrigir interferência entre NewDocumentEnvironment e pacote enumitem

Estou tentando escrever alguns novos ambientes e comandos para salvar respostas curtas de uma lista com ambiente enumerado, a ideia é salvar o primeiro e o segundo nível usando referências a listas armazenadas na memória.

Estou usando os pacotes enumitem, multicol, pgffore tcolorbox.xparse

Expliquei, criei dois novos ambientes cada um com um comando definido dentro deles para poder salvar as respostas curtas e adicionei um par de chaves ao enumitem.

\NewDocumentEnvironment{saveanswer} whit \answi
\NewDocumentEnvironment{saveansweri} whit \answii
\SetEnumitemKey{columns}{before=\begin{multicols}{#1},after=\end{multicols}}%
\SetEnumitemKey{savekey}{before=\begin{saveanswer}{#1},after=\end{saveanswer}}%
\SetEnumitemKey{ansref}{before=\begin{saveansweri}{#1},after=\end{saveansweri}}%

Aqui começa meu dilema. Se eu usar as chaves enumitem, ele perde o alinhamento das listas no último elemento e afeta todos os ambientes enumerados (com ou sem a chave habilitada) usando a chave do enumitem

Se eu comentar as chaves enumitem e encapsular os ambientes enumerados em "saveanswer", os que criei resolvem parcialmente o problema, mas ainda afeta os outros ambientes enumerados. enumerar em saveanswer

Em ambos os casos ele falha, além disso, não consigo executar o loop foreach para manter os itens corretamente na lista. Eu gostaria que ficasse assim: IDEAL

Estou um pouco perdido nisso, eu liConflito entre NewDocumentEnvironment e enumerare talvez a maneira de conseguir o que procuro não seja a certa e deva ser feita de forma diferente. Atenciosamente: O MWE:

% !file: forum.tex
% arara: pdflatex
% arara: clean: { files:[forum.aux, forum.log] }
\documentclass{article}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{pgffor}
\usepackage{tcolorbox}
\tcbuselibrary{many}
\pagestyle{empty}

% Set key for multicols in enumitem
\SetEnumitemKey{columns}{before=\begin{multicols}{#1},after=\end{multicols}}%
\SetEnumitemKey{savekey}{before=\begin{saveanswer}{#1},after=\end{saveanswer}}%
\SetEnumitemKey{ansref}{before=\begin{saveansweri}{#1},after=\end{saveansweri}}%

% Some definition
\newtheorem{theorem}{Theorem}
\newtheorem{exercise}[theorem]{Exercise}

% Adapted from https://tex.stackexchange.com/a/364763/7832
\ExplSyntaxOn
% Reporter macro, is expandable
\cs_new:Npn \reportnumberofseqitems #1{%
 \seq_count:c {l_bcp_data_#1_seq}%
}
\NewDocumentCommand{\storedata}{mm}
  {
   \bcp_store_data:nn { #1 } { #2 }
  }

\NewDocumentCommand{\appenddata}{+m+m}
 {
  \bcp_append_data:nn { #1 } { #2 }
 }

\DeclareExpandableDocumentCommand{\getdata}{O{1}m}
 {
  \bcp_get_data:nn { #1 } { #2 }
 }

\cs_new_protected:Npn \bcp_store_data:nn #1 #2
 {
  % create the sequence if it doesn't exist or clear it if it exists
  \seq_if_exist:cTF { l_bcp_data_#1_seq }
   { \seq_new:c { l_bcp_data_#1_seq } }
   { \seq_gclear:c { l_bcp_data_#1_seq } }
  % append the items
  \__bcp_append_data:nn { #1 } { #2 }
 }

\cs_new_protected:Npn \bcp_append_data:nn #1 #2
 {
  % create the sequence if it doesn't exist, do nothing if it exists
  \seq_if_exist:cF { l_bcp_data_#1_seq }
   { \seq_new:c { l_bcp_data_#1_seq } }
  % append the items
  \__bcp_append_data:nn { #1 } { #2 }
 }

\cs_new_protected:Npn \__bcp_append_data:nn #1 #2
 {
  % append items one at a time
  \tl_map_inline:nn { #2 }
   {
    \seq_gput_right:cn { l_bcp_data_#1_seq } { ##1 }
   }
 }

\cs_new:Npn \bcp_get_data:nn #1 #2
 {
  % retrieve the requested item
  \seq_item:cn { l_bcp_data_#2_seq } { #1 }
 }
% Save answer in first level answi 
\NewDocumentEnvironment{saveanswer}{m}{%
\NewDocumentCommand\answi{m}{%,
    \appenddata{#1}{{##1}}%
    }% close \answi
}{%
}% close saveanswer

% Save answer in second level answii 
\newcounter{myNo}
\NewDocumentEnvironment{saveansweri}{m}{%
\stepcounter{myNo}
\NewDocumentCommand\answii{m}{%,
    \appenddata{#1:\themyNo}{{##1}}%
    }% close \answii
}{%
\appenddata{#1}{{\begin{enumerate}\space%
\foreach \x in {1,...,\reportnumberofseqitems{#1:\themyNo}}{%
    \item \space \getdata[\x]{#1:\themyNo} \space%
                } % close foreach
    \space \end{enumerate}}} % close appendata
}% close saveansweri

\ExplSyntaxOff

% [#1] : pass to tcolorbox
% {#2} : title
% {#3} : list ref
% [#4] : columns
\DeclareTotalTColorBox{\showans}{O{} m m O{4}}
{ colback=white,size=small,top=0mm,bottom=1.5mm, left=0mm,width=\columnwidth,title filled,%
  fontupper=\small,fonttitle=\small\sffamily,%
    adjusted title={#2},center title,#1}
  {% 
  \begin{enumerate}[columns=#4,leftmargin=15pt,labelsep=3pt,font=\footnotesize,nosep,widest=25,]%
  \small
  \setlength{\columnsep}{0pt} 
   \foreach \x in {1,...,\reportnumberofseqitems{#3}} {
    \item \getdata[\x]{#3}%
    }
  \end{enumerate}
  }%
\begin{document}

\section{It looks like this}
\subsection{Exercices}
\begin{enumerate}[savekey=test1]
\setenumerate{labelsep=5pt, leftmargin=1cm, itemsep=0pt,widest=80}%
\item True False
      \begin{enumerate}[ansref=test1] 
      \item $2\alpha+2\delta=90^{\circ}$ \answii{True}
      \item $\angle EDF=45^{\circ}$ \answii{False}
      \end{enumerate} 
\item Factor $x^{2}-2x+1$  \answi{$\left(x-1\right)^{2}$} 
\item Factor$3x+3y+3z$    \answi{$3(x+y+z)$}
\item True False
      \begin{enumerate}[ansref=test1]
      \item $2\alpha+2\delta=90^{\circ}$ \answii{False}
      \item $\alpha=\delta$ \answii{True}
      \item $\alpha=\delta$ \answii{True}
      \end{enumerate} 
\end{enumerate}
\subsection{answers}
\showans{Exercices 1}{test1}[2]
\end{document}

Responder1

Estou aplicando uma estratégia diferente, descendo e subindo automaticamente os enumerateníveis e armazenando o \begin{enumerate}e \end{enumerate}em umsolteirolist, recuperando o ID da lista atual com \propvariável global.

% !file: forum.tex
% arara: pdflatex
% arara: clean: { files:[forum.aux, forum.log] }
\documentclass{article}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{pgffor}
\usepackage[many]{tcolorbox}
\pagestyle{empty}

% Set key for multicols in enumitem
\SetEnumitemKey{columns}{before=\begin{multicols}{#1},after=\end{multicols}}%
\SetEnumitemKey{savekey}{before={\storecurrentid{#1}\begin{saveanswer}{#1}},after=\end{saveanswer}}%

% Some definition
\newtheorem{theorem}{Theorem}
\newtheorem{exercise}[theorem]{Exercise}

% Adapted from https://tex.stackexchange.com/a/364763/7832
\ExplSyntaxOn

\prop_new:N \g_bpc_admin_prop 

\cs_new:Npn \storecurrentid #1{%
  \prop_gput:Nnn \g_bpc_admin_prop {current-id} {#1}
}

\cs_new:Npn \retrievecurrentid {%
  \prop_item:Nn \g_bpc_admin_prop {current-id} 
}


\NewDocumentEnvironment{saveanswer}{m}{%
  \setlist[enumerate,1]{labelsep=5pt, leftmargin=1cm, itemsep=0pt,widest=80}%
  \setlist[enumerate,2]{before={\stepdownlevel},after={\stepuplevel},labelsep=5pt, leftmargin=0.6cm, itemsep=0pt,widest=80}
  \seq_if_exist:cF { l_bcp_data_#1_seq }{%
    \seq_new:c { l_bcp_data_#1_seq }
  }
  \leavevmode
}{%
%
}% close saveanswer

\NewDocumentCommand{\answeris}{+m}{%
  \seq_gput_right:cn { l_bcp_data_ \retrievecurrentid _seq }{\item  #1}
  \par
}

\newcommand{\stepdownlevel}{%
  % Must start a new item with \begin{enumerate} on answer ... 
  \seq_gput_right:cn {l_bcp_data_ \retrievecurrentid _seq} {\item \begin{enumerate}}
  }

\newcommand{\stepuplevel}{%
  % Close current enumerate level
  \seq_gput_right:cn {l_bcp_data_ \retrievecurrentid _seq} {\end{enumerate}}
}

\cs_new:Npn \displayseqcontent #1#2 {%
  \seq_if_empty:cF { l_bcp_data_#1_seq } {%
    \seq_log:c { l_bcp_data_#1_seq }
    \setlist[enumerate,2]{before={},after={},labelsep=5pt, leftmargin=0.6cm, itemsep=0pt,widest=80}
    \begin{enumerate}[#2]
      \small
      \setlength{\columnsep}{0pt} 
      \seq_map_inline:cn { l_bcp_data_#1_seq } {%
        ##1%
      }
    \end{enumerate}
  }
}

\ExplSyntaxOff

% [#1] : pass to tcolorbox
% {#2} : title
% {#3} : list ref
% [#4] : columns
\DeclareTotalTColorBox{\showans}{O{} m m O{4}}
{ colback=white,size=small,top=0mm,bottom=1.5mm, left=0mm,width=\columnwidth,title filled,%
  fontupper=\small,fonttitle=\small\sffamily,%
  adjusted title={#2},center title,#1}
{% 
  \displayseqcontent{#3}{columns=#4,leftmargin=15pt,labelsep=3pt,font=\footnotesize,nosep,widest=25}
}%


% Set the list properties for level 1 and 2
%\setlist[enumerate,1]{labelsep=5pt, leftmargin=1cm, itemsep=0pt,widest=80}%
%\setlist[enumerate,2]{before={\stepdownlevel},after={\typeout{stepping up for \retrievecurrentid}\stepuplevel},labelsep=5pt, leftmargin=0.6cm, itemsep=0pt,widest=80}%

\begin{document}
\section{It looks like this}
\subsection{Exercices}
\begin{enumerate}[savekey=test1]
\item True False
  \begin{enumerate}
  \item $2\alpha+2\delta=90^{\circ}$ \answeris{True}
  \item $\angle EDF=45^{\circ}$ \answeris{False}
  \end{enumerate} 
\item Factor $x^{2}-2x+1$  \answeris{$\left(x-1\right)^{2}$} 
\item Factor $3x+3y+3z$    \answeris{$3(x+y+z)$}
\item True False
  \begin{enumerate}
  \item $2\alpha+2\delta=90^{\circ}$ \answeris{False}
  \item $\alpha=\delta$ \answeris{True}
  \item $\alpha > \delta$ \answeris{False}
  \item \LaTeX2e\ is cool? \answeris{Very True!}
  \end{enumerate} 
\end{enumerate}


\begin{enumerate}[savekey=test2]
\item True False 
  \begin{enumerate}
  \item $2\alpha+2\delta=90^{\circ}$ \answeris{False}
  \item $\alpha=\delta$ \answeris{True}
  \item $\alpha > \delta$ \answeris{False}
  \item \LaTeX2e\ is cool? \answeris{Very True!}
  \end{enumerate} 

\item True False 

  \begin{enumerate}
  \item $2\alpha+2\delta=90^{\circ}$ \answeris{True}
  \item $\angle EDF=45^{\circ}$ \answeris{False}
  \end{enumerate} 

\item Factor $x^{2}-2x+1$  \answeris{$\left(x-1\right)^{2}$} 

\item Factor $3x+3y+3z$    \answeris{$3(x+y+z)$}
\end{enumerate}


\subsection{answers}

\showans[colback=yellow!30!white]{Exercices 1}{test1}[2]

\showans[colback=yellow!30!white]{Exercices 2}{test2}[2]


\showans[colback=yellow!70!blue]{Exercices 1 again}{test1}[2]
\end{document}

insira a descrição da imagem aqui

insira a descrição da imagem aqui

informação relacionada