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}
Ivo
  • 977
  • Add \vspace{0pt} at the beginning of each minipage environment. Related to http://tex.stackexchange.com/questions/11630/aligning-image-and-text-on-top-with-minipages – jub0bs Dec 01 '13 at 21:42
  • That doesn't work for me. ...\begin{minipage}{5cm} \vspace{0pt}\begin{algorithm}[H]... – Ivo Dec 01 '13 at 21:45

2 Answers2

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}
Alex
  • 5,362
  • This also adds a blank line (or a vertical space of a line) at the top of the minipage (before the minipage). – Ivo Dec 01 '13 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 Dec 01 '13 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

  • This adds a blank line (or a vertical space of a line) at the top of the minipage (before the minipage). – Ivo Dec 01 '13 at 22:00