I am having a problem with some indentation of paragraphs when displaying an image next to a block of lstlisting
. I am using Overleaf online (and have found several weird quirks previously). Below is a MWE.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{wrapfig}
\usepackage{listings}
\usepackage{geometry}
\usepackage{setspace}
\usepackage{graphicx}
\usepackage{lipsum}
\geometry{letterpaper, portrait, margin=1in}
\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
keepspaces=true
}
\begin{document}
\doublespacing
\lipsum[1]
\begin{wrapfigure}{l}{0.5\textwidth}
\centering
\includegraphics[width=0.48\textwidth]{He3Defect.png}
\caption{Amplitudes of the 3-component OP components}
\end{wrapfigure}
\begin{lstlisting}
NOTICE: using acceleration to solve.
Building problem...done
time: 0 second(s).
solving...
Solver: successfully built
time: 0 seconds.
estimated: 21% done
Found solution:
iterations = 42
relative error = 7.61862e-07
time: 11 second(s).
\end{lstlisting}
\subsection{Free Energy}
Found a converging value for the free energy in 1 dimension.
The free energy calculations for 2 dimensions have not yet been correctly coded.
\lipsum[1]
\newpage
\section{Analysis}
The program will often converge to the correct solution in a reasonably small amount of time.
The possible causes of error were observed to come from poor choices of the relaxation parameter, too small of an iteration limit, poor initial guess, or a bad combination of step size and domain.
The result of the 1-dimensional free energy integral suggests...
The code has not yet been written to model neither the stripe phase nor the polka-dot phase.
\lipsum[2]
\end{document}
And this is some of the resulting document:
It appears almost as if the image is very tall, except I have verified that it is not; the extra space can span onto a new page and it affects all pages after it
I read that \lipsum
may cause some strange things as well (Problems with wrapfigure), but any text that I put in the same area does the same thing (so I just use \lipsum
for brevity). I also tried several other suggestions (Problem with wrapfigure and line breaks in the text, wrong indentation after wrapfigure, How to wrap text around a figure?, and Textwidth wrong after wrapfigure), but no luck.
A page on Overleaf says,
Take care while using adding wrapfigures very near the top or bottom of a page, as this can often cause unwanted effects that are hard or near-impossible to solve. It is not advisable to try to use wrapfigures alongside equations or sectional headers. They also cannot be used in lists, such as itemize and enumerate environments.
Does lstlisting
affect it in the same way?
Any ideas on how to fix it?
\doublespacing
. The graphic is not overlong, otherwise the caption would be much lower. Here's a possible hack (I haven't tested it): after the caption, insert\vspace{-7\baselineskip}
. The 7 is the actual number of (doublespaced) lines that are excess. This is a hack, but if it works, it would strengthen the case for the involvement of\doublespacing
.\doublespacing
does have something to do with it!\vspace{-7\baselineskip}
does remove the indentations on following pages, but then the text overlays the image. If I use a -3, the text falls just below the caption, and any value smaller than -3 makes the paragraphs on the following pages have really strange indentation. But just removing\doublespacing
doesn't fix it.\begin{wrapfigure}[...]{l}...
in which can be specified the "number of narrow lines". Counting the lines as doublespaced, it looks like 16 or 17 lines might be appropriate, so[17]
. Again, this is a wild guess, but it seems worth trying. (I haven't actually worked on a document usingwrapfig
for at least 5 years, and the only thing I remember for sure is that it is incompatible with any list environment. But I don't thinklstlisting
is that kind of list.) Good luck.\vspace{...}
before the next paragraph and it's working sufficiently now. Thanks for the ideas!