# How to make the pseudo code more compact?

I wrote the following piece of pseudo code. It shows the result as shown in the figure below. I think there is too much white space above the horizontal line in the picture. I want to make them more compact. Just like the bottom picture, what should I do? Thank you!

\documentclass{article}

\usepackage{listings}
%\usepackage{caption,subcaption}
\usepackage{subfig}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{algorithmic}
\usepackage[ruled,lined]{algorithm2e}
\usepackage{lipsum}
\begin{document}
\lipsum[1-2]
\begin{algorithm} [t]
\caption{Operations }
line 0-N : cache lines

m : 1-bit information

$M[k]$ : k bits message

\hrulefill  \newline
\textbf{Encoding:}

\hrulefill  \newline
// Encoding Phase \newline
\eIf{m==1}{
print a
}{
print b
}

sleep; // Allow the receiver code to decode

\end{algorithm}

\end{document}


• Try with \myrule defined as follows: \newcommand{\myrule}{\vspace{-0.7\baselineskip}\hrulefill\vspace{-0.1\baselineskip}}. Adjust the values according to your needs
– Ivan
Apr 26, 2021 at 8:26
• thank you very much. This is working. Apr 26, 2021 at 8:39
• @Ivan I guess you can post that as an answer (maybe with a bit of extra explanation). Apr 27, 2021 at 12:50

You can define a command \myrule as follows:

\newcommand{\myrule}{\vspace{-0.6\baselineskip}\hrulefill\vspace{-0.2\baselineskip}}


Note that the values are determined empirically. So if you change the font, you should probably adjust them.

MWE

\documentclass{article}
\usepackage[ruled,lined]{algorithm2e}

\newcommand{\myrule}{\vspace{-0.6\baselineskip}\hrulefill\vspace{-0.2\baselineskip}}

\begin{document}

\begin{algorithm} [t]
\caption{Operations }
line 0-N : cache lines

m : 1-bit information

$M[k]$ : k bits message

\myrule

\textbf{Encoding:}

\myrule

// Encoding Phase \newline
\eIf{m==1}{
print a
}{
print b
}

sleep; // Allow the receiver code to decode

\end{algorithm}

\end{document}


Consider using the appropriate algorithm2e syntax, ending lines with \; and using \DontPrintSemicolon if you don't want them printed. Also, there's no need to load algorithmic as algorithm2e provides all that's needed to produce what you want.

Here's an option using a tabular for the "headings":

\documentclass{article}

\usepackage[ruled,lined]{algorithm2e}
\DontPrintSemicolon

\rlap{\begin{tabular}{ @{} p{\dimexpr\linewidth-\algomargin} @{} }
\hline
\bfseries\strut #1:\strut \\
\hline
\end{tabular}}%
}

\begin{document}

\begin{algorithm}[t]
\caption{Operations}
line $0$-$N$ : cache lines\;
$m$ : 1-bit information\;
$M[k]$ : $k$ bits message\;
// Encoding Phase\;
\eIf{$m = 1$}{
print a
}{
print b
}
sleep // Allow the receiver code to decode\;
\end{algorithm}

\end{document}