我正在使用的方法这里生成我的伪代码,但是我的代码很长,运行 tex 文件后,它不会将代码框分成两部分,而是将整个代码移动到下一页。我想知道如何拆分它?代码如下:
\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}
PSI 删除了我之前的文本,导致了转变。
答案1
您有两个选择:
不要使用该
algorithm
环境(它是一个浮动环境,因此不允许分页)。使用
\algstore
,并\algrestore
按照第节所述2.6 分解长算法包的文档algorithmicx
。
展示这两种方法的完整示例:
\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}