Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 two 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.
share|improve this question
    
Look at this answer for some pointers. –  vonbrand Dec 8 '12 at 21:33
add comment

1 Answer 1

up vote 5 down vote accepted

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

share|improve this answer
    
exactly what I needed. Thanks! Is there also a better approach for the second point in my modifications? (see edited post) –  Matthias Dec 8 '12 at 21:51
    
Yes, you can use the aboveskip option of listings, e.g., \begin{listings}[aboveskip=-3pt] –  Guido Dec 8 '12 at 22:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.