6

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}
3
  • 2
    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?
    – CroCo
    Dec 26 '16 at 17:10
  • I updated the question, sorry for the missing information.
    – user61658
    Dec 26 '16 at 17:19
10

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

1
  • 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.
    – jwalton
    Jan 24 '20 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy