2

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

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}

enter image description here

A different coding that adds spaces between exercises and solutions.

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

enter image description here

| improve this answer | |
  • 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
  • 1
    @Thrash Added the code – egreg Dec 6 '18 at 22:50
  • 1
    @Thrash Changed the coding. – egreg Dec 6 '18 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.