0

I am using the exercise package to write exercises in a very long document. Several times in the document the exercise header (Exercise 0.1 in the MWE below) appears at the bottom of the page. I could use \newpage to move the heading to the next page, but I might want to add more text before some of the exercises at a later date, so this is not a good option.

Is there some code I can add to ensure that the exercise header never appears alone by itself at the bottom of the page?

I have attempted to create a MWE below where this happens.

\documentclass[12pt,twoside]{article}
\usepackage{amsmath,amsthm,amssymb,amsfonts}

\usepackage[usenames,svgnames,dvipsnames,table]{xcolor}
\colorlet{lightcyan}{cyan!40!white}

\setlength{\parskip}{1em}

\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\usepackage{tasks}
\settasks{label-format={\bfseries},  column-sep={10pt}} 

\usepackage[lastexercise,answerdelayed]{exercise}
\counterwithin{Exercise}{section}
\counterwithin{Answer}{section}
\renewcounter{Exercise}[section]
\newcommand{\QuestionNB}{\fbox{\bfseries\arabic{Question}}\ }
\renewcommand{\ExerciseName}{\textbf{Exercise}}
\renewcommand{\ExerciseHeader}{\noindent\def\stackalignment{l}% code from https://tex.stackexchange.com/a/195118/101651
\stackunder[0pt]{\colorbox{cyan}{\textcolor{white}{\LARGE\ExerciseName\;\textbf{\LARGE\ExerciseHeaderNB}}}}{\textcolor{lightcyan}{\rule{\linewidth}{2pt}}}\medskip}

\setlength{\QuestionIndent}{16pt}




\begin{document}

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC

ABC


 \begin{Exercise}
 \Question Solve the following equations, giving exact answers.
  \begin{tasks}(2)
   \task $\dfrac{5}{x}=2$
   \task $\dfrac{5x+6}{3}+\dfrac{4-x}{5}=3$
  \end{tasks}
  \Question Solve, giving your answers correct to 3 significant figures.
   \begin{tasks}(2)
    \task $\dfrac{x(x+1)^2}{3(x-1)}=x^2$
    \task $\dfrac{2}{x+3}-\dfrac{1}{x-7}=1$
   \end{tasks}
   \end{Exercise}

\end{document}

1 Answers1

1

Here is a needspace solution.

\documentclass[12pt,twoside]{article}
\usepackage{amsmath,amsthm,amssymb,amsfonts}

\usepackage[usenames,svgnames,dvipsnames,table]{xcolor}
\colorlet{lightcyan}{cyan!40!white}

\setlength{\parskip}{1em}

\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\usepackage{tasks}
\settasks{label-format={\bfseries},  column-sep={10pt}} 

\usepackage[lastexercise,answerdelayed]{exercise}
\counterwithin{Exercise}{section}
\counterwithin{Answer}{section}
\renewcounter{Exercise}[section]
\newcommand{\QuestionNB}{\fbox{\bfseries\arabic{Question}}\ }
\renewcommand{\ExerciseName}{\textbf{Exercise}}
\renewcommand{\ExerciseHeader}{\noindent\def\stackalignment{l}% code from https://tex.stackexchange.com/a/195118/101651
\stackunder[0pt]{\colorbox{cyan}{\textcolor{white}{\LARGE\ExerciseName\;\textbf{\LARGE\ExerciseHeaderNB}}}}{\textcolor{lightcyan}{\rule{\linewidth}{2pt}}}\medskip}

\setlength{\QuestionIndent}{16pt}

\usepackage{stackengine}
\usepackage{needspace}

\begin{document}

\noindent\rule{\textwidth}{32\baselineskip}% 31\baselineskip will include part of the exercise

\needspace{2\baselineskip}% could be bigger
 \begin{Exercise}
 \Question Solve the following equations, giving exact answers.
  \begin{tasks}(2)
   \task $\dfrac{5}{x}=2$
   \task $\dfrac{5x+6}{3}+\dfrac{4-x}{5}=3$
  \end{tasks}
  \Question Solve, giving your answers correct to 3 significant figures.
   \begin{tasks}(2)
    \task $\dfrac{x(x+1)^2}{3(x-1)}=x^2$
    \task $\dfrac{2}{x+3}-\dfrac{1}{x-7}=1$
   \end{tasks}
   \end{Exercise}

\end{document}

This version only allows Exercises larger than 0.5\textheight to break over pages.

I originally used \box instead of \unvbox for the unbreakable version, but this caused problems with the spacing at the top of a page.

\documentclass[12pt,twoside]{article}
\usepackage{amsmath,amsthm,amssymb,amsfonts}

\usepackage[usenames,svgnames,dvipsnames,table]{xcolor}
\colorlet{lightcyan}{cyan!40!white}

\setlength{\parskip}{1em}

\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\usepackage{tasks}
\settasks{label-format={\bfseries},  column-sep={10pt}} 

\usepackage[lastexercise,answerdelayed]{exercise}
\counterwithin{Exercise}{section}
\counterwithin{Answer}{section}
\renewcounter{Exercise}[section]
\newcommand{\QuestionNB}{\fbox{\bfseries\arabic{Question}}\ }
\renewcommand{\ExerciseName}{\textbf{Exercise}}
\renewcommand{\ExerciseHeader}{\noindent\def\stackalignment{l}% code from https://tex.stackexchange.com/a/195118/101651
\stackunder[0pt]{\colorbox{cyan}{\textcolor{white}{\LARGE\ExerciseName\;\textbf{\LARGE\ExerciseHeaderNB}}}}{\textcolor{lightcyan}{\rule{\linewidth}{2pt}}}\medskip}

\setlength{\QuestionIndent}{16pt}

\usepackage{stackengine}
\usepackage{needspace}
\usepackage{environ}

\newsavebox{\exercisebox}% just in case
\NewEnviron{ExerciseBox}{\par
  \setbox\exercisebox=\vbox{\BODY}% measure height
  \ifdim\ht\exercisebox>0.5\textheight
    \needspace{2\baselineskip}% still want to avoid naked header
  \else
    \needspace{\ht\exercisebox}% to use \unvbox instead of \box
  \fi
\unvbox\exercisebox}

\usepackage{showframe}% alignment tool

\begin{document}

\begin{ExerciseBox}
 \begin{Exercise}
 \Question Solve the following equations, giving exact answers.
  \begin{tasks}(2)
   \task $\dfrac{5}{x}=2$
   \task $\dfrac{5x+6}{3}+\dfrac{4-x}{5}=3$
  \end{tasks}
  \Question Solve, giving your answers correct to 3 significant figures.
   \begin{tasks}(2)
    \task $\dfrac{x(x+1)^2}{3(x-1)}=x^2$
    \task $\dfrac{2}{x+3}-\dfrac{1}{x-7}=1$
   \end{tasks}
    \Question Solve, giving your answers correct to 3 significant figures.
   \begin{tasks}(2)
    \task $\dfrac{x(x+1)^2}{3(x-1)}=x^2$
    \task $\dfrac{2}{x+3}-\dfrac{1}{x-7}=1$
   \end{tasks}
    \Question Solve, giving your answers correct to 3 significant figures.
   \begin{tasks}(2)
    \task $\dfrac{x(x+1)^2}{3(x-1)}=x^2$
    \task $\dfrac{2}{x+3}-\dfrac{1}{x-7}=1$
   \end{tasks}
    \Question Solve, giving your answers correct to 3 significant figures.
   \begin{tasks}(2)
    \task $\dfrac{x(x+1)^2}{3(x-1)}=x^2$
    \task $\dfrac{2}{x+3}-\dfrac{1}{x-7}=1$
   \end{tasks}
    \Question Solve, giving your answers correct to 3 significant figures.
   \begin{tasks}(2)
    \task $\dfrac{x(x+1)^2}{3(x-1)}=x^2$
    \task $\dfrac{2}{x+3}-\dfrac{1}{x-7}=1$
   \end{tasks}
\needspace{2\baselineskip}% you may wan to patch the \Question command
    \Question Solve, giving your answers correct to 3 significant figures.
   \begin{tasks}(2)
    \task $\dfrac{x(x+1)^2}{3(x-1)}=x^2$
    \task $\dfrac{2}{x+3}-\dfrac{1}{x-7}=1$
   \end{tasks}
   \end{Exercise}
\end{ExerciseBox}

\noindent\rule{\textwidth}{25\baselineskip}

\begin{ExerciseBox}
 \begin{Exercise}
 \Question Solve the following equations, giving exact answers.
  \begin{tasks}(2)
   \task $\dfrac{5}{x}=2$
   \task $\dfrac{5x+6}{3}+\dfrac{4-x}{5}=3$
  \end{tasks}
  \Question Solve, giving your answers correct to 3 significant figures.
   \begin{tasks}(2)
    \task $\dfrac{x(x+1)^2}{3(x-1)}=x^2$
    \task $\dfrac{2}{x+3}-\dfrac{1}{x-7}=1$
   \end{tasks}
   \end{Exercise}
\end{ExerciseBox}

\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thanks for your suggestion. If I understand correctly, it seems I have to manually adjust the needspace for each exercise to prevent the header from being at the bottom of the page. Your minipage suggestion sounds better but this is also not ideal since some exercises span several pages. – A. Goodier Feb 20 '20 at 15:50
  • If you only want to avoid naked header, 2\baselinekip should do. You could only use minpages where needed. It is difficult to automate aesthetics. – John Kormylo Feb 20 '20 at 19:51