Search code examples
latex

How to get a Solution environment to break across pages in LaTeX


For typesetting math homework I've created (simple) custom environments for problems and solutions.

The code I uses is

\newenvironment{question}{
\textbf{Problem \arabic{question}.} 
    \begin{center} 
        \begin{minipage}[t]{.9\textwidth}} {\end{minipage} 
    \end{center} 
\vspace{12pt} \stepcounter{question}}

and

\newlength{\currentparskip}
\newenvironment{solution} {
\setlength{\currentparskip}{\parskip}
    \begin{center} 
        \begin{minipage}[t]{.9\textwidth}
            \setlength{\parskip}{\currentparskip}
            \textbf{Solution:} \\[12pt] } 
        {\end{minipage} 
    \end{center} 
\vspace{24pt} }

It works well when the problems and solutions are fairly short, but since I achieve the positioning I want with a minipages it does not break across pages.

This is the general spacing effect I want.

To date the only problem I've had is solutions being too long so that I would get the problem on one page with a bunch of white space and then the solution on its own page. This has been annoying, but I've ignored it.

Now I'm having to address the issue because one of my solutions is so long it doesn't fit on its own page, like this

I (think I) know that my issue is that I'm using minipages, but I'm not sure what else to use in order have different margins for my solutions. What structure should I be using instead?

Minimal working example:

\documentclass[12pt]{article}
\usepackage{parskip}
\usepackage{lipsum}

\newcounter{question}
\setcounter{question}{1}

\newlength{\currentparskip}

\newenvironment{question}{
\setlength{\currentparskip}{\parskip}
\textbf{Problem \arabic{question}.} \begin{center} \begin{minipage}[t]{.9\textwidth}} {\end{minipage} \end{center} \vspace{12pt} \stepcounter{question}}



\newenvironment{solution} {
\setlength{\currentparskip}{\parskip}
\begin{center} \begin{minipage}[t]{.9\textwidth}
\setlength{\parskip}{\currentparskip}
\textbf{Solution:} \\[12pt] } {\end{minipage} \end{center} \vspace{24pt} }


\setlength{\parskip}{24pt}

\begin{document}



\begin{question}
A question with an answer too long to fit on the same page as the question is on. 

\lipsum[100]
\end{question}

\begin{solution}
\lipsum[100]
\lipsum[100]
\lipsum[100]
\end{solution}

\begin{question}
A question with too long of a solution even for its own page.
\end{question}
\begin{solution}
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\end{solution}

\end{document}

Solution

  • One possibility using the tcolorbox package

    \documentclass[12pt]{article}
    \usepackage{parskip}
    \usepackage{lipsum}
    
    \usepackage[most]{tcolorbox}
    
    \tcbsetforeverylayer{
      fonttitle=\bfseries,
      left=.1\textwidth,
      right=.1\textwidth,
      breakable,
      enhanced,
      interior hidden,
      frame hidden,
      coltitle=black,
    }
    
    \newtcolorbox[auto counter]{question}{
      title={Problem~\thetcbcounter.},
      attach boxed title to top left={xshift=-3mm},
    }
    
    \newtcolorbox{solution}{title={Solution:}}
    
    \begin{document}
    
    A question with an answer too long to fit on the same page as the question is on. 
    
    \begin{question}
    A question with an answer too long to fit on the same page as the question is on. 
    
    \lipsum[100]
    \end{question}
    
    \begin{solution}
    \lipsum[100]
    \lipsum[100]
    \lipsum[100]
    \end{solution}
    
    \begin{question}
    A question with too long of a solution even for its own page.
    \end{question}
    \begin{solution}
    \lipsum[100]
    \lipsum[100]
    \lipsum[100]
    \lipsum[100]
    \lipsum[100]
    \lipsum[100]
    \end{solution}
    
    \end{document}
    

    enter image description here