I'm starting my experiment with LaTeX, i'm writing an article and all that I want is to embed an algorithm inside normal text, this is my code:

\documentclass{article}
\usepackage{algorithmic}
\begin{document}
\section{Lettura file .gpx} 
Lorem ipsum ...

\algsetup{indent=1.7em}
\begin{algorithm}
\caption{Alg capt}
\label{alg1}

\begin{algorithmic}[1]
... algorithm here...
\end{algorithmic}
\end{algorithm}
\end{document}

This is fully working but algorithmic uses a pagebreak automatically, how can I avoid this break and show the algorithm immediately after "lorem ipsum" part?

migrated from stackoverflow.com Nov 6 '11 at 19:45

This question came from our site for professional and enthusiast programmers.

Here are some suggestions:

  • If you're adding your algorithm inside the algorithm float, then it will float according to the "float specifiers" given as the optional argument to the algorithm environment. For example

    \begin{algorithm}[ht]
      ...
    \end{algorithm}
    

    To force the float to be less float-y, add ! to the float specifier. However, as suggested by the float package, this still remains a "suggestive specifier". Instead, the package provides the additional H float specifier which tells LaTeX to "PUT IT HERE!"

  • Use the algorithmx package. It is very similar to algorithmic, but allows for more flexibility. Here's a short, yet complete, minimal example (from the algorithmicx package documentation).

  • If you wish to have the algorithm break across the page boundary, you have to set things up differently. I've incorporated a breakablealgorithm environment below.

enter image description here

\documentclass{article}
\usepackage{algorithm,algpseudocode,float}
\usepackage{lipsum}

\makeatletter
\newenvironment{breakablealgorithm}
  {% \begin{breakablealgorithm}
   \begin{center}
     \refstepcounter{algorithm}% New algorithm
     \hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
     \renewcommand{\caption}[2][\relax]{% Make a new \caption
       {\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}%
       \ifx\relax##1\relax % #1 is \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
       \else % #1 is not \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
       \fi
       \kern2pt\hrule\kern2pt
     }
  }{% \end{breakablealgorithm}
     \kern2pt\hrule\relax% \@fs@post for \@fs@ruled
   \end{center}
  }
\makeatother

\begin{document}

\listofalgorithms

\section{Some section}

\lipsum[1]

\begin{algorithm}[H]
  \caption{Euclid’s algorithm}\label{euclid}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
    \State $r \gets a \bmod b$
    \While{$r \neq 0$}\Comment{We have the answer if~$r$ is~$0$}
      \State $a \gets b$
      \State $b \gets r$
      \State $r \gets a \bmod b$
    \EndWhile
    \State \textbf{return} $b$\Comment{The g.c.d.\ is~$b$}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\lipsum[2]

\begin{breakablealgorithm}
  \caption{Euclid’s algorithm}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
    \State $r \gets a \bmod b$
    \While{$r \neq 0$}\Comment{We have the answer if~$r$ is~$0$}
      \State $a \gets b$
      \State $b \gets r$
      \State $r \gets a \bmod b$
    \EndWhile
    \State \textbf{return} $b$\Comment{The g.c.d.\ is~$b$}
    \EndProcedure
  \end{algorithmic}
\end{breakablealgorithm}

\end{document}

The above redefinition of \caption assumes that it will be placed where it's located in the algorithm; specifically at the top. Rule definitions were taken from the float package's \@fs@ruled construction.

If you don't want your algorithm to be treated as a floating object, then don't use the algorithm environment; you can still have a caption using the \captionof command from the caption package; using the appropriate style and format, this caption can have the same look and feel as those produced by the algorithm environment:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{caption}
\usepackage{lipsum}

\DeclareCaptionFormat{algor}{%
  \hrulefill\par\offinterlineskip\vskip1pt%
    \textbf{#1#2}#3\offinterlineskip\hrulefill}
\DeclareCaptionStyle{algori}{singlelinecheck=off,format=algor,labelsep=space}
\captionsetup[algorithm]{style=algori}

\begin{document}
\section{Lettura file .gpx} 
\lipsum*[1]
\begin{center}
  \captionof{algorithm}{Euclid’s algorithm}\label{euclid}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}
\end{center}

\end{document}

enter image description here

  • 1
    Is there a way to insert a rule automatically at the end of the algorithm? I've try \rule[15pt]{\textwidth}{0.5pt} manually but it's so inconvenient and there will be a large gap between the rule and the following text. Thank you! @Gonzalo – Stan Jan 8 '13 at 8:48
  • @Stan, did you get a solution for the bottom line?? – CroCo Feb 18 '15 at 22:30
  • Note: mixing this with actual algorithm floats leads to duplicated horizontal ruling. – Richard Dec 24 '15 at 3:56

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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