3

I am trying to get multiple lines aligned inside an algorithm. So far, my code and outcome looks like this:


\usepackage[german,onelanguage,linesnumbered,ruled]{algorithm2e}

\documentclass[pdftex,12pt,a4paper,twoside,ngerman]{article}
\begin{document}

\begin{algorithm}
\SetAlgoLined
\SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{Eingabe}\SetKwInOut{Output}{Ausgabe}
\SetKwBlock{Loop}{Loop}{Ende}
    
\Loop{
  $h_G \leftarrow zusammenfassen(G_R)$\;
  $P_{Obj} \leftarrow f_{Hinzu}(h_G)$\;
  $ok \leftarrow stichprobe(P_{Obj})$\;
}
\end{algorithm}
\end{document}

Loop without align

However, when I use the align environment like this:

\begin{align*}
  h_G &\leftarrow zusammenfassen(G_R) \\
  P_{Obj} &\leftarrow f_{Hinzu}(h_G) \\
  ok &\leftarrow stichprobe(P_{Obj}) \\
  \end{align*}\;

I get the following outcome which is quite misplaced:

Loop with align

Is there a way to align these lines on \leftarrow without affecting the layout of the algorithm ?

3
  • Did you try using $\begin{aligned} … \end{aligned}$ instead?
    – Bernard
    Dec 16, 2020 at 11:04
  • This works, but the algorithm tracks the three aligned lines as one line, which is bad because i need to reference individual lines in the text explaining the algorithm. Dec 16, 2020 at 11:10
  • 1
    I added the automated version also showing how you can reference lines.
    – egreg
    Dec 16, 2020 at 11:29

1 Answer 1

5

If you want line numbers for each assignment, you need to guess the width of the largest object.

\documentclass[pdftex,12pt,a4paper,twoside,ngerman]{article}
\usepackage[german,onelanguage,linesnumbered,ruled]{algorithm2e}

\newcommand{\ali}[2]{\makebox[#1][r]{#2}}

\begin{document}

\begin{algorithm}
\SetAlgoLined
\SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{Eingabe}\SetKwInOut{Output}{Ausgabe}
\SetKwBlock{Loop}{Loop}{Ende}
    
\Loop{
  \ali{2em}{$h_G$}              ${}\leftarrow \mathit{zusammenfassen}(G_R)$\;
  \ali{2em}{$P_{\mathrm{Obj}}$} ${}\leftarrow f_{\mathrm{Hinzu}}(h_G)$\;
  \ali{2em}{$\mathit{ok}$}      ${}\leftarrow \mathit{stichprobe}(P_{\mathrm{Obj}})$\;
}
\end{algorithm}

\end{document}

enter image description here

Can this be automated? Yes, of course.

\documentclass[pdftex,12pt,a4paper,twoside,ngerman]{article}
\usepackage[german,onelanguage,linesnumbered,ruled]{algorithm2e}

\ExplSyntaxOn
\NewDocumentEnvironment{alignassign}{b}
 {
  \tilman_alignassign:n { #1 }
 }{}

\seq_new:N \l__tilman_alignassign_body_seq
\dim_new:N \l__tilman_alignassign_wd_dim

\cs_new_protected:Nn \tilman_alignassign:n
 {
  \seq_set_split:Nnn \l__tilman_alignassign_body_seq { \\ } { #1 }
  \dim_zero:N \l__tilman_alignassign_wd_dim
  \seq_map_inline:Nn \l__tilman_alignassign_body_seq
   {
    \__tilman_alignassign_measure:nn ##1
   }
  \seq_map_inline:Nn \l__tilman_alignassign_body_seq
   {
    \__tilman_alignassign_print:nn ##1
   }
 }
\cs_new_protected:Nn \__tilman_alignassign_measure:nn
 {
  \hbox_set:Nn \l_tmpa_box { $#1$ }
  \dim_set:Nn \l__tilman_alignassign_wd_dim
   {
    \dim_max:nn { \l__tilman_alignassign_wd_dim } { \box_wd:N \l_tmpa_box }
   }
 }
\cs_new_protected:Nn \__tilman_alignassign_print:nn
 {
  \makebox[\l__tilman_alignassign_wd_dim][r]{ $#1$ }
  ${}\leftarrow #2$\;
 }

\ExplSyntaxOff

\begin{document}

\begin{algorithm}
\SetAlgoLined
\SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{Eingabe}\SetKwInOut{Output}{Ausgabe}
\SetKwBlock{Loop}{Loop}{Ende}
    
\Loop{
  \begin{alignassign}
  {h_G}{\mathit{zusammenfassen}(G_R)}\\
  {P_{\mathrm{Obj}}}{f_{\mathrm{Hinzu}}(h_G)}\\
  {\mathit{ok}}{\mathit{stichprobe}(P_{\mathrm{Obj}})\label{third}}
  \end{alignassign}
}
\end{algorithm}

The assignment in line~\ref{third} is important.

\end{document}

You may need to add \usepackage{xparse} if you don't have LaTeX released 2020-10-01.

enter image description here

You must log in to answer this question.

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