10

How can I remove only the top rule, which is above the caption, from the algorithm package? So not the two rules which are above and below the algorithm.

I borrowed the mini example from: http://www.latex-community.org/forum/viewtopic.php?f=4&t=15388

And the question Remove the rule below the algorithm title - using Algorithm2e is similar only it is for the algorithm2e package and the solution didn't work.

\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}

--edit--

The solution provided by egreg does not seem to work in combination with the floatrow package. How do I solve this?

\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}

2 Answers 2

10

You have to define a new float style.

\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}

enter image description here

2
  • How strange the minimal example works. But I don't get it working in my class file. ! LaTeX Error: \fst@algorithm undefined. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.125 \renewcommand\fst@algorithm {\fs@ruled@notop}
    – WG-
    Commented May 25, 2014 at 19:31
  • the problem seems to come from the floatrow package which I also use.
    – WG-
    Commented May 25, 2014 at 21:22
1

The answer of @egreg is correct. However it did not work for me since I was using the floatrow package, which I did not include in my minimal example.

To remove the top rule from the algorithm environment when using the floatrow package you can use:

\DeclareFloatStyle{rulednotop}{midcode=rule,postcode=lowrule,capposition=top,heightadjust=all}
\floatsetup[algorithm]{style=rulednotop}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .