1

I am writing pseudo code in the algorithmic environment. It is quite long and doesn't fit on one single page. I tried with many option but cannot divide into 2 pages. In another file (since this is thesis) I declared packages for:

\usepackage[vlined,linesnumbered]{algorithm2e} % Algorithm
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{algcompatible}

Here is my code:

\begin{figure}[h]
\small

\hrule
\vspace{2mm}
\textbf{Reader Operation}
%\vspace{1mm}

\begin{algorithmic}[1]

    \STATE $ws = 1$
    \STATE FQwT([\textquoteleft 0'],$ws$); FQwT([\textquoteleft 1'],$ws$)   
    \STATE function $FQwT$(char [] $query,$ int $ws$)
    \STATE $k = ID.length$
    \STATE $L = query.length$
    \STATE $c_g=k-L$
    \STATE $s=log2(ws)$
    \STATE broadcast([$query], s$)
    \STATE $[winMatch,crcCheck] = $receiveResponses()
    \IF{$nSuccess=0$} 
    \STATE{$Phase1$}
    \IF{$isempty$($winMatch$)}
    \STATE $nIdles ++$ 
    \ELSE [$crcCheck = 0$] 
    \STATE $nCollisions ++$ 
    \IF{$ws=1$}
    \STATE $Store$ $c_g$ $with$ $query$ $into$ $LIFO$ $(c_g=k-L)$
    \STATE $FQwT$([$query$, \textquoteleft 0'],$ws$); FQwT([$query$, \textquoteleft 1'],$ws$)
    \ELSE 
    \STATE $ws = 1$
    \STATE $FQwT$ ([$query$],$ws$); FQwT([$query$],$ws$)
    \ENDIF
    \ELSE [$crcCheck=1$]
    \IF{$L + ws < k$} 
    \STATE $nGoons ++$ 
    \STATE $ws=ws ++$ 
    \STATE FQwT($[query,winMatch],ws$)
    \ELSE  [$L + ws = k$]  
    \STATE $ws=1$
    \STATE $nSuccess ++$
    \ENDIF
    \ENDIF
    \ELSE 
    \STATE{$Phase2$}
    \IF{isempty($winMatch$)}
    \STATE $nIdles ++$
    \ELSE[$crcCheck = 0$] 
    \STATE $nCollisions ++$
    \STATE FQwT([$query$, \textquoteleft 0'],$ws$); FQwT([$query$, \textquoteleft 1'],$ws$)
    \ELSE[$crcCheck = 1$]
    \IF{$L + ws < k$} 
    \STATE $nGoons ++$
    \STATE $ ws=f(c_g,L)$
    \STATE $s= log2(ws)$
    \STATE FQwT([$query$, $winMatch$],$ws$); 
    \ELSE[$L + ws = k$]  
    \STATE $nSuccess ++$
    \ENDIF
    \ENDIF
    \ENDIF

\end{algorithmic}

\vspace{1mm}
\hrule
\vspace{2mm}

\textbf{Tag Operation}
\vspace{1mm}

\begin{algorithmic}[1]

    \STATE $receive(query, s)$
    \STATE $L = query.length$
    \STATE $ws=2^s$
    \IF{$query = ID[0:L-1]$}
    \STATE $CRC=$ crc($ID[L:L{+}ws]$)
    \STATE backscatter($ID[L:L{+}ws],CRC$)
    \ENDIF

\end{algorithmic}
\vspace{1mm}
\hrule
\caption{Pseudo-code of FQwT.
    First, the operation of the reader is presented, then the tag operation.}
\label{Fig:dis53}
\end{figure}
  • Floats like algorithimic cannot be broken over two pages, but environments like alagorithm can be, See also \captionof (caption package). – John Kormylo Apr 23 '18 at 2:38
  • @JohnKormylo what can I do? – John988 Apr 23 '18 at 2:47
  • Maybe have a look here: ctan.org/topic/pseudocode – Dr. Manuel Kuehner Apr 23 '18 at 5:30
3

To break your algorithm over two pages, then use Werner's handy breakablealgorithm environment as described here.

Add this code to your preamble:

\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

Then surround your algorithmic with \begin{breakablealgorithm} and \end{breakablealgorithm}. Thus:

\begin{breakablealgorithm}
  \begin{algorithmic}[1]
    ...
    ...
    ...
  \end{algorithmic}
\end{breakablealgorithm}

EDIT: If I stuff your long algorithm into Werner's environment, then it works perfectly well.

\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{breakablealgorithm}
  \caption{Euclid’s algorithm}
  \begin{algorithmic}[1]

    \State $ws = 1$
    \State FQwT([\textquoteleft 0'],$ws$); FQwT([\textquoteleft 1'],$ws$)   
    \State function $FQwT$(char [] $query,$ int $ws$)
    \State $k = ID.length$
    \State $L = query.length$
    \State $c_g=k-L$
    \State $s=log2(ws)$
    \State broadcast([$query], s$)
    \State $[winMatch,crcCheck] = $receiveResponses()
    \If{$nSuccess=0$} 
    \State{$Phase1$}
    \If{$isempty$($winMatch$)}
    \State $nIdles ++$ 
    \Else [$crcCheck = 0$] 
    \State $nCollisions ++$ 
    \If{$ws=1$}
    \State $Store$ $c_g$ $with$ $query$ $into$ $LIFO$ $(c_g=k-L)$
    \State $FQwT$([$query$, \textquoteleft 0'],$ws$); FQwT([$query$, \textquoteleft 1'],$ws$)
    \Else 
    \State $ws = 1$
    \State $FQwT$ ([$query$],$ws$); FQwT([$query$],$ws$)
    \EndIf
    \Else [$crcCheck=1$]
    \If{$L + ws < k$} 
    \State $nGoons ++$ 
    \State $ws=ws ++$ 
    \State FQwT($[query,winMatch],ws$)
    \Else  [$L + ws = k$]  
    \State $ws=1$
    \State $nSuccess ++$
    \EndIf
    \EndIf
    \Else 
    \State{$Phase2$}
    \If{isempty($winMatch$)}
    \State $nIdles ++$
    \Else[$crcCheck = 0$] 
    \State $nCollisions ++$
    \State FQwT([$query$, \textquoteleft 0'],$ws$); FQwT([$query$, \textquoteleft 1'],$ws$)
    \Else[$crcCheck = 1$]
    \If{$L + ws < k$} 
    \State $nGoons ++$
    \State $ ws=f(c_g,L)$
    \State $s= log2(ws)$
    \State FQwT([$query$, $winMatch$],$ws$); 
    \Else[$L + ws = k$]  
    \State $nSuccess ++$
    \EndIf
    \EndIf
    \EndIf

\end{algorithmic}
\end{breakablealgorithm}

\end{document}

If it works for you, all credit to Werner.

| improve this answer | |
  • I tried and got the same :( @AML – John988 Apr 23 '18 at 22:06
  • @John988 Works for me. Try the additional code above. – AML Apr 25 '18 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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