@TOC
LaTeX伪代码(algorithm)分页展示
有些时候,LaTeX中的算法伪代码很长需要跨页展示,如下如图所示
我们只需要在\begin{document}
之前定义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
注意,要在导包部分加入下面命令。
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
然后在要展示算法伪代码的时候,将\begin{algorithm}
和\end{algorithm}
替换成
\begin{breakablealgorithm}
和\end{breakablealgorithm}
, 例如
\begin{breakablealgorithm}%[H]
\caption{your algorithm}%算法标题
\begin{algorithmic}[1]%一行一个标行号
\State \textbf{Given} : Fixed taxi ID $t$, trip request set $P$, distance matrix $D$
\State \textbf{Given} : tolerance factor $\Delta$, weight matrix $W$
\State \Return {$\mathcal{U}$}
\end{algorithmic}
\end{breakablealgorithm}
这样运行就可以了。