8

I would like to be able to wrap source code around a figure. Here's a MWE that should make it clear what I have in mind:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{listings}
\lstset{language={},
    breaklines=true,
    basicstyle=\ttfamily,
    breakatwhitespace=true}

\begin{document}
\noindent
\def \myasywidth {5cm}
\begin{wrapfigure}{l}{\myasywidth}
\vspace{0pt}
\rule{\myasywidth}{\myasywidth}
\end{wrapfigure}
%
%
\begin{lstlisting}
settings.outformat="pdf";
size(5cm,0);

real xmin = -0.1;
real xmax = 2;
real ymin = -0.1;
real ymax = 2;

path s = (0,0){up} .. (1/2,sqrt(1/2))  .. (1,1) .. (xmax,sqrt(xmax));
fill(s -- (xmax,0) -- cycle, mediumgray);
draw(s);

arrowbar axisarrow = Arrow(TeXHead);
draw((xmin,0) -- (xmax,0), arrow=axisarrow);
draw((0,ymin) -- (0,ymax), arrow = axisarrow);

real x = 1.4;
real dx = .05;
real t0 = times(s,x)[0];
real t1 = times(s,x+dx)[0];
path striptop = subpath(s,t0,t1);
draw((x,0) -- striptop -- (x+dx,0) --  cycle, blue);
\end{lstlisting}
%
\end{document}

Here's the result (produced with some warnings about conflicting environments):

enter image description here

How do I make this work? (In addition to wrapping, the solution should also restore the behavior of the listings package that continuations of automatically broken lines should be indented.)

  • 1
    Try leaving an empty line right after the definition of \def\myasywidth {5cm} -- at least, it works (ie., wraps) when I run your case. – Jesse Sep 17 '13 at 6:52
  • @Jesse, that's a surprisingly useful suggestion. Now if I could just get the indentation after line breaks to work... – Charles Staats Sep 17 '13 at 14:06
  • Let me get this right. Are you saying you want "arrowbar" indented in this case? If yes, you may check the listings manual on page 39, top, to activate \indent manually. – Jesse Sep 17 '13 at 16:10
  • @Jesse: No, that's not what I'm saying. Every time a single long line of code (e.g., path s = (0,0){up} .. (1/2,sqrt(1/2)) .. (1,1) .. (xmax,sqrt(xmax));) is split into several shorter lines, I want all but the first of these indented, so that it is easy to see that this is conceptually a single line of code. This is the normal behavior of the lstlisting environment, but using wrapfigure seems to break it. – Charles Staats Sep 17 '13 at 16:41
  • In alternative, potentially confusing terminology: I want a hanging indent on long lines that wrap (no relation to wrapfigure) to the next line. – Charles Staats Sep 17 '13 at 16:49
2

Is this solution acceptable?

\documentclass{article}
\usepackage{wrapfig}
\begin{document}
\noindent
\def \myasywidth {5cm}
\begin{wrapfigure}{l}{\myasywidth}
\vspace{0pt}
\rule{\myasywidth}{\myasywidth}
\end{wrapfigure}
%
%
{\ttfamily\obeyspaces\obeylines
settings.outformat="pdf";
[...listing ommited...]
draw((x,0) -- striptop -- (x+dx,0) --  cycle, blue);
}
%
\end{document}

Produces:

Result

Caveats:

  • The code should not include any latex special char, as it is the case in this example. Otherwise they should have been "escaped".
  • Although this is poorly illustrated by the given example, Asymptote code often does include special latex characters, since text labels are always processed using TeX. Since I want to be able to copy and paste source code into the code environment, it really does need to be more verbatim-like than this. – Charles Staats Dec 18 '13 at 17:29
0

Packages like wrapfig, picins, cutwin, etc. do not work well in the presence of list environments, or other environments that work by managing vertical boxes directly. Most of these packages mention this in the documentation explicitly. I will be surprised if you find a straightforward solution that is compatible with the listings package.

I suggest that you do it the hard way: wrap the listing around the figure manually. You'll need to break the listing, put the two (or three?) parts in minipage and arrange so that these are placed in the way you want them. This will work if your figure is of rectangular shape. If not, I'm out of suggestions.

  • I don't necessarily have to use the listings package; I'm okay with almost any verbatim-like environment that will split too-long lines and provide indentation. – Charles Staats Sep 17 '13 at 14:12
  • Looks like a doable solution. There won't be differences in the inter line distance of subsequent lines, so you can count the number of lines that should be next to the figure and the number of lines that should follow the figure. – user10274 Oct 17 '13 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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