[Tex/LaTex] Minipages across page breaks

minipagepage-breaking

I have defined a command that uses the minipage environment to format an "example" question within my document.

\newcounter{Example}
\newcommand{\example}[3]{%
    \refstepcounter{Example}
    \fbox{%
        \begin{minipage}{\textwidth}
            \subsubsection{Example \theExample:\textsf{\ #1}}
                {\leftskip 1em \rightskip 1em 
                    #2

                    }

                \vskip 0.5em

                \colorbox{lightergray}{%
                    \begin{minipage}[c]{0.98\textwidth}
                        #3
                    \end{minipage}%
                    }
            %end subsubsection{Example}     
        \end{minipage}
        }%
    }

(This code creates a command \example{<title>}{<question>}{<answer>}).

To produce this:

Current output of above code

However I need this to be able to split across multiple page breaks. At the moment the following is produced at a page break:

Undesired page splitting

This is not desirable. I know this is because of the minipage environment. Is there a way that I can produce this same output whilst allowing page breaks? (Ideally I would like no border at the bottom of the first page and no top border on the new page.)

I am compiling with TeX4ht so require either common packages or standard LaTeX/TeX.

I understand a type of list environment may be useful here but I'm unsure how to apply this to this specific case.

Best Answer

Alternative: Use tcolorbox (Gonzalo Medina was proposing it while I wrote this answer ;-)) with the breakable option. There is a huge number of options for the shape, colours etc -- it's impossible to show them all here in a small example.

\documentclass{article}

\PassOptionsToPackage{svgnames}{xcolor}
\usepackage[most]{tcolorbox}

\usepackage{blindtext}

\newtcbtheorem[auto counter]{example}{My Example}{arc=0pt, auto outer arc, breakable,left=2pt,right=2pt,colback=LightBlue,colbacktitle=LightBlue,coltitle=black,title after break={Continued from Example \thetcbcounter},titlerule=0pt}{examples}


\begin{document}

In \ref{examples:brontosaurs}, we will see that...


\begin{example}{Theory on Brontosaurs}{brontosaurs}
  \blindtext[20]
\end{example}


\end{document}

enter image description here

Related Question