2

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}

enter image description here

enter image description here

3
  • 1
    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.
    – Yujie
    Apr 26, 2021 at 8:39
  • @Ivan I guess you can post that as an answer (maybe with a bit of extra explanation).
    – Marijn
    Apr 27, 2021 at 12:50

2 Answers 2

1

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}

enter image description here

1

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":

enter image description here

\documentclass{article}

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

\newcommand{\algorithmheading}[1]{%
  \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\;
    \algorithmheading{Encoding}
    // Encoding Phase\;
    \eIf{$m = 1$}{
      print a
    }{
      print b
    }
    sleep // Allow the receiver code to decode\;
\end{algorithm}  

\end{document}

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.