1

Is it possible to create environments or something similar to get the behavior:

Example input

\section
\begin{problem}
    problem 1
    \begin{solution}{section 1}
       solution 1
    \end{solution}
\end{problem}

\begin{problem}
    problem 2
    \begin{solution}{section 1}
       solution 2
    \end{solution}
\end{problem}

\begin{problem}
    problem 3
    \begin{solution}{section 2}
       solution 3
    \end{solution}
\end{problem}

Desired output:

 problem 1
 problem 2
 problem 3

 section 1
    solution 1
    solution 2
 section 2
    solution 3
2

I can think to something like this:

\documentclass{article}
\usepackage{xparse}
\usepackage{amsthm}

\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem*{printsolution}{Problem \problemnumber}

\ExplSyntaxOn

\NewDocumentEnvironment{solution}{+b}
 {
  \angelos_solutions_grab:n { #1 }
 }
 {}

\NewDocumentCommand{\printsolutions}{}
 {
  \angelos_solutions_print:
 }

\seq_new:N \g_angelos_solutions_seq
\int_new:N \l_angelos_solutions_section_int

\cs_new_protected:Nn \angelos_solutions_grab:n
 {
  \seq_gput_right:Nx \g_angelos_solutions_seq
   {
    \angelos_solutions_item:nnn
     { \int_to_arabic:n { \value{problem} } }
     { \int_to_arabic:n { \value{section} } }
     { \exp_not:n { #1 } }
   }
 }

\cs_new_protected:Nn \angelos_solutions_item:nnn
 {
  \int_compare:nF { #2 = \l_angelos_solutions_section_int }
   {
    \subsection*{Section~#2}
    \int_set:Nn \l_angelos_solutions_section_int { #2 }
   }
  \cs_set:Npn \problemnumber { #1 }
  \begin{printsolution}
  #3
  \end{printsolution}
 }

\cs_new_protected:Nn \angelos_solutions_print:
 {
  \section*{Solutions}
  \seq_map_function:NN \g_angelos_solutions_seq \use:n
 }

\ExplSyntaxOff

\begin{document}

\section{First}

\begin{problem}
First problem in the first section
\begin{solution}
Solution to the first problem in the first section
\end{solution}
\end{problem}

\begin{problem}
Second problem in the first section
\begin{solution}
Solution to the second problem in the first section
\end{solution}
\end{problem}

\section{Second}

This section has no problems

\section{Third}

\begin{problem}
First problem in the third section
\begin{solution}
Solution to the first problem in the third section
\end{solution}
\end{problem}

\printsolutions

\end{document}

The solution is stored in a sequence, together with the current problem number and the current section number.

The \printsolutions command delivers the contents, issuing when necessary a \subsection* command.

enter image description here

You can use nameref if you want to repeat the section title.

\documentclass{article}
\usepackage{xparse}
\usepackage{amsthm}
\usepackage{nameref}

\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem*{printsolution}{Problem \problemnumber}

\ExplSyntaxOn

\cs_set_eq:NN \angelos_section: \section
\cs_new_protected:Nn \angelos_nameref:n { \nameref { #1 } }

\RenewDocumentCommand{\section}{sO{#3}m}
 {
  \IfBooleanTF { #1 }
   {
    \angelos_section: * { #3 }
   }
   {
    \angelos_section: [#2]{#3} \label{angelosautolabel\int_to_roman:n { \value{section} } }
   }
 }

\NewDocumentEnvironment{solution}{+b}
 {
  \angelos_solutions_grab:n { #1 }
 }
 {}

\NewDocumentCommand{\printsolutions}{}
 {
  \angelos_solutions_print:
 }

\seq_new:N \g_angelos_solutions_seq
\int_new:N \l_angelos_solutions_section_int

\cs_new_protected:Nn \angelos_solutions_grab:n
 {
  \seq_gput_right:Nx \g_angelos_solutions_seq
   {
    \angelos_solutions_item:nnnn
     { \int_to_arabic:n { \value{problem} } }
     { \int_to_arabic:n { \value{section} } }
     { \exp_not:N \protect \exp_not:N \nameref {angelosautolabel\int_to_roman:n { \value{section} } } }
     { \exp_not:n { #1 } }
   }
 }

\cs_new_protected:Nn \angelos_solutions_item:nnnn
 {
  \int_compare:nF { #2 = \l_angelos_solutions_section_int }
   {
    \subsection*{Section~#2\ --\ #3}
    \int_set:Nn \l_angelos_solutions_section_int { #2 }
   }
  \cs_set:Npn \problemnumber { #1 }
  \begin{printsolution}
  #4
  \end{printsolution}
 }

\cs_new_protected:Nn \angelos_solutions_print:
 {
  \section*{Solutions}
  \seq_use:Nn \g_angelos_solutions_seq {}
 }

\ExplSyntaxOff

\begin{document}

\section{First}

\begin{problem}
First problem in the first section
\begin{solution}
Solution to the first problem in the first section
\end{solution}
\end{problem}

\begin{problem}
Second problem in the first section
\begin{solution}
Solution to the second problem in the first section
\end{solution}
\end{problem}

\section{Second}

This section has no problems

\section{Third}

\begin{problem}
First problem in the third section
\begin{solution}
Solution to the first problem in the third section
\end{solution}
\end{problem}

\printsolutions

\end{document}

enter image description here

8
  • Thank you! Does compiling your code require anything special? I failed compiling it using pdflatex. – Angelos Apr 29 '20 at 12:36
  • @Angelos This requires an up-to-date TeX distribution. – egreg Apr 29 '20 at 14:37
  • I have run TeX Live Utility and updated all packages but I still get the error "LaTeX3 Error: Unknown argument type 'b' for the command '\environment(LaTeX3) solution'. {}". Do I have to do another type of upgrade? – Angelos May 9 '20 at 13:16
  • @Angelos If you get that error, it means your system is not up-to-date. What OS are you using? – egreg May 9 '20 at 13:22
  • I understand. I am using Mac. – Angelos May 9 '20 at 13:24

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.