4

I have the following code :

\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{mathtools}

\begin{algorithm}
\caption{XEdDsa Sign}
\SetAlgoLined
\DontPrintSemicolon

$A,a \gets functionCall(5)$\;
$\hphantom{A,a}\mathllap{v} \gets hash(a)$\;
$\hphantom{A,a}\mathllap{b} \gets 15$\;
\textbf{return} $whatever$

\end{algorithm}

However, when this compiles i get:
enter image description here

I really dislike that v and b are not left aligned here. Is there any way to fix this so that they are aligned to the left?

3
  • 1
    Does \mathrlap{v}\hphantom{A,a} \gets hash(a) result in the expected output?
    – leandriis
    Jun 14, 2021 at 17:52
  • also don't use math italic for multi-letter identifiers, it is designed to make adjacent letters separate as a prodcut of variables not a word. See spacing around fand C in particular. Jun 14, 2021 at 17:58
  • 1
    Avoid loading conflicting algorithm packages. Either algorithm2e or algpseudocode (together with algorithm), but not both.
    – Werner
    Jun 14, 2021 at 18:35

2 Answers 2

4

Here's a solution that employs suitably defined \parbox directives. It also uses \mathit to type "functionCall", "hash", and "whatever".

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{mathtools,calc}
\newcommand\myAAbox[1]{\parbox{\widthof{$A,a$}}{$#1$}} % <-- new

\begin{document}
\begin{algorithm}
\caption{XEdDsa Sign}
\SetAlgoLined
\DontPrintSemicolon

$A,a         \gets \mathit{functionCall}(5)$\;
$\myAAbox{v} \gets \mathit{hash}(a)$\;
$\myAAbox{b} \gets 15$\;
\textbf{return} $\mathit{whatever}$

\end{algorithm}
\end{document}
2

You can have a generic solution with the eqparbox package: I defined an \eqmathboxcommand with a left-aligned contents, using a tag rather than a length, and all boxes sharing the same tag have the natural width of the largest contents:

\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{mathtools}
\usepackage{eqparbox}
\newcommand{\eqmathbox}[2][A]{\eqmakebox[#1][l]{$\displaystyle#2$}}

\begin{document}

\begin{algorithm}
\caption{XEdDsa Sign}
\SetAlgoLined
\DontPrintSemicolon

$\eqmathbox{A,a} \gets \operatorname{functionCall}(5)$\;
$\eqmathbox{v} \gets \operatorname{hash}(a)$\;
$\eqmathbox{b} \gets 15$\;
\textbf{return} $whatever$
\end{algorithm}

\end{document} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .