## LaTeX forum ⇒ Text Formatting ⇒ Prevent Page Breaks in the same Item of a List

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
GommaFleX
Posts: 10
Joined: Tue Aug 16, 2011 12:19 pm

### Prevent Page Breaks in the same Item of a List

Hello, I'm new in LaTeX. I want to create a custom list of algorithms, with a title and a custom description for each.
So I used \newcommand to define this custom item:
\newcommand{\algoritmo}[5]{
\textbf{#1} \\
\textbf{Descrizione:} #2
\begin{algorithm}
\KwIn{#3}
\KwOut{#4}
#5
\end{algorithm}
}


Then in the document i use itemize to create a list:
\begin{itemize}
\item \algoritmo{bla bla}{.....
\item \algoritmo{.....


The result should be:

- TITLE1
DESCRIPTION1
ALGORITHM1
- TITLE2
DESCRIPTION2
ALGORITHM2

My problem is that if there isn't enaugh space in the same page, then the section \begin{algorithm} is put in the next page, after the next element in the same way:

- TITLE1
DESCRIPTION1
- TITLE2
DESCRIPTION2
NEW PAGE
ALGORITHM1
ALGORITHM2

If I use \begin{samepage} it doesn't work and the result is the same.
Is there any solution?
Thank you
Last edited by GommaFleX on Tue Aug 16, 2011 8:04 pm, edited 1 time in total.

Stefan Kottwitz
Posts: 9703
Joined: Mon Mar 10, 2008 9:44 pm
Hi,

you could use the needspace package, if you know how much space is meaningful to reserve, for example:

\usepackage{needspace}
...
\needspace{3\baselineskip}
\begin{algorithm}
...

If there's not enough space, a page break would be inserted. If there are more than three (or more) lines in an item, a page break could be meaningful and thus allowed.

Stefan

GommaFleX
Posts: 10
Joined: Tue Aug 16, 2011 12:19 pm
I adapted it in this way, so the space is a parameter and I can specify it every time:
\newcommand{\algoritmo}[6]{
\needspace{#6\baselineskip}
\textbf{#1} \\
\textbf{Descrizione:} #2
\begin{algorithm}
\KwIn{#3}
\KwOut{#4}
#5
\end{algorithm}
}