6

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}

Source: https://www.writelatex.com/examples/euclids-algorithm-an-example-of-how-to-write-algorithms-in-latex/mbysznrmktqf

2 Answers 2

6

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

enter image description here

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

2
  • 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, 2014 at 16:11
  • 1
    @mcb: I've added some more discussion on the overlapping (left/right).
    – Werner
    Nov 27, 2014 at 16:37
4

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

aligned algorithm

\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}
2
  • 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, 2014 at 10:57
  • @egreg - implemented Nov 27, 2014 at 15:51

You must log in to answer this question.

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