# How can I use an align environment or tabbing in the algorithm environment?

I would like to have these expressions aligned so that the kerning and slightly different character widths do not result in an uneven overall appearance. I tried using an align environment within algorithm but it resulted in errors. Here is a MWE:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $m\gets l$
\State $l\gets r$
\State $r\gets m\bmod l$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

This is very similar in nature to John's answer, using mathtools's math overlapping tools:

\documentclass{article}

\usepackage{mathtools,algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Euclid's algorithm}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d.\ of~$m$ and~$l$}
\State $r\gets m\bmod l$
\While{$r \neq 0$}\Comment{We have the answer if~$r$ is $0$}
\State $m \gets l$
\State $\hphantom{m}\mathllap{l} \gets r$
\State $\hphantom{m}\mathllap{r} \gets m\bmod l$
\EndWhile
\While{$r \neq 0$}\Comment{We have the answer if~$r$ is $0$}
\State $m \gets l$
\State $\mathrlap{l}\hphantom{m} \gets r$
\State $\mathrlap{r}\hphantom{m} \gets m\bmod l$
\EndWhile
\State \textbf{return} $l$\Comment{The g.c.d.\ is~$l$}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

Left- or right-alignment is easily achieved with appropriate placement of \phantoms, together with an accompanying overlapping: \mathllap = left overlap; \mathrlap = right overlap.

• Can I left-align the phantom wrapped expressions? I tried \mathrlap but that results in overdraw: i.imgur.com/zbNxECp.png – Lenar Hoyt Nov 27 '14 at 16:11
• @mcb: I've added some more discussion on the overlapping (left/right). – Werner Nov 27 '14 at 16:37

If you really want to do it, you can use \makebox set to a constant width.

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\newlength{\maxwidth}
\newcommand{\algalign}[2]% #1 = text to left, #2 = text to right
{\makebox[\maxwidth][r]{$#1{}$}${}#2$}

\begin{document}

\begin{algorithm}
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\settowidth{\maxwidth}{$m$}% use the widest one
\State \algalign{m}{\gets l}
\State \algalign{l}{\gets r}
\State \algalign{r}{\gets m\bmod l}
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}
• Rather than \null, use {}. They have the same effect, but \null is undocumented and using it in some places can have very surprising effects. – egreg Nov 27 '14 at 10:57
• @egreg - implemented – John Kormylo Nov 27 '14 at 15:51