# Algorithm / algorithmicx horizontal lines in booktabs style

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.

\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}
• Member for more than two years, seven questions, and no MWE: -1 Dec 26 '16 at 16:45
• Well, good luck with that but what is exactly your question? Dec 26 '16 at 17:10
• I updated the question, sorry for the missing information.
– user61658
Dec 26 '16 at 17:19

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}

• I was getting some interaction between captionsetup, some other bits of my preamble (couldn't quite pin down exactly what), and this solution. This interaction resulted in the algorithm's caption being centered (which was undesired). If anyone else has this problem, I found that setting \captionsetup[algorithm]{singlelinecheck=off} fixed it. Jan 24 '20 at 10:54