title: Breaking a LaTeX environment across pages - the smart way
body: <p>I am using the <a href="http://www.ctan.org/tex-archive/macros/latex/contrib/exercise/" rel="nofollow noreferrer">exercise</a> package to display exercises in a book. I have redefined some commands like this, which basically adds some space, a pencil, and two hrule's before and after the exercise:</p>
  
  <pre><code>\renewcommand{\ExerciseHeader}{\vskip 1em\hrule\vskip 1em\centerline{\textbf{\large\smallpencil
  \ExerciseHeaderNB\ExerciseHeaderTitle%
  \ExerciseHeaderDifficulty\ExerciseHeaderOrigin\medskip}}}
  \makeatletter\def\endExerciseEnv{\termineliste{1}\@EndExeBox\vskip .5em\hrule\vskip 1em}\makeatother
  </code></pre>
  
  <p>Now this works, but there's a small problem:</p>
  
  <p>There are situations where only the \hrule ends up being at the bottom of a page, and the rest of the exercise goes on the next page. There is also the opposite behavior: the entire exercise is on one page, except the \hrule in "endExerciseEnv", which is flushed to the next page.</p>
  
  <p>How do I force the top hrule come?</p>
  
  <ul>
  <li>either together with the header of the exercise (caption, title, whatever not) and at least say 5em or so of "exercise text" (so it doesn't look ugly),</li>
  <li>OR be flushed altogether, with the entire exercise?</li>
  </ul>
  
  <p>A similar question for the bottom hrule:</p>
  
  <p>How do I force it have at least 5 em of "exercise text" in front of it on the visible page where the hrule itself goes to? (or the beginning of the Exercise environment, whichever comes first - as it's the case if the environment is not higher than 5em)</p>
  
  <p><strong>Addendum</strong></p>
  
  <p>I have asked in the past LaTeX questions like this and I've got answers which required me to do stuff manually, like "insert a \vskip here and there" or such.</p>
  
  <p>Let me be clear: This is a book, there are a lot of exercises, and I <em>need</em> it be done "automatically", by going the proper way of redeclaring commands &amp; co.</p>