Formatting – How to Wrap Text Around Figure Being Formatted into a New Column in TexStudio

formattingwrapwrapfigure

I'm just learning LaTeX, and I am having a problem with wrapping an image. When I use the try to use wrapfigure in TexStudio, I get a second column generated in my document, with the wrapped text being pushed into this column, and the image being inserted in-line. Text below the wrapped section is moved up the document to where I would expect the wrapped text to have started.

I've cut everything from my document except the minimum to generated a wrapped image (as per code below). I've also tried changing the paper size, documentclass and removing the specific margin sizes I'm using, all produce the same result.
Searches on the internet unfortunately have given me little insight to this problem.

I get the same result when I set the compiler to PdfLaTeX or LuaLaTeX. I've also tried to compile the document using LuX, with the same result, but I don't know enough yet about LaTeX to rule out the editor as the source of my the problem, based on what I've read about how the wrapfigure tag is supposed to be used.

The desired result I am looking for is to have the image displayed on the right of the page, with the text wrapping around it to the the left, in single column (i.e. typical book or report format)

Anything that can help me understand why I am getting this result instead would be much appreciated.

\documentclass[10pt,legalpaper]{book}
\usepackage[
left=1cm,
right=2cm,
top=3cm,
bottom=4cm,]{geometry}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{lipsum}


\title{Sandbox}

\begin{document}
    
\chapter{First Chapter}
\subsection{1st Section}
\lipsum[1-1]
\subsection{Wrapped Image}
    \begin{wrapfigure}{r}{5cm}
        \lipsum[1-1]
        \includegraphics[width=3cm]{autumn-tree-1382832}
        \caption{A Tree}
        \label{fig:autumn-tree-1382832}
        %\lipsum[1-1]
    \end{wrapfigure}
\subsection{Last Subsection}    
\lipsum[1-8]    
\end{document}

image of the result I am getting:

enter image description here

Best Answer

You just want to move the text after the wrapfigure environment:

enter image description here

\documentclass[10pt,legalpaper]{book}
\usepackage[
left=1cm,
right=2cm,
top=3cm,
bottom=4cm,]{geometry}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{lipsum}


\title{Sandbox}

\begin{document}
    
\chapter{First Chapter}
\subsection{1st Section}
\lipsum[1-1]
\subsection{Wrapped Image}
    \begin{wrapfigure}{r}{5cm}
        \includegraphics[width=3cm]{example-image}
        \caption{A Tree}
        \label{fig:autumn-tree-1382832}
        %\lipsum[1-1]
    \end{wrapfigure}
        \lipsum[1-1]

\subsection{Last Subsection}    
\lipsum[1-8]    
\end{document}