In the following MWE, I would like the rules (the gray lines above and below the floats) of both environments (listings
and algorithmicx
, resp. algpseudocode
style) to have visually (I'm not aiming for pt perfection) the same indentation relative to the line numbers and the text margins.
Personally, I prefer my current listings
setting. So unless I unintentionally broke some typographic rule (no pun intended), I would like to make the algorithmicx
float to conform to the code listing.
One picture says it all
MWE
Yes, it's a bit long...
\documentclass{memoir}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{xcolor}
\usepackage{listings}
% algorithm settings
\makeatletter
\newcommand\fs@plainruled{\def\@fs@cfont{\rmfamily}\let\@fs@capt\floatc@plain
\def\@fs@pre{{\color{gray}\hrule height.8pt depth0pt \kern2pt}}%
\def\@fs@post{}%
\def\@fs@mid{{\kern2pt\color{gray}\hrule height.8pt depth0pt\relax\kern\abovecaptionskip}}%
\let\@fs@iftopcapt\iffalse}
\makeatother
\floatstyle{plainruled}
\restylefloat{algorithm}
\algrenewcommand{\alglinenumber}[1]{\color{gray}\tiny#1}
% listings settings
\lstset{
numbers=left,numberstyle=\color{gray}\tiny,numbersep=1em,
showspaces=false,showtabs=false,showstringspaces=false,
frame=tb,tabsize=4,rulecolor=\color{gray},
xleftmargin=1.5em,
breaklines=true,breakatwhitespace=true
}
\begin{document}
\begin{algorithm}
\caption{K-Means algorithm}
\label{alg:k-means-orig}
\begin{algorithmic}[1]
\State select $K$ observations randomly as initial cluster centroids
\Repeat
\State assign each observation to the closest centroid
\State recalculate cluster positions
\Until{cluster centroids do not move anymore}
\end{algorithmic}
\end{algorithm}
\begin{lstlisting}[float,caption={A blind text code listing},label={lst:blind}]
for(k=0; k<N; k++) {
if(k%2=0) {
tmp = foo[k];
foo[k] = foo[k+1];
foo[k+1] = tmp;
}
}
\end{lstlisting}
\end{document}