太长的算法无法拆分成两页

太长的算法无法拆分成两页

我在算法环境中编写伪代码。它很长,无法放在一页上。我尝试了许多选项,但无法分成两页。在另一个文件中(因为这是论文),我声明了以下包:

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

这是我的代码:

\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}

答案1

要将算法拆分为两页,请使用 Werner 的便捷breakablealgorithm环境,如下所述这里

将此代码添加到您的序言中:

\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

然后algorithmic\begin{breakablealgorithm}和包围你的\end{breakablealgorithm}。因此:

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

编辑: 如果我把你的长算法塞进Werner的环境中,那么它就能完美地运行。

\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}

如果它对你有用,那么所有的功劳都归功于 Werner。

相关内容