Estoy intentando escribir un par de entornos y comandos nuevos para guardar respuestas breves de una lista con un entorno enumerado, la idea es guardar el primer y segundo nivel usando referencias a listas almacenadas en la memoria.
Estoy usando los paquetes enumitem
, multicol
, y pgffor
.tcolorbox
xparse
Le expliqué, creé dos entornos nuevos cada uno con un comando definido dentro de ellos para poder guardar las respuestas cortas y agregué un par de claves a 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}}%
Aquí comienza mi dilema. Si uso las claves enumitem, se pierde la alineación de las listas en el último elemento y afecta a todos los entornos de enumeración (con o sin la clave habilitada)
Si comento las claves de enumitem y encapsulo los entornos de enumeración dentro de "saveanswer", los que he creado resuelven parcialmente el problema, pero aún afecta a los otros entornos de enumeración.
En ambos casos falla, además, no puedo ejecutar el bucle foreach para mantener los elementos correctamente en la lista. Me gustaría que se viera así:
Estoy un poco perdido en esto, leí.Conflicto entre NewDocumentEnvironment y enumerary tal vez la forma de conseguir lo que busco no es la adecuada y se debe hacer de otra manera. Saludos: El 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}
Respuesta1
Estoy aplicando una estrategia diferente, bajando y subiendo enumerate
niveles automáticamente y almacenando el \begin{enumerate}
y \end{enumerate}
en uno.solterolist, recuperando la identificación de la lista actual con \prop
la variable 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}