# Unreliable positioning of Wrapfigure

I am new to Latex and currently I am "probing" Latex for issues that I want an solution to before actually using Latex for work. My current problem is placement of wrapped figures. Wrapfigure{} seems completely unreliable to me.

I have created a compileable code to illustrate the problem. I have included all the used packages even though most are not relevant to this problem.

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage[table,xcdraw]{xcolor}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage[nottoc,numbib]{tocbibind}
\usepackage{verbatim}
\usepackage{float}
\usepackage{caption}
\usepackage{wrapfig}

\begin{document}

\section{Example}
\lipsum[1-2]
\lipsum[1-1]
\begin{wrapfigure}{R}{70mm}
\centering
\includegraphics[width=60mm]{./MeltingEnergyNH2}
\caption{Melting Energy}
\label{figur:NH2}
\end{wrapfigure}
\textbf{\lipsum[120]}

\subsection{WrapFigur Test}
\lipsum[1-4]

\end{document}


Since the page has so much text the wrapped picture is pushed into the bottom margin. If I change the [r] to [R] and thus make the wrapped figure a float the figure disappears completely(it is not at the end of the document) and severely messes up the formatting of rest of the document as it is seen on picture 2(every page is like this one or worse)

This problem seems to appear when too much text is in front of the wrapped figure. Using \newpage or similar can remove the problem, but this does not seem like a flexible solution. Is there a reliable solution to this problem?

Pictures(too large for the post) of the problem in the document:

Document with "[r]" Picture1

Document with "[R]" Picture2

• Welcome to TeX.SX! Could you please make your test document compilable? May 1 '18 at 17:28
• Welcome to TeX.sx! That code in a empty article, with the three packages needed in the preamble (wrapfig,lipsum,graphicx), and a example-image (supplied by the mwe package) works perfectly (with r and R option). You should add a minimal working example (MWE) that illustrates your problem. We need a compilable code, using example images availables for everybody (i.e, those of the mwe package,) starting with \documentclass{...}, ending with \end{document} and of course reproducing your problem.
– Fran
May 1 '18 at 17:41
• Your MWE works with R removing titlesec, With r you simply shoud decide if move your figure up or down.
– Fran
May 1 '18 at 17:46
• TeXnician and Fran I have created a compileable version of the code and included it in the original topic. I am sorry for not doing this right away. I am not quite aware of the normal way to post these topics. May 1 '18 at 17:49
• Fran You are right. Removing titlesec does make it work. Thank you. Not quite sure why it works though. However this produces an another problem. I want the figure to be wrapped around the bolded lipsum text. However with [R] the figure wraps around the lipsum text after the next subsection(1.1 Wrapfigur Test). This is obviously not desireable if the figure has nothing to do with the next subsection(1.1). Is there a way to tell Latex that the figure needs to be before subsection 1.1, even if that means a lot of empty page space? May 1 '18 at 17:54

In general with floats, the best approach for me is do not care of where is finally placed the image until the final version, but take care that it is always referenced when is really relevant (i.e, see fig. \ref{keylabel}) as sometimes simply the image cannot be placed just here.

In the final draft, then move floats some paragraphs above/below solve most problems, and as last resource one can fix the float options, but being as permissive as possible, especially when you have a lot of floats. As more restrictive you are with floats, more problems you will find later.

Said that, for this edge case (the figure must be in a paragraph that have a page break, with no enough space in the first page) this is a dirty hack for the very final version only: split the paragraph in two parts just where the page break should be, taking care of hiding that to the reader (fill the last line of the first part and avoid indentation of the first line of the second part). Then take care that the wrap figure take enough vertical space, so that the second part of the paragraph do not take a extravagant shape (the [14] option, although in this MWE work equally without that).

\documentclass[14pt,a4paper]{article}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{wrapfig}

\begin{document}

\section{Example}
\lipsum[1-2]
\lipsum[1-1]

{\parfillskip=0pt
Nunc id nulla nec mauris iaculis rutrum. Nunc nisl. Integer mi.
Praesent lorem neque, egestas at, molestie in, faucibus et, eros.
Sed rutrum, ante vitae aliquet tincidunt, diam elit auctor risus,
eu elementum purus turpis eu elit. Proin ac orci. Integer varius,
urna non sollicitudin consequat, massa libero pharetra erat, et ve-
nenatis dui orci eget purus. Aliquam iaculis est eget ipsum. Ut
volutpat velit. Phasellus fringilla. Aliquam mollis tellus vel odio.}

\begin{wrapfigure}[12]{r}{70mm}
\includegraphics[width=\linewidth]{example-image}
\caption{Melting Energy}
\label{figur:NH2}
\end{wrapfigure}

\noindent Vestibulum ante ipsum primis in faucibus orci luctus et ultrices
posuere cubilia Curae; Vestibulum gravida sapien sed diam dic-
tum pharetra. Nulla ac odio. Duis vitae metus ut purus feugiat
interdum. Duis eros enim, tincidunt ac, venenatis et, dignissim id,
lacus. Curabitur sagittis dolor nec augue. Sed ultricies mauris.
Donec semper, enim eu vestibulum placerat, justo risus eleifend
quam, ac semper velit pede convallis arcu.

\subsection{WrapFigur Test}
\lipsum[1-4]
\end{document}

• Thank you for that excellent explanation of your workflow with floats. I will try and follow those guidelines. For future readers: In addition to the general management of floats I also had an issue using the [R] parameter for the wrapfigure{} which would "mess up" the formatting of the rest of the document in this edge case. This was also solved by Fran in the comments for the question. The solution were to remove the titlesec package. May 1 '18 at 19:42