[Tex/LaTex] Algorithm tag and page break

algorithmsfloats

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?

Best Answer

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}
\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{\fname@algorithm~\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.