9

I want to get two algorithm2e environments side by side aligned at the top. I tried it with two minipages, but it does not work. How do I get them side by side in the normal text (not in a floating environment)?

Here is a MWE:

\documentclass{article}
\usepackage[ruled,noend]{algorithm2e}
\begin{document}

\begin{minipage}{5cm}
  \begin{algorithm}[H]
    \caption{Algo 1}
    line 1\;
    line 2\;
  \end{algorithm}
\end{minipage}%
\begin{minipage}{5cm}
  \begin{algorithm}[H]
    \caption{Algo 1}
    line 1\;
  \end{algorithm}
\end{minipage}

\end{document}
2
  • Add \vspace{0pt} at the beginning of each minipage environment. Related to tex.stackexchange.com/questions/11630/…
    – jub0bs
    Commented Dec 1, 2013 at 21:42
  • That doesn't work for me. ...\begin{minipage}{5cm} \vspace{0pt}\begin{algorithm}[H]...
    – Ivo
    Commented Dec 1, 2013 at 21:45

2 Answers 2

11

You need the [t] flag and the magic '\vspace{0pt}:

\documentclass{article}
\usepackage[ruled,noend]{algorithm2e}
\begin{document}

\begin{minipage}[t]{5cm}
  \vspace{0pt}  
  \begin{algorithm}[H]
    \caption{Algo 1}
    line 1\;
    line 2\;
  \end{algorithm}
\end{minipage}%
\begin{minipage}[t]{5cm}
  \vspace{0pt}
  \begin{algorithm}[H]
    \caption{Algo 1}
    line 1\;
  \end{algorithm}
\end{minipage}

\end{document}
2
  • This also adds a blank line (or a vertical space of a line) at the top of the minipage (before the minipage).
    – Ivo
    Commented Dec 1, 2013 at 22:04
  • @Ivo Then make the vspaces negative. There is no text before the algorithms in your question, so we didn't know.
    – Alex
    Commented Dec 1, 2013 at 22:11
3

A bit different approach:

\documentclass{article}
\usepackage[ruled,noend]{algorithm2e}

\begin{document}

\begin{minipage}[t]{5cm}
\null 
 \begin{algorithm}[H]
    \caption{Algo 1}
    line 1\;
    line 2\;
  \end{algorithm}
\end{minipage}%
\begin{minipage}[t]{5cm}
\null
 \begin{algorithm}[H]
    \caption{Algo 1}
    line 1\;
  \end{algorithm}
\end{minipage}


\end{document}

enter image description here

1
  • This adds a blank line (or a vertical space of a line) at the top of the minipage (before the minipage).
    – Ivo
    Commented Dec 1, 2013 at 22:00

You must log in to answer this question.

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