1

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:

basic example

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

indent spans new page indent on next page

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?

6
  • Welcome to tex.sx. Taking a wild guess, the extra space may be related in some way to the \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. Oct 26, 2021 at 23:46
  • Well, \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.
    – Izek H
    Oct 27, 2021 at 0:06
  • Sometimes it's worth reading the documentation. There's an option that can be placed in this position: \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 using wrapfig 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 think lstlisting is that kind of list.) Good luck. Oct 27, 2021 at 1:41
  • Somehow, any value greater than 11 causes similar problems, but I kept it at 11 and added some \vspace{...} before the next paragraph and it's working sufficiently now. Thanks for the ideas!
    – Izek H
    Oct 27, 2021 at 2:45
  • Why not use two side by side minipages, one for the image and another one for the listing?
    – leandriis
    Oct 27, 2021 at 5:20

1 Answer 1

0

This shows how to use paracol for this. It is incredibly robust, but you have to manually split paragraphs. Just let paracol run too long and see where you want the split to occur.

The [h] option for figure is needed to add the gap at the top. I added the split a line early just to show how it is done.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{paracol}
\usepackage{listings}
\usepackage{geometry}
\usepackage{setspace}
\usepackage{graphicx}
\usepackage{lipsum}
\geometry{letterpaper, portrait, margin=1in}

\lstset{
    basicstyle=\ttfamily,
    columns=fullflexible,
    keepspaces=true
}
% used to manually split paagraphs
\newsavebox{\textbox}
\newcommand{\splitpar}[2][\textwidth]{% #1 = width of column (optional), #2 = rest of paragraph after split
  \unskip\strut{\parfillskip=0pt\parskip=0pt\par}%
  \global\setbox\textbox=\vbox{\hsize=#1\relax\noindent\strut #2\strut}}
\newcommand{\continuepar}{\unvbox\textbox}

\begin{document}

\doublespacing

\lipsum[1]

\begin{paracol}{2}
\begin{figure}[ht]
    \centering
    \includegraphics[width=\linewidth, height=3.9in]{example-image}
    \caption{Amplitudes of the 3-component OP components}
\end{figure}
\switchcolumn
\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 \splitpar{not yet been correctly coded.}
\end{paracol}
\continuepar

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .