I'm trying to write a couple of new environments and commands to save short answers from a list with enumerate environment, the idea being to save the first and second level using references to lists stored in memory.
I am using the enumitem
, multicol
, pgffor
, tcolorbox
and xparse
packages.
I explained, I created two new environments each with a defined command inside them to be able to save the short answers and I added a pair of keys to 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}}%
Here begins my dilemma. If I use the enumitem keys, it loses the alignment of the lists in the last element and affects all enumerate environments (with or without the enabled key)
If I comment the enumitem keys and encapsulate the enumerate environments within "saveanswer" the ones I have created partially solve the issue, but it still affects the other enumerate environments.
With both cases it fails, in addition, I can not run the foreach loop to keep the items correctly in the list. I would like it to look like this:
I'm a little lost in this, i read Conflict between NewDocumentEnvironment and enumerate and maybe the way to get what I'm looking for is not the right one and it should be done differently. Regards: The 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}
\setenumerate
is deprecated. You should use\setlist[enumerate,1]
, i.e. setting the keys for the individualenumerate
levels here, which is most likely the cause for your problem -- they are mixed\themyNo
-- it is not expanded and therefore the lists are mixed\edef\tmp
in\answii
?\edef\tmp
would be sufficient (and not possible there because of the assignments with\seq_gput_right:Nnn
, most likely)\keych
macro change try to change\seq_gput_right:cn { l_bcp_data_ \retrievecurrentid _seq }{\item \Alph{enumii}}
to\seq_gput_right:cx { l_bcp_data_ \retrievecurrentid _seq }{\protect\item \Alph{enumii}}