[Tex/LaTex] Floating text around listing

floatslistings

I can't manage to let my text float around an listing. Assuming I have a text paragraph and I want the listing to be at the top right place with a certain width, and to be floated by the text.

\documentclass[letterpaper]{article}

\usepackage{listings}
\usepackage{blindtext}

\begin{document}

\blindtext

\begin{lstlisting} % this should be printed to the right of the text
public void Main (string[] args)
{
   // here goes some code
}
\end{lstlisting}

\end{document}

How can I do this?

Solution according to Guido's answer:

\begin{wrapfigure}[6]{r}{.6\textwidth}
\begin{minipage}{.6\textwidth}
\vspace{-2em}
\begin{lstlisting}
....
\end{lstlisting}
\end{minipage}
\end{wrapfigure}

With three modifications:

  • My listings produces captions with a box that is spanned over the whole textwidth, so I added a minipage.
  • The listing was a bit to far below, so I added a vspace.
  • The first optional parameter for wrapfigure specifies the number of lines the wrapfigure occupies (based on this answer: https://tex.stackexchange.com/a/285203/198066)

Best Answer

If I have understood correctly, you can use the wrapfig package (see textdoc wrapfig for the documentation).

\documentclass[letterpaper]{article}

\usepackage{listings}
\usepackage{blindtext}
\usepackage{wrapfig}

\begin{document}

\begin{wrapfigure}[5]{r}{.6\textwidth}
\begin{lstlisting} 
public void Main (string[] args)
{
   // here goes some code
}
\end{lstlisting}
\end{wrapfigure}

\blindtext
\end{document}

Producing enter image description here