2

I'd like to change the style of the line that runs along the side of a statement block in a pseudocode algorithm typeset using the algorithm environment of the algorithm2e package. Here's the initial example code:

\documentclass{article}
\usepackage[ruled,noend]{algorithm2e}
\begin{document}
  \begin{algorithm}
    \caption{A simple algorithm.}
    \Begin
    {
      \ForEach{item}
      {
        \ForEach{sub-item}
        {
          do something.\\
        }
      }
    }
  \end{algorithm}
\end{document}

This produces:

initial line style

But I'd like to add little endcaps on the lines and adjust the spacing a bit. Here's what I'd like to achieve:

desired style

1 Answer 1

2

You can adjust the vlined package option draws the vertical and horizontal rule:

enter image description here

\documentclass{article}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{etoolbox}

\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\algocf@Vline}{\vrule}{\vrule\hspace{-0.25em}}{}{}
\makeatother

\begin{document}

\begin{algorithm}
  \caption{A simple algorithm.}
  \Begin
  {
    \ForEach{item}
    {
      \ForEach{sub-item}
      {
        do something.\\
      }
    }
  }
\end{algorithm}

\end{document}

The idea behind this solution is to draw the vertical rule, and then back up half the width of the regular horizontal rule (default width is 0.5em), before setting the rest of the code blocks.

One can adjust the width of the little horizontal rule as well, if need be.

This should suffice for most uses.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.