自动排列同类型定理环境

自动排列同类型定理环境

考虑以下:

\documentclass{article}
\newtheorem{exe}{Exercise}
\newtheorem{sol}{Solution}

\begin{document}

    \begin{exe}
        \dots 
    \end{exe}

    \begin{sol}
        here the solution for Exercise 1
    \end{sol}   

    \begin{exe}
        \dots 
    \end{exe}

    \begin{sol}
        here the solution for Exercise 2
    \end{sol}

\end{document}

有没有一种方法(开关)可以自动按类型重新排序定理环境,而无需手动重新排序,这意味着获得与

\documentclass{article}
\newtheorem{exe}{Exercise}
\newtheorem{sol}{Solution}

\begin{document}

    \begin{exe}
        \dots 
    \end{exe}   

    \begin{exe}
        \dots 
    \end{exe}

    \begin{sol}
        here the solution for Exercise 1
    \end{sol}

    \begin{sol}
        here the solution for Exercise 2
    \end{sol}

\end{document}

更新:

实际上我需要一个“开关”来解决这个问题:

\documentclass{article}
\newtheorem{exe}{Exercise}
\newtheorem{sol}{Solution}

\begin{document}

    \begin{exe}
        \dots 
    \end{exe}

    \bigskip

    \begin{sol}
        here the solution for Exercise 1
    \end{sol}   

    \bigskip

    \begin{exe}
        \dots 
    \end{exe}

    \bigskip

    \begin{sol}
        here the solution for Exercise 2
    \end{sol}

\end{document}

这样它的输出就和

\documentclass{article}
\newtheorem{exe}{Exercise}
\newtheorem{sol}{Solution}

\begin{document}

    \begin{exe}
        \dots 
    \end{exe}   

    \bigskip

    \begin{exe}
        \dots 
    \end{exe}

    \bigskip

    \begin{sol}
        here the solution for Exercise 1
    \end{sol}

    \bigskip

    \begin{sol}
        here the solution for Exercise 2
    \end{sol}

\end{document}

答案1

您可以收集解决方案的文本并在某个点打印它们。

这可以通过 来实现environ

\documentclass{article}
\usepackage{environ}

\newtheorem{exe}{Exercise}
\newtheorem{printsol}{Solution}

\NewEnviron{sol}{%
  \xdef\solutionsuptonow{%
    \unexpanded\expandafter{\solutionsuptonow}%
    \noexpand\begin{printsol}%
    \unexpanded\expandafter{\BODY}%
    \noexpand\end{printsol}%
  }%
}
\newcommand{\solutionsuptonow}{}
\newcommand{\printsolutions}{\solutionsuptonow\gdef\solutionsuptonows{}}

\begin{document}

\begin{exe}
\dots 
\end{exe}

\begin{sol}
here the solution for Exercise 1
\end{sol}   

\begin{exe}
\dots 
\end{exe}

\begin{sol}
here the solution for Exercise 2
\end{sol}

\printsolutions

\end{document}

在此处输入图片描述

不同的编码在练习和解决方案之间添加了空格。

\documentclass{article}
\usepackage{environ,xparse}

\newtheorem{printexe}{Exercise}
\newtheorem{printsol}{Solution}

\ExplSyntaxOn
\NewEnviron{exe}
 {
  \thrash_add:NnV \g_thrash_exercises_seq {exe} \BODY
 }
\NewEnviron{sol}
 {
  \thrash_add:NnV \g_thrash_solutions_seq {sol} \BODY
 }

\NewDocumentCommand{\printexercises}{}
 {
  \seq_use:Nn \g_thrash_exercises_seq { \bigskip }
  \bigskip
  \seq_use:Nn \g_thrash_solutions_seq { \bigskip }
  \seq_gclear:N \g_thrash_exercisess_seq
  \seq_gclear:N \g_thrash_solutions_seq
 }

\seq_new:N \g_thrash_exercises_seq
\seq_new:N \g_thrash_solutions_seq

\cs_new_protected:Nn \thrash_add:Nnn
 {
  \seq_gput_right:Nn #1 { \begin{print#2}#3\end{print#2} }
 }
\cs_generate_variant:Nn \thrash_add:Nnn { NnV }

\ExplSyntaxOff

\begin{document}

\begin{exe}
\dots 
\end{exe}

\begin{sol}
here the solution for Exercise 1
\end{sol}   

\begin{exe}
\dots 
\end{exe}

\begin{sol}
here the solution for Exercise 2
\end{sol}

\printexercises

\end{document}

在此处输入图片描述

相关内容