3

I have written some algorithms using the package algorithm2e. Some of my algorithms also include aligned formulas. My problem is that line numbers below lines ending with equations have a larger indent than the other line numbers. I'd like to have all line numbers aligned. How can I accomplish that?

I use pdflatex.

A minimal working example with what I have tried:

\documentclass[12pt,a4paper,twoside,openright]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[ruled,algochapter,linesnumbered]{algorithm2e}
\DontPrintSemicolon

\SetKwInput{Input}{Input}
\SetKwInput{Output}{Output}
\SetKwProg{Fn}{Function}{: }{end}
\begin{document}
%
\begin{algorithm}[ht]
    \caption{Test algorithm}
    \label{alg:test}
    \Input{$i$.}
    \Output{$x$.}
    Set
    \begin{align*}
        i &\coloneqq i+1, \\
        j &\coloneqq i^2.
    \end{align*}%
    \nl Set $x \coloneqq j$.
\end{algorithm}
%
\begin{algorithm}[ht]
    \caption{Test algorithm 2}
    \label{alg:test2}
    \Input{$i$.}
    \Output{$x$.}
    Set
    \begin{algomathdisplay}
        \begin{aligned}
            i &\coloneqq i+1, \\
            j &\coloneqq i^3. \;
        \end{aligned}
    \end{algomathdisplay}
    Set $x \coloneqq j$. \;
\end{algorithm}
%
\end{document}

1

2 Answers 2

2

The algomathdisplay environment doesn't really work: adding \nl at the end shifts the line number, \; adds a blank line.

Using directly align* is out of the question, as it produces too large space.

I can propose a patch that uses \; and removes the blank line.

\documentclass[12pt,a4paper,twoside,openright]{scrreprt}

\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[ruled,algochapter,linesnumbered]{algorithm2e}
\DontPrintSemicolon

\makeatletter
\renewenvironment{algomathdisplay}
 {\[}
 {\@endalgocfline\vspace{-\baselineskip}\]\;}
\makeatother


\SetKwInput{Input}{Input}
\SetKwInput{Output}{Output}
\SetKwProg{Fn}{Function}{: }{end}

\begin{document}

\begin{algorithm}[ht]
    \caption{Test algorithm 2}
    \label{alg:test2}
    \Input{$i$.}
    \Output{$x$.}
    Set
    \begin{algomathdisplay}
        \begin{aligned}
            i &\coloneqq i+1, \\
            j &\coloneqq i^3.
        \end{aligned}
    \end{algomathdisplay}
    Set $x \coloneqq j$.
\end{algorithm}

\end{document}

enter image description here

I tested it both with and without linesnumbered.

1
  • If \DontPrintSemicolon is not used, this produces a stray semicolon after the equation. This can be fixed by replacing \; in your definition with \hfill\strut\par, see the definition of \@endalgoln (which is used in \;).
    – gerw
    Commented Dec 1, 2021 at 9:22
1

In the first case, the indent can be solved by removing \nl and adding a \; add the end of the first line. For the second case a patch of the algomathdisplay seems necessary.

\documentclass[12pt,a4paper,twoside,openright]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[ruled,algochapter,linesnumbered]{algorithm2e}
\DontPrintSemicolon

\SetKwInput{Input}{Input}
\SetKwInput{Output}{Output}
\SetKwProg{Fn}{Function}{: }{end}

\makeatletter
\renewenvironment{algomathdisplay}{%
    \[%
    }{%
    \@endalgocfline%
    \]%
    %\ifthenelse{\boolean{algocf@linesnumbered}}{\nl}{\relax}% <- in the original .sty
    }%
\makeatother

\begin{document}
%
\begin{algorithm}[ht]
    \caption{Test algorithm}
    \label{alg:test}
    \Input{$i$.}
    \Output{$x$.}
    Set
    \begin{align*}
        i &\coloneqq i+1, \\
        j &\coloneqq i^2.
    \end{align*}\;
    Set $x \coloneqq j$.
\end{algorithm}
%
\begin{algorithm}[ht]
    \caption{Test algorithm 2}
    \label{alg:test2}
    \Input{$i$.}
    \Output{$x$.}
    Set%
    \begin{algomathdisplay}
        \begin{aligned}
            i &\coloneqq i+1, \\
            j &\coloneqq i^3.
        \end{aligned}
    \end{algomathdisplay}\;
    Set $x \coloneqq j$. \;
\end{algorithm}
%
\end{document}

\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 .