2

I wish to align lines on my algorithm on a certain symbol. For instance:

long_variable_name = k | long_variable_name = k
variable = l           | variable           = l

In terms of LaTeX I have the following example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithm2e}
\begin{document}


% ALGORITHM PARAMETERS %
\SetAlFnt{\small\fontfamily{cmr}}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\RestyleAlgo{boxed}
\LinesNumbered
\IncMargin{1em}

\begin{algorithm}
    \footnotesize
    \DontPrintSemicolon
    \SetKwData{VARA}{VAR$_{old}$}
    \SetKwData{VARB}{VAR$_{new}$}
    \SetKwData{TEMP}{tmp$_{cc}$}
    
    \Input{ A matrix N$\times$M}
    \Output{ A number K}
   
        \BlankLine
    
        \VARA  \ := \ 123 \\
        \VARB  \ := \ 123 \\
        \TEMP  \ := \ 423 \\
    
\end{algorithm}


\end{document}

And I wish to align the lines where VARA VARB and TEMP are assigned their values on the := symbol. Is is possible to do so? Thank you in advance

P.S.:

Here's the output of the code

enter image description here

1 Answer 1

5

You can use eqparbox, this needs two compilations.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithm2e}
\usepackage{eqparbox}
\begin{document}


% ALGORITHM PARAMETERS %
\SetAlFnt{\small\fontfamily{cmr}}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\RestyleAlgo{boxed}
\LinesNumbered
\IncMargin{1em}

\begin{algorithm}
    \footnotesize
    \DontPrintSemicolon
    \SetKwData{VARA}{\eqmakebox[var][l]{VAR$_{\mathit{old}}$}}
    \SetKwData{VARB}{\eqmakebox[var][l]{VAR$_{\mathit{new}}$}}
    \SetKwData{TEMP}{\eqmakebox[var][l]{tmp$_{\mathit{cc}}$}}
    
    \Input{ A matrix N$\times$M}
    \Output{ A number K}

        \BlankLine

        \VARA  \ := \ 123 \\
        \VARB  \ := \ 123 \\
        \TEMP  \ := \ 423 \\

\end{algorithm}


\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.