6

I have a custom example environment:

\newcounter{examplecounter}
\renewcommand{\theexamplecounter}{\arabic{examplecounter}}
\newenvironment{Beispiel}[1][]{%
\goodbreak%
\refstepcounter{examplecounter}%
\begin{list}{}{\setlength{\rightmargin}{\leftmargin}}%
\item[{\textbf{Beispiel~\theexamplecounter:}}]#1\item[]}{%
\end{list}}

This works fine, except if there is a bad page break:

enter image description here

It's ok to break this environment (in fact it is necessary as some examples are very long), but I would like to force LaTeX to keep the title with some text (lets say at least two lines or a complete table / image). How can I get that?

Complete minimal example

\documentclass[a4paper]{scrartcl}
\usepackage{blindtext}

\newcounter{examplecounter}
\renewcommand{\theexamplecounter}{\arabic{examplecounter}}
\newenvironment{Beispiel}[1][]{%
\goodbreak%
\refstepcounter{examplecounter}%
\begin{list}{}{\setlength{\rightmargin}{\leftmargin}}%
\item[{\textbf{Beispiel~\theexamplecounter:}}]#1\item[]}{%
\end{list}}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quam 
elit, vestibulum nec facilisis at, condimentum id enim. Sed iaculis 
lacinia quam, vel accumsan eros tempor in. Integer ipsum metus, 
accumsan sit amet commodo a, egestas vitae sem. Mauris ut orci ut 
dolor viverra convallis nec a erat. Aenean consequat elit vel eros 
fermentum vestibulum id at ipsum. In vitae orci mauris, et rhoncus 
odio. Pellentesque habitant morbi tristique senectus et netus et 
malesuada fames ac turpis egestas.

\blindtext[4]

\begin{Beispiel}[A title]
\begin{tabular}{ccc}
    1 & 2 & 3\\
    4 & 5 & 6\\
    7 & 8 & 9
  \end{tabular}
\end{Beispiel}
\end{document}
  • have a look at my answer to Definitive guide to trivlists it's very much related, possibly a duplicate.... – cmhughes Aug 5 '13 at 12:41
  • @Werner: Thank you very much! This is the answer to my question! But why do I need \par? If you post this as an answer, I'll accept it. – Martin Thoma Aug 5 '13 at 12:48
4

\item is very complex. Replacing \item[] with \par\nobreak inserts a similarly-spaced (vertical) break yet allows control over the page breaking:

\newenvironment{Beispiel}[1][]
  {% \begin{Beispiel}[<title>]
  \goodbreak%
  \refstepcounter{examplecounter}%
  \begin{list}{}{\setlength{\rightmargin}{\leftmargin}}%
    \item[{\textbf{Beispiel~\theexamplecounter:}}]#1\par\nobreak}%
  {\end{list}}% \end{Beispiel}
  • This seems not to work if the content is complex. So instead of a tabular environment as in the MWE I have something with linebreaks. Is there an easy way to avoid page breaks in these situations? Something like the minipage solution inside the environment definition? – Stefan Müller Sep 29 at 11:04
  • 1
    @StefanMüller: Indeed, you can use a minipage (or even the samepage environment). – Werner Sep 29 at 16:02
  • Thanks, sorry for the question. I did not see how to integrate this into the list environment, but now i figured out. \o/ – Stefan Müller Sep 29 at 16:41
1

One way would be to use the minipage environment.

\begin{minipage}{\textwidth}
\begin{Beispiel}[A title]
\begin{tabular}{ccc}
    1 & 2 & 3\\
    4 & 5 & 6\\
    7 & 8 & 9
  \end{tabular}
\end{Beispiel}
\end{minipage}
  • I don't think this is an answer to my question. I want all Beispiel environments to have the "dont break after title" property, not only one. – Martin Thoma Aug 5 '13 at 12:52
  • Ok. Then I got it wrong. I thought the whole example (Beispiel) should stick/glue together. – Dr. Manuel Kuehner Aug 5 '13 at 13:10

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.