[Tex/LaTex] Unreliable positioning of Wrapfigure

floatsformattingpositioningwrapfigure

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

Best Answer

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).

enter image description here

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