4

I am splitting my algorithm over two pages, since it is to long. And I would like them to appear one after the other, but If I do it like in the code below, the text coming after the second part pushes between the algorithms, so on page 1..

  \documentclass{scrbook}
  \usepackage[utf8]{inputenc}
  \usepackage{algorithm}
  \usepackage{algpseudocode}

  \begin{document}

 \begin{algorithm} 
 \caption{My algorithm} 
 \begin{algorithmic} 
 \Require \(a, c, b ,d \) 
 \State some information and values.. 
 \State some information and values.. 
 \State some information and values.. 
 \State some information and values.. 
 \State some information and values.. 
 \State some information and values.. 
 \State some information and values.. 
 \For{\(j=e+1\) \textbf{TO} \(s-1\)} 
 \State do something 
 \If{eine Bedinung}
 \State doSomething
 \EndIf 
 \State Other..... 
 \algstore{alg:example}
 \end{algorithmic} 
 \end{algorithm} 


 \begin{algorithm}
 \begin{algorithmic}
 \algrestore{alg:example}
 \State lalalla
 \State lalalla
 \State lalalla
 \State lalalla
 \EndFor
 \end{algorithmic}
 \end{algorithm}

 Here is my text what floats between the algorithms but I want it to be below both of the second one, so that the algorithm parts are not interrupted...
 \end{document}

I tried to force the floats with "h" or "t" but nothing is working. I also tried to "elongate" first algorithm, because the text floats up to its page since there are two lines left, but if i do that the complete algorithm is pushed onto another page...

user63716
  • 231

2 Answers2

7

You can always force the algorithms not to float using the the floating specifier H.

MWE

  \documentclass{scrbook}
  \usepackage[utf8]{inputenc}
  \usepackage{algorithm}
  \usepackage{algpseudocode}
  \usepackage{lipsum} %just for the example

  \begin{document}

  \lipsum[1-3]

 \begin{algorithm}[H]
 \caption{My algorithm}
 \begin{algorithmic}
 \Require \(a, c, b ,d \)
 \State some information and values..
 \State some information and values..
 \State some information and values..
 \State some information and values..
 \State some information and values..
 \State some information and values..
 \State some information and values..
 \For{\(j=e+1\) \textbf{TO} \(s-1\)}
 \State do something
 \If{eine Bedinung}
 \State doSomething
 \EndIf
 \State Other.....
 \algstore{alg:example}
 \end{algorithmic}
 \end{algorithm}


 \begin{algorithm}[H]
 \begin{algorithmic}
 \algrestore{alg:example}
 \State lalalla
 \State lalalla
 \State lalalla
 \State lalalla
 \EndFor
 \end{algorithmic}
 \end{algorithm}

 Here is my text what floats between the algorithms but I want it to be below both of the second one, so that the algorithm parts are not interrupted...
 \end{document} 

Output

enter image description here

karlkoeller
  • 124,410
0

The correct way is to use

\algstore

\algstore{bkbreak}
\algrestore{bkbreak}

http://tug.ctan.org/macros/latex/contrib/algorithmicx/algorithmicx.pdf

While some time when you break the algorithm, some text will fill in between the two parts of the algorithm, which is annoying.
I finally find out that in order to avoid the text coming in, you need to make your first algorithm page almost full.

Stefan Pinnow
  • 29,535
Kevin
  • 101
  • Your final statement is not necessarily true. There is no need to keep the bulk of you algorithm on the first page. What if your text flow doesn't allow this? – Werner Mar 15 '17 at 01:29
  • I am saying the first page of your algorithm, since algorithm may be split into multiple pages. – Kevin Mar 15 '17 at 01:55