简洁版本:
我想定义一个新的浮动环境,用DeclareNewTOC
水平线包围它。
长版本:
我使用包algpseudocode
来排版我的算法。由于algpseudocode
不提供浮动环境,因此可以使用旧algorithm
包中的环境。此包与章节标题/标签结合使用会产生警告Class scrbook Warning: \float@addtolists detected!
。当然,可以使用包来避免此警告scrhack
。
以下是演示此行为的最小示例:
\documentclass{scrbook}
% \usepackage{scrhack}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\chapter{Chapter Heading}
\label{ch:1}
\end{document}
由于 KOMA-scripts 提供了DeclareNewTOC
,我认为我可以使用此命令为此目的定义一个新环境。除了水平线之外,这工作得很好。我尝试了\hrulefill
一些其他命令,但间距不令人满意。以下是使用 的最小示例DeclareNewTOC
:
\documentclass{scrbook}
\DeclareNewTOC[
type=algorithm,
float,
floatpos=h,
name=Algorithm,
counterwithin=chapter]
{loalg}
% \usepackage{scrhack}
% \usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Caption for algorithm 1.}
\label{alg:alg1}
\begin{algorithmic}[0]
\For{$i = 0$ to $10$}
\If{$i$ is odd}
\State $x_i \gets 1$
\EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
最小示例的输出和我根据algorithm
包所针对的输出。我至少想要标题和代码末尾之间的那行。
答案1
您可以使用密钥atbegin
来修补环境algorithmic
内部的环境algorithm
。
\documentclass{scrbook}
\usepackage{algpseudocode}
\usepackage{xpatch}
\DeclareNewTOC[
type=algorithm,
float,
floatpos=h,
name=Algorithm,
counterwithin=chapter,
atbegin={%
%\noindent\rule{\linewidth}{.8pt}\vspace{-\baselineskip}% rule at begin of algorithm
\xpretocmd\algorithmic
{\noindent\rule[\ht\strutbox]{\linewidth}{.8pt}\vspace{-\baselineskip}}
{}{\PatchFailed}%
\xapptocmd\endalgorithmic
{\noindent\rule[\ht\strutbox]{\linewidth}{.8pt}\vspace{-\baselineskip}}
{}{\PatchFailed}%
}
]
{loalg}
\usepackage{blindtext}% dummy text
\begin{document}
\chapter{Chapter Heading}
\label{ch:1}
\blindtext
\begin{algorithm}
\caption{Caption for algorithm 1.}
\label{alg:alg1}
\begin{algorithmic}[0]
\For{$i = 0$ to $10$}
\If{$i$ is odd}
\State $x_i \gets 1$
\EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\blindtext
\begin{algorithmic}[0]
\For{$i = 0$ to $10$}
\If{$i$ is odd}
\State $x_i \gets 1$
\EndIf
\EndFor
\end{algorithmic}
\end{document}
结果:
您还可以在使用的开头插入一行algorithm
:
atbegin={%
\noindent\rule{\linewidth}{.8pt}\vspace{-\baselineskip}% rule at begin of algorithm
...
}
如果没有这一行你可以(或应该)使用
\captionabove{Caption for algorithm 1.}
反而\caption{...}
:
也许你不希望环境algorithm
浮动(因为你正在使用floatpos=h
)。那么你可以使用选项nonfloat
和环境algorithm-
:
\documentclass{scrbook}
\usepackage{algpseudocode}
\usepackage{xpatch}
\DeclareNewTOC[
type=algorithm,
nonfloat,
name=Algorithm,
counterwithin=chapter,
atbegin={%
%\noindent\rule{\linewidth}{.8pt}\vspace{-\baselineskip}% rule at begin of algorithm
\xpretocmd\algorithmic
{\noindent\rule[\ht\strutbox]{\linewidth}{.8pt}\vspace{-\baselineskip}}
{}{\PatchFailed}%
\xapptocmd\endalgorithmic
{\noindent\rule[\ht\strutbox]{\linewidth}{.8pt}\vspace{-\baselineskip}}
{}{\PatchFailed}%
}
]
{loalg}
\usepackage{blindtext}% dummy text
\begin{document}
\chapter{Chapter Heading}
\label{ch:1}
\blindtext
\begin{algorithm-}
\captionabove{Caption for algorithm 1.}
\label{alg:alg1}
\begin{algorithmic}[0]
\For{$i = 0$ to $10$}
\If{$i$ is odd}
\State $x_i \gets 1$
\EndIf
\EndFor
\end{algorithmic}
\end{algorithm-}
\blindtext
\begin{algorithmic}[0]
\For{$i = 0$ to $10$}
\If{$i$ is odd}
\State $x_i \gets 1$
\EndIf
\EndFor
\end{algorithmic}
\end{document}
更新(因为下面的评论)
如果新环境的所有标题都应使用像浮动上方标题那样的间距,则可以使用atbegin
选项为该新环境\DeclareNewTOC
设置 KOMA 选项。captions=heading
\DeclareNewTOC[...,
atbegin={\KOMAoptions{captions=heading}%
...},
...]{loalg}
\caption
然后就可以使用\captionabove
来获得相同的间距。
代码:
\documentclass{scrbook}
\usepackage{algpseudocode}
\usepackage{xpatch}
\DeclareNewTOC[
type=algorithm,
float,
floatpos=h,
name=Algorithm,
counterwithin=chapter,
atbegin={%
\KOMAoptions{captions=heading}% <- added
\xpretocmd\algorithmic
{\noindent\rule[\ht\strutbox]{\linewidth}{.8pt}\vspace{-\baselineskip}}
{}{\PatchFailed}%
\xapptocmd\endalgorithmic
{\noindent\rule[\ht\strutbox]{\linewidth}{.8pt}\vspace{-\baselineskip}}
{}{\PatchFailed}%
}
]
{loalg}
\usepackage{blindtext}% dummy text
\begin{document}
\chapter{Chapter Heading}
\label{ch:1}
\blindtext
\begin{algorithm}
\caption{Caption for algorithm 1.}% <- caption with spacing like captionabove
\label{alg:alg1}
\begin{algorithmic}[0]
\For{$i = 0$ to $10$}
\If{$i$ is odd}
\State $x_i \gets 1$
\EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\blindtext
\begin{algorithmic}[0]
\For{$i = 0$ to $10$}
\If{$i$ is odd}
\State $x_i \gets 1$
\EndIf
\EndFor
\end{algorithmic}
\end{document}