Text FormattingPrevent Page Breaks in the same Item of a List

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

Prevent Page Breaks in the same Item of a List

Post by GommaFleX »

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:

Code: Select all

\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:

Code: Select all

\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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10340
Joined: Mon Mar 10, 2008 9:44 pm

Prevent Page Breaks in the same Item of a List

Post by Stefan Kottwitz »

Hi,

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

Code: Select all

\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
LaTeX.org admin
GommaFleX
Posts: 10
Joined: Tue Aug 16, 2011 12:19 pm

Prevent Page Breaks in the same Item of a List

Post by GommaFleX »

Thank you, your solution worked!
I adapted it in this way, so the space is a parameter and I can specify it every time:

Code: Select all

\newcommand{\algoritmo}[6]{
\needspace{#6\baselineskip}
\textbf{#1} \\
\textbf{Descrizione:} #2 
\begin{algorithm}
\KwIn{#3}
\KwOut{#4}
#5
\end{algorithm}
}
Post Reply