如何修復 NewDocumentEnvironment 和 enumitem 套件之間的干擾

如何修復 NewDocumentEnvironment 和 enumitem 套件之間的干擾

我正在嘗試編寫幾個新的環境和命令來保存具有枚舉環境的列表中的簡短答案,其想法是使用對內存中存儲的列表的引用來保存第一級和第二級。

我正在使用enumitemmulticolpgffortcolorbox套件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」中,我創建的環境可以部分解決問題,但它仍然會影響其他枚舉環境。 在 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}單身的list,使用全域變數檢索目前列表 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}

在此輸入影像描述

在此輸入影像描述

相關內容