我正在尝试编写一些新的环境和命令,以使用枚举环境从列表中保存简短的答案,其想法是使用对存储在内存中的列表的引用来保存第一级和第二级。
我正在使用enumitem
、、multicol
和pgffor
包。tcolorbox
xparse
我解释说,我创建了两个新环境,每个环境里面都有一个定义的命令,以便能够保存简短的答案,并且我向 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}}%
我的困境就从这里开始。如果我使用 enumitem 键,它会丢失最后一个元素中列表的对齐,并影响所有枚举环境(无论是否启用了该键)
如果我注释掉 enumitem 键并将枚举环境封装在“saveanswer”中,我创建的枚举环境可以部分解决问题,但它仍然会影响其他枚举环境。
这两种情况都会失败,此外,我无法运行 foreach 循环来将项目正确保存在列表中。我希望它看起来像这样:
我有点迷茫,我读过NewDocumentEnvironment 与枚举之间的冲突也许我得到的答案并不是正确的,应该用不同的方式来实现。问候: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}
答案1
我正在应用一种不同的策略,通过自动降低和提高级别并将和enumerate
存储到一个\begin{enumerate}
\end{enumerate}
单身的列表,使用全局变量检索当前列表 id \prop
。
% !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}