10

Let suppose that I have an environment \begin{sol} ... \end{sol} for writing solutions of exercises in a test. For practical reasons, I would like that two or more solutions can be put in the same page only if their whole contents are only in this page.

For example, if the solutions of exercises 1 and 2 are only in the page, they can be put together, but if not, the solution 1 must be alone, and then the solution 2 must starts at a new page.

The main problem is that the sizes of the solutions are not fixed, and they can be smaller or not that one page. It can have one small solution followed by another small one, or not.

The number of solutions is not fixed neither. Indeed, in reality I need less than 10 solutions.

6
  • Is every single solution shorter than one page? Commented Nov 24, 2013 at 11:30
  • That's a good question. Sometimes there are small solutions, sometimes not. I update my question.
    – projetmbc
    Commented Nov 24, 2013 at 11:33
  • Do you need any special formatting of the environment? Maybe a headline/frame etc.? I have something in mind. Commented Nov 24, 2013 at 12:03
  • Not specially. I just put a title like Exercise 1, a back return and then the text of the solution.
    – projetmbc
    Commented Nov 24, 2013 at 18:10
  • 1
    If you use \filbreak (from plain TeX) between each chunk, it should behave the way you want. See TeXbook page 111. So a minimal answer is \newenvironment{sol}{}{\filbreak} Commented Dec 4, 2013 at 13:42

3 Answers 3

4
+400

Having read the "don't put answers in comments" instructions, here is my "\filbreak" answer as an answer.

\documentclass[12pt,a5paper]{article}
\usepackage[margin=20mm]{geometry}
\usepackage{lipsum}

\newenvironment{sol}{\paragraph{Solution:}}{\filbreak\bigbreak} 

\begin{document}
\begin{sol} \lipsum[3] \end{sol}
\begin{sol} \lipsum[3] \end{sol}
\begin{sol} \lipsum[3-10] \end{sol}
\begin{sol} Very short one \end{sol}
\begin{sol} Very short one \end{sol}
\begin{sol} \lipsum[3] \end{sol}
\begin{sol} Very short one \end{sol}
\end{document}

Output

1
  • Efficient and elegant solution. Maybe one day, I will start to read the TeXbook... Thanks a lot !
    – projetmbc
    Commented Dec 4, 2013 at 19:36
3

Here is an approach that will allow solutions longer than a page. We typeset them once, and measure their height, if they will not fit on the current page then we issue a \clearpage first.

\documentclass{article}

\usepackage{etoolbox,environ}
\newsavebox{\mytmpbox}

\newcounter{solution}

\NewEnviron{sol}{\setbox\mytmpbox\vbox{\bigskip
\stepcounter{solution}\noindent{\textbf{Solution~\thesolution.}} \BODY\par}%
\ifdimgreater{\ht\mytmpbox}{\pagegoal-\pagetotal}{\clearpage}{}%
\unvbox\mytmpbox\bigbreak
}

\usepackage{lipsum}

\begin{document}

\begin{sol}
  \lipsum[1]
\end{sol}

\begin{sol}
  \lipsum[2]
\end{sol}

\begin{sol}
  \lipsum[3-10]
\end{sol}

\begin{sol}
  \lipsum[11]
  \begin{equation}
    x = y
  \end{equation}
  \lipsum[12]
\end{sol}
\end{document}

This places solutions 1 and 2 on page 1, solution 3 on the top of page 2 spreading to page 3 and solution 4 in the remaining space on page 3.

Page 1:

Page 1

Pages 2 and 3:

Pages 2 and 3

2

In the follwoing solution I "designed" a very simple look for the solution environment. Feel free to change it/customize it to your needs. However, the style is not important for the solution, which consists in wrapping the solution body into a \vbox. This will alredy do the job.

In the example "Solution 1" and "Solution 2" can stay on one page, but "Solution 3" does not fit on the same page, hence it breaks to the next one.

The Code

\documentclass{article}
\usepackage{environ}
\usepackage{lipsum}

\newcounter{sol}
  \providecommand{\solname}{Solution}
\NewEnviron{sol}{%
  \parindent=0em
  \par
  \stepcounter{sol}
    \vbox{%
    \bgroup\itshape \solname\space\thesol.\egroup
    \par
    \BODY
    }
  \par\bigskip
}

\begin{document}
\begin{sol}
  \lipsum[1]
\end{sol}
\begin{sol}
  \lipsum[2]
\end{sol}
\begin{sol}
  \lipsum[3-6]
\end{sol}
\end{document}

Output

nobreak_p1 nobreak_p2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .