![从算法包中删除顶级规则](https://linux22.com/image/291267/%E4%BB%8E%E7%AE%97%E6%B3%95%E5%8C%85%E4%B8%AD%E5%88%A0%E9%99%A4%E9%A1%B6%E7%BA%A7%E8%A7%84%E5%88%99.png)
我怎样才能从算法包中仅删除标题上方的顶部规则?因此,不要删除算法上方和下方的两条规则。
我借用了以下小例子:http://www.latex-community.org/forum/viewtopic.php?f=4&t=15388
问题删除算法标题下方的规则 - 使用 Algorithm2e类似,只是它适用于 algorithm2e 包,并且解决方案不起作用。
\documentclass{article}
\usepackage[noend]{algorithmic}
\usepackage{algorithm}
\begin{document}
\begin{algorithm}
\caption{Iterate} \label{alg:it}
\begin{algorithmic}
\STATE n = 1
\FOR { $i=1$ \TO $n$ }
\STATE $print(i)$
\ENDFOR
\end{algorithmic}
\end{algorithm}
\end{document}
- 编辑 -
egreg 提供的解决方案似乎无法与 floatrow 包结合使用。我该如何解决这个问题?
\documentclass{report}
% url: http://www.ctan.org/pkg/floatrow
\RequirePackage{floatrow}
% url: http://www.ctan.org/pkg/algorithms
\RequirePackage{algorithm}
% url: http://www.ctan.org/pkg/algorithmicx
\RequirePackage{algorithmicx}
% url: http://www.ctan.org/pkg/algpseudocode
\RequirePackage[noend]{algpseudocode}
% url: http://www.ctan.org/pkg/listings
\RequirePackage{listings}
% url: http://www.ctan.org/pkg/etoolbox
\RequirePackage{etoolbox}
%% Setup list of algorithms
% ref: https://tex.stackexchange.com/questions/115023/combine-listoflistings-and
% -listofalgorithms-into-one-list
\newcommand{\algCaption}[1]{%
\caption{#1}
\addcontentsline{lol}{lstlisting}{\protect\numberline{\thealgorithm}#1}
\addtocounter{lstlisting}{1}
}
\AtBeginEnvironment{lstlisting}{\addtocounter{algorithm}{1}}
% ref: https://tex.stackexchange.com/questions/65993/algorithm-numbering
\renewcommand{\thealgorithm}{\arabic{chapter}.\arabic{algorithm}}
\renewcommand{\lstlistingname}{Algorithm}
\renewcommand{\lstlistlistingname}{List of Algorithms}
%% Setup source code formatting
\lstset{%
basicstyle=\ttfamily\scriptsize,
numbers=left,
numberstyle=\tiny,
stepnumber=1,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=tb,
tabsize=2,
captionpos=t,
breaklines=true,
breakatwhitespace=false,
title=\lstname,
xleftmargin=12pt,
framexleftmargin=12pt
}
\makeatletter
\newcommand\fs@ruled@notop{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
%\def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}% <----removed
\def\@fs@pre{}%
\def\@fs@post{\kern2pt\hrule\relax}%
\def\@fs@mid{\kern2pt\hrule\kern2pt}%
\let\@fs@iftopcapt\iftrue}
\renewcommand\fst@algorithm{\fs@ruled@notop}
\makeatother
\begin{document}
\chapter{test}
\begin{algorithm}
\algCaption{Iterate} \label{alg:it}
\begin{algorithmic}[1]
\Require{$x$ and $y$ are packed DNA strings of equal length $n$}
\Statex
\Function{Distance}{$x, y$}
\State $z$ $\gets$ $x \oplus y$ \Comment{$\oplus$: bitwise exclusive-or}
\State $\delta$ $\gets$ $0$
\For{$i \gets 1 \textrm{ to } n$}
\If{$z_i \neq 0$}
\State $\delta$ $\gets$ $\delta + 1$
\EndIf
\EndFor
\State \Return{$\delta$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}
答案1
您必须定义一种新的浮动样式。
\documentclass{article}
\usepackage[noend]{algorithmic}
\usepackage{algorithm}
\makeatletter
\newcommand\fs@ruled@notop{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
%\def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}% <----removed
\def\@fs@pre{}%
\def\@fs@post{\kern2pt\hrule\relax}%
\def\@fs@mid{\kern2pt\hrule\kern2pt}%
\let\@fs@iftopcapt\iftrue}
\renewcommand\fst@algorithm{\fs@ruled@notop}
\makeatother
\begin{document}
\begin{algorithm}
\caption{Iterate} \label{alg:it}
\begin{algorithmic}
\STATE n = 1
\FOR { $i=1$ \TO $n$ }
\STATE $print(i)$
\ENDFOR
\end{algorithmic}
\end{algorithm}
\end{document}
答案2
@egreg 的答案是正确的。但是它对我不起作用,因为我使用的是 floatrow 包,而我的最小示例中没有包含该包。
要在使用 floatrow 包时从算法环境中删除顶部规则,可以使用:
\DeclareFloatStyle{rulednotop}{midcode=rule,postcode=lowrule,capposition=top,heightadjust=all}
\floatsetup[algorithm]{style=rulednotop}