LaTeX forum ⇒ 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, ...)
GommaFleX
Posts: 10
Joined: Tue Aug 16, 2011 12:19 pm

Prevent Page Breaks in the same Item of a List

Postby GommaFleX » Tue Aug 16, 2011 12:35 pm

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:
  1. \newcommand{\algoritmo}[5]{
  2. \textbf{#1} \\
  3. \textbf{Descrizione:} #2
  4. \begin{algorithm}
  5. \KwIn{#3}
  6. \KwOut{#4}
  7. #5
  8. \end{algorithm}
  9. }

Then in the document i use itemize to create a list:
  1. \begin{itemize}
  2. \item \algoritmo{bla bla}{.....
  3. \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.

User avatar
Stefan Kottwitz
Site Admin
Posts: 9572
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Tue Aug 16, 2011 2:35 pm

Hi,

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

  1. \usepackage{needspace}
  2. ...
  3. \needspace{3\baselineskip}
  4. \begin{algorithm}
  5. ...


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

Postby GommaFleX » Tue Aug 16, 2011 3:50 pm

Thank you, your solution worked!
I adapted it in this way, so the space is a parameter and I can specify it every time:
  1. \newcommand{\algoritmo}[6]{
  2. \needspace{#6\baselineskip}
  3. \textbf{#1} \\
  4. \textbf{Descrizione:} #2
  5. \begin{algorithm}
  6. \KwIn{#3}
  7. \KwOut{#4}
  8. #5
  9. \end{algorithm}
  10. }


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 2 guests