我使用algorithm
float 环境编写伪代码,并希望某些算法跨越两页。使用\algstore
和可以很好地完成此\algrestore
操作。但是,我想在算法第一部分的末尾和后续部分的顶部省略这些边框。
有没有办法去除它们并保留剩余的边界线?
我拥有的是类似的东西:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algcompatible}
\newcommand{\algoallowbreak}{\algstore{myalg}\end{algorithmic}\end{algorithm}
\begin{algorithm}
\begin{algorithmic}\algrestore{myalg}
}
\begin{document}
\begin{algorithm}[h]
\caption{Algorithm 1}
\begin{algorithmic}[1]
\State $a \gets b$
\State some other code
\algoallowbreak
\State some code that could possibly be wrap onto the next side
\end{algorithmic}
\end{algorithm}
\end{document}
我想删除算法行 2 和 3 之间的两条边界线。
答案1
这是一个可能的解决方案;它要求您\BreakAlgo
在具有新规范的算法之前发出:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algcompatible}
\usepackage{etoolbox}
\makeatletter
\newcommand\fs@myruledtop{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
\def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}%
\def\@fs@post{\kern2pt}%
\def\@fs@mid{\kern2pt\hrule\kern2pt}%
\let\@fs@iftopcapt\iftrue}
\newcommand\fs@myruledbottom{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
\def\@fs@pre{\kern2pt}%
\def\@fs@post{\kern2pt\hrule\relax}%
\def\@fs@mid{}%
\let\@fs@iftopcapt\iftrue}
\makeatother
\newif\ifalgobreak
\newif\ifalgobreaksecond
\newcommand\BreakAlgo{\algobreaktrue}
\AtBeginEnvironment{algorithm}{
\ifalgobreak
\floatstyle{myruledtop}
\restylefloat{algorithm}
\fi
\ifalgobreaksecond
\floatstyle{myruledbottom}
\restylefloat{algorithm}
\else
\fi
}
\AfterEndEnvironment{algorithm}{%
\algobreakfalse\algobreaksecondfalse
\floatstyle{ruled}
\restylefloat{algorithm}%
}
\newcommand\algoallowbreak[1][]{%
\algstore{myalg}
\end{algorithmic}
\end{algorithm}
\algobreaksecondtrue
\begin{algorithm}
\begin{algorithmic}[#1]
\algrestore{myalg}%
}
\begin{document}
\BreakAlgo
\begin{algorithm}[h]
\caption{Algorithm 1}
\begin{algorithmic}[1]
\State $a \gets b$
\State some other code
\algoallowbreak
\State some code that could possibly be wrap onto the next side
\end{algorithmic}
\end{algorithm}
\begin{algorithm}[h]
\caption{Algorithm 2}
\begin{algorithmic}[1]
\State $a \gets b$
\State some other code
\end{algorithmic}
\end{algorithm}
\end{document}
这个想法是定义两种新的算法风格(一种抑制底部规则,另一种抑制顶部规则),并根据以下情况有条件地应用这些新风格:\BreakAlgo
我还添加了一个可选参数,以便\algoallowbreak
在延续部分中指定编号;请注意,这解决了您在延续的原始设置中遇到的对齐问题。如果编号始终存在,则不需要可选参数,在这种情况下,定义将是
\newcommand\algoallowbreak{%
\algstore{myalg}
\end{algorithmic}
\end{algorithm}
\algobreaksecondtrue
\begin{algorithm}
\begin{algorithmic}[1]
\algrestore{myalg}%
}
然后,在您的文档中:
\BreakAlgo
\begin{algorithm}[h]
\caption{Algorithm 1}
\begin{algorithmic}[1]
\State $a \gets b$
\State some other code
\algoallowbreak
\State some code that could possibly be wrap onto the next side
\end{algorithmic}
\end{algorithm}
答案2
我怀疑您想要这种平滑换行的唯一原因是因为algorithm
您希望跨页面换行。为此,我创建了一个nofloatalgorithmic
环境,它接受一个可选参数作为行号(就像常规algorithmic
环境一样)和一个强制参数作为算法标题*。
\documentclass{article}
\usepackage[paper=a6paper]{geometry}% Just for this example
\usepackage{algorithm,needspace}
\usepackage{algcompatible}
\makeatletter
\newenvironment{nofloatalgorithmic}[2][0]
{% \begin{nofloatalgorithmic}
\par
\addvspace{\intextsep}% Vertical gap above in-text float
\needspace{\dimexpr\baselineskip+6.8pt}%
\noindent%
\hrule height.8pt depth0pt \kern2pt% Top horizontal rule
\refstepcounter{algorithm}% Step algorithm counter
\addcontentsline{loa}{algorithm}{\numberline{\thealgorithm}#2}% Add entry to LoA
\noindent\textbf{\fname@algorithm~\thealgorithm} #2\par% Write caption
\kern2pt\hrule\kern2pt% Middle horizontal rule
\begin{algorithmic}[#1]
}
{% \end{nofloatalgorithmic}
\end{algorithmic}
\nobreak\kern2pt\hrule\relax% Bottom horizontal rule
\addvspace{\intextsep}% Vertical gap below in-text float
}
\makeatother
\begin{document}
\listofalgorithms
\begin{nofloatalgorithmic}[1]{Some algorithm}
\State $a \gets b$
\State some other code
\State some code that could possibly be wrap onto the next side
\State $a \gets b$
\State some other code
\State some code that could possibly be wrap onto the next side
\State $a \gets b$
\State some other code
\State some code that could possibly be wrap onto the next side
\State $a \gets b$
\State some other code
\State some code that could possibly be wrap onto the next side
\State $a \gets b$
\State some other code
\State some code that could possibly be wrap onto the next side
\State $a \gets b$
\State some other code
\State some code that could possibly be wrap onto the next side
\State $a \gets b$
\State some other code
\State some code that could possibly be wrap onto the next side
\State $a \gets b$
\State some other code
\State some code that could possibly be wrap onto the next side
\end{nofloatalgorithmic}
\begin{algorithm}[h]
\caption{Another algorithm}
\begin{algorithmic}[1]
\State $a \gets b$
\State some other code
\end{algorithmic}
\end{algorithm}
\end{document}
标题的生成方式与algorithm
包裹确实如此。事实上,algorithm
使用float
以使用以下“前”、“中”和“后”定义标题的样式algorithm
来设置浮动:ruled
\newcommand\fs@ruled{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
\def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}%
\def\@fs@post{\kern2pt\hrule\relax}%
\def\@fs@mid{\kern2pt\hrule\kern2pt}%
\let\@fs@iftopcapt\iftrue}
needspace
确保新标题符合实际的分页列表(即algorithmic
)。
最后,由于这被设置为文本内的浮动,因此上下的间隙\intextsep
以真正的浮动方式将其与常规段落文本区分开来,正如浮动中使用的长度视图所建议的那样(来自layouts
文档):
附言:我尚未进行广泛的测试以确保分页符不会破坏/分割水平规则,因此如果有问题,请告诉我。
*此定义可以扩展,以采用可用于不同 LoA 样式条目的附加可选参数。