Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using algorithmicx to write algorithms. I want to be able to write algorithms so that they appear right after the paragraph they have been written and possibly split them from the end of one page to the starting of the next page if the space is not sufficient.

How to do so?

share|improve this question
    
Please add a MWE. –  Marco Daniel Sep 4 '11 at 1:13

1 Answer 1

To break down your post, you want:

  1. To have an algorithm appear immediately after a paragraph in which it was discussed; and
  2. To have the algorithm split across pages (if possible).

In order to fulfill both requests, you cannot place your algorithmicx environment within an algorithm environment, since the latter is a float. And, floats move around. Moreover, floats do not break over pages. So, you'll have to make your own environment that accounts for an algorithm caption and number. Here is a very basic example of what that would entail:

\documentclass{article}
\usepackage{algorithmicx}% http://ctan.org/pkg/algorithmicx
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{xifthen}% http://ctan.org/pkg/xifthen
\usepackage{needspace}% http://ctan.org/pkg/needspace
\usepackage{hyperref}% http://ctan.org/pkg/hyperref

% ================ ALGORITHM ENVIRONMENT ================
\newcounter{myalg}% Algorithm counter
\newenvironment{myalg}[1][]%
  {% \begin{myalg}[#1]
    \needspace{2\baselineskip}% At least 2\baselineskip required, otherwise break
    \noindent \rule{\linewidth}{1pt} \endgraf% Top rule
    \refstepcounter{myalg}% For correct reference of algorithm
    \centering \textsc{Algorithm}~\themyalg%
    \ifthenelse{\isempty{#1}}{}{:\ #1}% Typeset name (if provided)
  }{% \end{myalg}
  \noindent \rule{\linewidth}{1pt}% Bottom rule
  }%

\begin{document}

\lipsum[1-4]

Here is Algorithm~\ref{first-alg}:

\begin{myalg}[My first algorithm]
\label{first-alg}
\begin{algorithmic}[1]
 \State This is some code
 \State This is some more code
 \State Here is some code
 \State This is some code
 \State This is some more code
 \State Here is some code
 \State This is some code
 \State This is some more code
 \State Here is some code
 \State This is some code
 \State This is some more code
 \State Here is some code
\end{algorithmic}
\end{myalg}

That was Algorithm~\ref{first-alg}.

\lipsum[4-6]

\end{document}

In the above example, the myalg environment does not float and replaces the original algorithm environment. As an optional argument, myalg will typeset a caption. The xifthen package provides a conditional clause to check for a caption or not. The needspace package conditions on the amount of space left on the page. In order to not be stuck with a rule or caption at the page bottom, needspace will issue a \break if there is less then 2\baselineskip left on the page. The hyperref package was also included to show you that the hyperlinks work properly, while the lipsum package provides dummy text.

enter image description here

If you're into a more vibrant array of decorations for your algorithms, then the mdframed package allows easy breaking of decorative frames across pages:

enter image description here

This can be extended to include items into a "List of Algorithms" as well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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