GeneralChange how vertical line is rendered in algorithm2e

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
stalecu64
Posts: 2
Joined: Thu Mar 20, 2025 6:06 am

Change how vertical line is rendered in algorithm2e

Post by stalecu64 »

Hello! I am faced with rendering the following pseudocode to LaTeX:
0SWfJCYy.png
0SWfJCYy.png (32.4 KiB) Viewed 5954 times
This is my attempt in LaTeX (for convenience, it has been translated into English):

Code: Select all

\documentclass{article}
\usepackage[vlined]{algorithm2e}

\makeatletter
\renewcommand{\algocf@Hlne}{%
  \rule{.5em}{.4pt}%
  \smash{\rule[\dimexpr-.5ex+.2pt]{1ex}{1ex}}%
}
\makeatother

\SetKw{Read}{read}
\SetKw{Write}{write}
\SetKw{And}{and}
\begin{document}

\begin{algorithm}
  \DontPrintSemicolon
  \Read{$n,\,x,\,y$} (non-zero natural numbers, $x \leq n$, $y \leq n$)\;
  $nr \gets 0$\;
  \While{$i \gets n,\,1,\,-1$}{
    \eIf{$i~\%~x = 0$ \And $i~\%~y \neq 0$}{
        $nr \gets nr + 1$\;
    }{
        \If{$i~\%~x \neq 0$ \And $i~\%~y = 0$}{
            $nr \gets nr + 1$\;
        }
    }
  }
  \Write{nr}\;
\end{algorithm}
with the following output:
BRB6Y7zu.png
BRB6Y7zu.png (37.92 KiB) Viewed 5954 times
However, I don't know how to make the lines like in the given pseudocode. I don't see how I could do it with \algocf@Hlne, because that deals with the horizontal line as I understand it, and looking at how \algocf@Vline is in algorithm2e doesn't provide me any clue. How would I accomplish my goal? To be precise, the line should start from the left of the keyword and go all the way down until where the little box is. Additionally, the line shouldn't cross through any other keywords, such as with if and else there. Also, all keywords on the first level should be aligned, so that the line goes outside of the block instead of offsetting what's inside; in other words, it should be like this:

Code: Select all

 read n,x,y
 nr←0
┌for i←n,1,-1 do
│  write i
│  read x
└■
 write nr
not this:

Code: Select all

read n,x,y
nr←0
┌for i←n,1,-1 do
│  write i
│  read x
└■
write nr
Thanks for your attention. I am open to trying other solutions, however from my research algorithm2e was the only one that let me add a square to the end of the block.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
stalecu64
Posts: 2
Joined: Thu Mar 20, 2025 6:06 am

Re: Change how vertical line is rendered in algorithm2e

Post by stalecu64 »

Hello, does anyone have any idea on how to accomplish this? :)
Post Reply