5

I'm currently using the following mwe

\documentclass{article}
\usepackage{algpseudocode}
\usepackage[plain]{algorithm}
\begin{document}
\begin{algorithm}[H]
\hrule
\vspace{0.5em}
\caption{Euclide's algorithm}\label{euclid}
\begin{algorithmic}[5]
\Procedure{Euclide}{$a,b$}\Comment{The g.c.d. of a and b}
   \State $r\gets a\bmod b$
   \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
   \EndWhile\label{euclidendwhile}
   \State \Return $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\hrule
\end{algorithm}
\end{document}

To obtain a mix of plain and ruled style (notice the hrule). I like ruled for the separation to the text it induces, I like plain for the respect of the caption style across my documents. See pic. enter image description here

Is there an easy way to redefine the plain environment ? I've tried to use the answer to this question

\makeatletter
\let\oldalgorithmic\algorithmic
\def\algorithmic{\@ifnextchar[\algorithmic@i \algorithmic@ii}
  \def\algorithmic@i[#1]{\hrule\vspace{0.5em}\oldalgorithmic[#1]\hrule}
  \def\algorithmic@ii[#1]{\hrule\vspace{0.5em}\oldalgorithmic\hrule}
\makeatother

But this doesn't work as expected since the second hrule still comes top of the algorithm. Am I missing something ?

1 Answer 1

7

algorithm uses the float package to style the algorithm environment using \floatstyles. Here are the float styles for plain and ruled:

% The 'plain' float style
\newcommand\fs@plain{\def\@fs@cfont{\rmfamily}\let\@fs@capt\floatc@plain
  \def\@fs@pre{}\def\@fs@post{}%
  \def\@fs@mid{\vspace\abovecaptionskip\relax}%
  \let\@fs@iftopcapt\iffalse}
% The 'ruled' float style
\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}

Each float style defines a number of components used in constructing the float. Consider the ruled float style: A pre- and post-component surrounds the \caption, while the mid-components ends off the float.

Using this as foundation, we can create a new float style, say plainruled that has a flavour of both:

enter image description here

\documentclass{article}
\usepackage{algpseudocode,algorithm}

\makeatletter
% The 'plainruled' float style
\newcommand\fs@plainruled{\def\@fs@cfont{\rmfamily}\let\@fs@capt\floatc@plain
  \def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}%
  \def\@fs@post{}%
  \def\@fs@mid{\kern2pt\hrule height.8pt depth0pt\relax\kern\abovecaptionskip}%
  \let\@fs@iftopcapt\iffalse}
\makeatother

\floatstyle{plainruled}
\restylefloat{algorithm}

\begin{document}

\begin{algorithm}[H]
  \caption{Euclide's algorithm}\label{euclid}
  \begin{algorithmic}[5]
    \Procedure{Euclide}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
       \State $r \gets a \bmod b$
       \While{$r \neq 0$}\Comment{We have the answer if~$r$ is~$0$}
          \State $a \gets b$
          \State $b \gets r$
          \State $r \gets a \bmod b$
       \EndWhile\label{euclidendwhile}
       \State \Return $b$\Comment{The g.c.d.\ is~$b$}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\end{document}

You must log in to answer this question.

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