0

I have a command \sectionExercise{...} that produce a title followed by a thick horizontal line.

This command is always followed by a minipage : \begin{exo}...\end{exo}

Sometimes, there is a column break between the title (of a series of exercises) and the exercises.

In general, is there a way to prevent a column or page break between two "boxes"?

Example of such a column break

enter image description here

Some code

\newcommand{\sectionExercise}[1]{%
\vspace{8mm plus 2mm minus 1mm}
%\bigskip
{\bfseries \Large #1\par}
\par
\begin{tikzpicture}
    \node[minimum width=\columnwidth, minimum height=2mm] (a) {};
    \draw[color=blue!30, fill=blue!30, rounded corners=2pt] (a.south west) -- (a.north west) -- (a.north east) -- (a.south east) -- cycle ;
\end{tikzpicture}
\gdef\varIsNewSectionExercise{1}
}
  • 2
    You can add, say, \penalty-\@lowpenalty before the \sectionExercise and \nobreak between the horizontal rule and the first exercise. This may work. For a more concrete answer please post a working example. You've been around here longer than me, you should know that by now... – Phelype Oleinik May 13 '18 at 19:32
  • Yes I know, of course! – Colas May 13 '18 at 19:46
  • @Colas If the solution posted by me works as a charm, please accept it. – gvgramazio May 14 '18 at 8:39
3

Just use \needspace{...} from needspace package inside of your \sectionExercise command.

Edit: Someone pointed out in the comments that my answer is short, without references nor example... and is right. The fact is that I don't have a MWE to work with. Regarding references, the package comes with the official distribution of TeX Live and has a short documentation of only two commands. The actual solution is just to use the command with almost any lenght (in this case), i.e. using \needspace{4\baselineskip} at the beginning of the definition of the command defined by OP. All other thing could remain unchanged and doesn't have particular conficts with other packages (maybe it could not behave as expected if \flushbottom is in effect).

Edit: Just for future readers. This problem arise also with default commands like \section and similar. By default LaTeX considers ugly putting the title of a section at the end of the page and the text at the beginning of the new page. However, if \section is followed by \subsection it can happen. I don't know why it don't handle automatically this situation, probably because one shouldn't put a new subsection right after section. However, in this cases, instead of using needspace one could use the right options given by titlesec.

  • Please expand your answer so that it is self-contained (e.g. that the parameter of needspace is a length and maybe a real code example). – TeXnician May 13 '18 at 20:05
  • 1
    @TeXnician How I'm supposed to do that if OP doesn't even posted a mwe that I should adjust? Beside, almost every lenght is ok, but if I add a particular lenght maybe some future reader may thinks that it's a particular lenght. – gvgramazio May 13 '18 at 20:11
  • You could leave \needspace{...} and add: for example \needspace{2\baselineskip}. – corporal May 14 '18 at 1:39
  • Hope that this is enough. Otherwise please point out what I should add. – gvgramazio May 14 '18 at 8:38
  • Your answer is good, suggest you delete the * and amend to \needspace{4\baselineskip}. By the way, I upvoted your answer. Yes, lack of minimal working example made your answer more difficult to be comprehensive. The documentation of needspace package is very brief. I didn't know about this package until I read your answer. – corporal May 14 '18 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.