“Not in outer par mode” error when using Algorithm inside minipage

algorithmsminipage

I am trying to place a figure and an algorithm side-by-side using minipage. Hee is the code for the algorithm part. However, I am getting an error LaTeX Error: Not in outer par mode. on the line \KwData. If I delete it, I am getting the error on the next line \Input

\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\begin{minipage}{0.6\textwidth}
    \centering
    \captionof{algocf}{CAPTION}
\label{alg:alg2}
\begin{algorithm}
\KwData{$ . . .$}
\Input{$ . . . $}
\eIf{$ condition $}{
        $ . . . .  $
    }{
        $ . . . . $
    }
\end{algorithm}
\end{minipage}%

Best Answer

Use the H specifier as mentioned in the manual.

\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{caption}

\SetKwInOut{Input}{Input}

\begin{document}

\noindent
\begin{minipage}{0.58\textwidth}
\captionof{algocf}{CAPTION}
\label{alg:alg2}
\begin{algorithm}[H]
\KwData{$ . . .$}
\Input{$ . . . $}
\eIf{$ condition $}{
        $ . . . .  $
    }{
        $ . . . . $
    }
\end{algorithm}
\end{minipage}\hfill
\begin{minipage}{0.38\textwidth}
\captionof{algocf}{CAPTION}
\label{alg:alg2-bis}
\begin{algorithm}[H]
\KwData{$ . . .$}
\Input{$ . . . $}
\eIf{$ condition $}{
        $ . . . .  $
    }{
        $ . . . . $
    }
\end{algorithm}
\end{minipage}

\end{document}

enter image description here

Related Question