[Tex/LaTex] Wrap text around pseudocode

algorithmswrapfigure

I use algorithm/algorithmic packages to write some pseudocode. The width of the box spans across the entire page and I was wondering if there is any way to constrain it (since the actual code doesn't exceed the mid of the page). Practically, I want to wrap text around my pseudocode.

ps: I tried wrapfig package, but I had some problems. Would be also helpful if anyone could verify whether this approach works.

Any ideas ?

Best Answer

You can put the algorithm inside of a minipage, put that inside of the wrapfigure, and it should work like a charm.

\documentclass{article}
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{wrapfig}
\usepackage{lipsum}
\begin{document}
  \lipsum[1]
  \begin{wrapfigure}{L}{0.5\textwidth}
    \begin{minipage}{0.5\textwidth}
      \begin{algorithm}[H]
        \caption{assignment algorithm}
        \begin{algorithmic}
          \STATE i $\leftarrow$ j
        \end{algorithmic}
      \end{algorithm}
    \end{minipage}
  \end{wrapfigure}
  \lipsum
\end{document}

And the result:

Wrapped algorithm

You might want to tweak the alignment modifier on the minipage to get vertical alignment to work out a little better. Alternatively, you can play with the optionial lineheight of wrapfigure.

Related Question