# Automatic arrangement of the same type of theorem environments

Consider the following:

\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}


Is there a way (a switch) to automatically reorder the theorem environments by type without reordering them manually, that means to get the same output as

\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}


?

Update:

Actually I need a "switch" for this one:

\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}


so that it has the same output as

\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}


You can gather the text of the solutions and print them at a certain point.

This can be accomplished with 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}


A different coding that adds spaces between exercises and solutions.

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

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

\ExplSyntaxOn
\NewEnviron{exe}
{
}
\NewEnviron{sol}
{
}

\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

{
\seq_gput_right:Nn #1 { \begin{print#2}#3\end{print#2} }
}

\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}


• Thank you! Why not just \newcommand{\solutionsuptonow} instead of the two lines and in the end \solutionsuptonow instead of \printsolutions? It works, too. – Thrash Dec 6 '18 at 22:07
• @Thrash You want to clear the container of solutions up to now and not print again and again the old solutions, don't you? ;-) – egreg Dec 6 '18 at 22:09
• I don't understand your question, but I'm a LateX layman. Another question: What if I have a \bigskip between the theorem environments? – Thrash Dec 6 '18 at 22:32
• @Thrash Added the code – egreg Dec 6 '18 at 22:50
• @Thrash Changed the coding. – egreg Dec 6 '18 at 23:14