4

I would like to put a <- b and e <- f, in parallel on a single line in an algorithmicx environment. I want the same with c <- d and g <- h. Then I want to align both lines. The output shouod look like this.

1.    a <- b            e <- f
2.    c <- d            g <- h

This is the code I have now:

\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{Test}{}
    \State{$a \gets b$}
    \State{$e \gets f$}
    \State{$c \gets d$}
    \State{$g \gets h$}
\EndProcedure
\end{algorithmic}
\end{algorithm}

Does anyone know how to do this? Could I use the multicolumn environment perhaps?

1

2 Answers 2

4

You can define a new command \BiState which takes two arguments

\newcommand{\BiState}[2]{%
  \State{\makebox[2cm]{#1\hfill}#2}%
  }

and use it like this

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\newcommand{\BiState}[2]{%
  \State{\makebox[2cm]{#1\hfill}#2}%
  }

\begin{document}

\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{Test}{}
    \BiState{$a \gets b$}{$e \gets f$}
    \BiState{$c \gets d$}{$g \gets h$}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document} 

Result

enter image description here

Adjust 2cm in its definition to the value that better fits your requirements.

1
  • thanks just what I needed. Your solution works like a charm!
    – Luc
    Jan 22, 2014 at 15:47
2

One solution is:

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{Test}{}
    \State{$a \gets b \qquad e \gets f$}
    \State{$c \gets d \qquad g \gets h$}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

Output: enter image description here

1
  • thanks for your suggestion. However, I have many lines in my algorithm. Consequently, this approach will make it very difficult to align all lines.
    – Luc
    Jan 22, 2014 at 13:17

You must log in to answer this question.

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