3

I am using the method here to generate my pseudocode, however my code is long and after running the tex file, instead of breaking the code box into two parts, it moves the whole code to the next page. I want to know how can I break it? Here is the code:

\documentclass[11pt,bezier]{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{amssymb,amsfonts,xspace}
\usepackage{amsmath}
\usepackage{algorithm,fontspec}
\usepackage[noend]{algpseudocode}
\renewcommand{\baselinestretch}{1.1}
\textwidth = 15 cm \textheight = 20 cm \oddsidemargin =0.7 cm
\evensidemargin = 0 cm \topmargin = -0.2 cm
\parskip = 2 mm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\alglinenumber[1]{
    {\sf\footnotesize\addfontfeatures{Colour=888888,Numbers=Monospaced}#1}}
\algrenewcommand\algorithmicrequire{\textbf{Precondition:}}
\algrenewcommand\algorithmicensure{\textbf{Postcondition:}}



    \begin{document}
    \begin{algorithm}
      \caption{Counting mismatches between two packed DNA strings
        \label{alg:packed-dna-hamming}}
      \begin{algorithmic}[1]
        \Require{$x$ and $y$ are packed DNA strings of equal length $n$}
        \Statex
        \Function{Distance}{$x, y$}
          \Let{$z$}{$x \oplus y$} \Comment{$\oplus$: bitwise exclusive-or}
          \Let{$\delta$}{$0$}
          \For{$i \gets 1 \textrm{ to } n$}
            \If{$z_i \neq 0$}
              \Let{$\delta$}{$\delta + 1$}
            \EndIf
          \EndFor
          \State \Return{$\delta$}
        \EndFunction
      \end{algorithmic}
    \end{algorithm}
    \end{document}

P.S.I removed my text before it, which results in the shift.

1
  • The algorithm environment is like figure or table, so it can't be split across pages.
    – egreg
    Apr 18, 2013 at 23:43

1 Answer 1

2

You have two options:

  1. Don't use the algorithm environment (it's a floating environment so it doesn't admit page breaks).

  2. Use \algstore, and \algrestore as described in Section 2.6 Breaking up long algorithms of the documentation for package algorithmicx.

A complete example showing both approaches:

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{algpseudocode}
\usepackage{algorithm}

\begin{document}

\vspace*{18\baselineskip}

\begin{algorithmic}[1]
  \Require{$x$ and $y$ are packed strings of equal length $n$}
  \Statex
  \Function{Distance}{$x, y$}
    \For{$i \gets 1 \textrm{ to } n$}
      \If{$z_i \neq 0$}
      \EndIf
    \EndFor
    \State \Return{$\delta$}
  \EndFunction
\end{algorithmic}

\begin{algorithm}
\caption{Part 1}
\begin{algorithmic}[1]
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
\ForAll {$v \in V(G)$}
\State $l(v) \leftarrow \infty$
\EndFor
\State $l(u) \leftarrow 0$
\Repeat
\For {$i \leftarrow 1, n$}
\State $min \leftarrow l(v_i)$
\For {$j \leftarrow 1, n$}
\If {$min > e(v_i, v_j) + l(v_j)$}
\State $min \leftarrow e(v_i, v_j) + l(v_j)$
\State \Comment For some reason we need to break here!
\algstore{bkbreak}
\end{algorithmic}
\end{algorithm}
And we need to put some additional text between\dots
\addtocounter{algorithm}{-1}
\begin{algorithm}[h]
\caption{Part 2}
\begin{algorithmic}[1]
\algrestore{bkbreak}
\State $p(i) \leftarrow v_j$
\EndIf
\EndFor
\State $l’(i) \leftarrow min$
\EndFor
\State $changed \leftarrow l \not= l’$
\State $l \leftarrow l’$
\Until{$\neg changed$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}


\end{document}

enter image description here

Not the answer you're looking for? Browse other questions tagged .