[Tex/LaTex] Algorithm / algorithmicx horizontal lines in booktabs style

algorithmicxalgorithmsbooktabs

I am using packages algorithm, algorithmicx and booktabs.

There are horizontal lines for both algorithms and tables. As you can see, the lines have different line strength and spacing.

I would like the horizontal lines and the spacing surrounding the algorithm and its caption to match exactly the beautiful design from booktabs.

enter image description here

\documentclass{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{booktabs}

\begin{document}

\begin{algorithm}
\caption{Euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$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 \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\begin{table}
\centering
\begin{tabular}{ccc}
\toprule
First name & Last Name & Age \\
\midrule
John & Doe & 30 \\
John & Doe & 30 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

Best Answer

Nice idea!

We can create a new float style by modifying ruled:

\documentclass{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{booktabs}

\makeatletter
\newcommand\fs@booktabsruled{%
  \def\@fs@cfont{\bfseries\strut}\let\@fs@capt\floatc@ruled
  \def\@fs@pre{\hrule height\heavyrulewidth depth0pt \kern\belowrulesep}%
  \def\@fs@mid{\kern\aboverulesep\hrule height\lightrulewidth\kern\belowrulesep}%
  \def\@fs@post{\kern\aboverulesep\hrule height\heavyrulewidth\relax}%
  \let\@fs@iftopcapt\iftrue
}
\makeatother

\floatstyle{booktabsruled}
\restylefloat{algorithm}

\begin{document}

\begin{algorithm}
\caption{Euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$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 \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\begin{table}
\centering
\begin{tabular}{ccc}
\toprule
First name & Last Name & Age \\
\midrule
John & Doe & 30 \\
John & Doe & 30 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

enter image description here