5

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}
  • 2
    Member for more than two years, seven questions, and no MWE: -1 – Henri Menke 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. – jpmath Dec 26 '16 at 17:19
8

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.