# How to correctly use geometry package with wrapfig package to avoid wrong layout

I am learning the wrapfig package. I noticed that when I include my common latex file, which has common definitions, the text no longer wrap around the figures, but the figure are pushed to the right only (since I set wrapfig to do this).

This turns out due to my using the geometry package in the common latex file, where I set the margin to 2.2cm for all my documents, which is something I had set long time ago.

If I do not use the geometry package, then the text will wrap around as expected.

I do not see why this setting of the margin should make wrapfig miss-behave so much. I'd like to keep my use of geometry package there with the 2.2cm margin, and still use the wrapfigure. Is this possible? Is there a better way to do this?

A MWE example will explain. First will show the problem when using \usepackage[margin={2.2cm}]{geometry}

Now will show the same page, but without the geometry package being used at all.

So now the text does wrap around. But the margins of the page are too large, and that is why I want to use geometry. It does not matter if I load the geometry package after or before wrapfig. same problem.

Here is the MWE

\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage[margin={2.2cm}]{geometry}%
\usepackage{wrapfig, blindtext}

\begin{document}
\section{The problem}
\begin{wrapfigure}{R}{0.3\textwidth}
\centering
\rule{0.9\linewidth}{0.75\linewidth}
\caption{\label{fig:d1}Integrating a function}
\end{wrapfigure}
%
To find a numerical value for the integral of a real valued function of a real
variable over a specific range over the real line. This means to
evaluate
$I={\displaystyle\int\limits_{a}^{b}}f\left( x\right) \ dx$
Geometrically, this integral represents the area under $f(x)$
from $a$ to $b$%

\section{Solution}
\begingroup
\begin{wrapfigure}{R}{0.3\textwidth}
\centering
\rule{0.9\linewidth}{0.75\linewidth}
\caption{\label{fig:d2}Numerical integration}
\end{wrapfigure}
\endgroup

We can always approximate the area by dividing it in equal width strips and
then sum the areas of all the strips.

In general, there will always be an error in the estimate of the area using
this method. The error will become smaller the more strips
we use (which implies a smaller strip width). Hence we can write%

${\displaystyle\int\limits_{a}^{b}} f\left( x\right) \ dx=\left( {\displaystyle\sum\limits_{i=1}^{N}} \Delta x\ f\left( x_{i}\right) \right) +E$

Where $E$ is the error between the actual area and the approximated area using the
above method of numerical integration. $N$ above is the number of strips or
can also be refereed to as the number of integration points.

Instead of keep referring to the 'width of the strip' all the time, we will
call this quantity the weight $w_{i}$ that we will multiply the value of the
function with to obtain the area. Hence the above becomes

$% {\displaystyle\int\limits_{a}^{b}} f\left( x\right) \ dx=\left( {\displaystyle\sum\limits_{i=1}^{N}} w_{i}\ f\left( x_{i}\right) \right) +E$

Using implied summation on indices the above becomes

$% {\displaystyle\int\limits_{a}^{b}} f\left( x\right) \ dx=w_{i}\ f\left( x_{i}\right) +E$

In the above we divided the range of the integration (the distance between the
upper and lower limits of integration) into equal intervals. We can decide to
evaluate $f(x_{i})$ at the middle of the strip or at the start
of the strip or at the end of the strip. In the diagram above we evaluated the
$f(x)$ at the left end of the strip.

Our goal is to evaluate this integral such as the error $E$ is minimum and using
the smallest number of integration points. In a sense this can be considered
an optimization problem with constraints: minimize the error of integration
using the smallest possible number of points.

\end{document}

• The problem is you don't have sufficient text in your paragraph. With geometry the margins are reduced than normal hence you get more space resulting in less text content wrt space. Increase margins or rewrite your paragraph to fill in some more space.
– user11232
Jun 13, 2014 at 7:25
• @HarishKumar sorry, not following you. I have large paragraphs below the figure, with lots of text. My question is why those are not flushed to the right edge of the page once the figure is set? The whole document, all the pages that follow, have this problem. everything got pushed to the left as seen in this screen shot !Mathematica graphics (Will update the question with this new screen shot also to make it clear) but feel free to modify the MWE if you can resolve this issue to show me what I need to change. Jun 13, 2014 at 19:20
• I have added some details in my answer. Hope it is clear now.
– user11232
Jun 13, 2014 at 22:19

# Don't

1. use capital letters (like R) for float specifiers. Capital letters mean the figure can float.
2. don't use a group to wrap the wrapfigure environment.

# Do

1. put sufficient text in the paragraph following wrapfigure.
2. specify the number of lines to be cut (so as to accomodate wrapfigure) as tols by olga.saucedo.

Here is the commented code.

\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage[margin={2.2cm}]{geometry}%
\usepackage{wrapfig, blindtext}

\begin{document}
\section{The problem}
\begin{wrapfigure}[12]{r}{0.3\textwidth}
\centering
\rule{0.9\linewidth}{0.75\linewidth}
\caption{\label{fig:d1}Integrating a function}
\end{wrapfigure}
%
To find a numerical value for the integral of a real valued function of a real
variable over a specific range over the real line. This means to
evaluate
$I={\displaystyle\int\limits_{a}^{b}}f\left( x\right) \ dx$
Geometrically, this integral represents the area under $f(x)$
from $a$ to $b$.%

%% uncomment following dummy text to see the undesired effect
This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text.
This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text.
This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text.
This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text.
This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text.
This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text.
This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text.

\section{Solution}
%\begingroup   %% why group?
\begin{wrapfigure}[12]{r}{0.3\textwidth}
\centering
\rule{0.9\linewidth}{0.75\linewidth}
\caption{\label{fig:d2}Numerical integration}
\end{wrapfigure}
%\endgroup

We can always approximate the area by dividing it in equal width strips and
then sum the areas of all the strips.


In general, there will always be an error in the estimate of the area using
this method. The error will become smaller the more strips
we use (which implies a smaller strip width). Hence we can write%

${\displaystyle\int\limits_{a}^{b}} f\left( x\right) \ dx=\left( {\displaystyle\sum\limits_{i=1}^{N}} \Delta x\ f\left( x_{i}\right) \right) +E$

Where $E$ is the error between the actual area and the approximated area using the
above method of numerical integration. $N$ above is the number of strips or
can also be refereed to as the number of integration points.

Instead of keep referring to the 'width of the strip' all the time, we will
call this quantity the weight $w_{i}$ that we will multiply the value of the
function with to obtain the area. Hence the above becomes

$% {\displaystyle\int\limits_{a}^{b}} f\left( x\right) \ dx=\left( {\displaystyle\sum\limits_{i=1}^{N}} w_{i}\ f\left( x_{i}\right) \right) +E$

Using implied summation on indices the above becomes

$% {\displaystyle\int\limits_{a}^{b}} f\left( x\right) \ dx=w_{i}\ f\left( x_{i}\right) +E$

In the above we divided the range of the integration (the distance between the
upper and lower limits of integration) into equal intervals. We can decide to
evaluate $f(x_{i})$ at the middle of the strip or at the start
of the strip or at the end of the strip. In the diagram above we evaluated the
$f(x)$ at the left end of the strip.

Our goal is to evaluate this integral such as the error $E$ is minimum and using
the smallest number of integration points. In a sense this can be considered
an optimization problem with constraints: minimize the error of integration
using the smallest possible number of points.

\end{document}

• Thanks. But I am having hard time getting the semantics of this put sufficient text in the paragraph following wrapfigure. What is sufficient text for each case? And what if I said all I have to say and want to start new section like with the MWE I have? Where do I come up with extra text? I see with the extra text it works nicely. But I can't always know how much text to put it there to make wrapfig happy. For me, wrapfig should provide a better solution than having one keep adding text until things work out. But thank you again for the hints. Jun 13, 2014 at 22:52
• @Nasser When a section starts, put enough text. enough means put text until the last line goes full text width. Or alternatively reduce the size of the picture height or increase the width of the picture so that it is more broad (hence pushing text below). You have to adjust until you are happy. There are limits on what packages can do ;)
– user11232
Jun 13, 2014 at 22:58
• That seems to have been the trick! putting a line and little more, above the wrapfig when starting new section. If the wrapfig was above the text, the problem shows up as in the MWE. So the solution seems to be, is to avoid wrapfig as first thing in a section! Ok, that is a rule I can remember. Will add it to my latex cheat sheet which is growing larger and larger each day. Thank you. Jun 13, 2014 at 23:09

Wrapfig calculates the short line width for whole paragraphs. Your text has many short paragraphs, so it is "lost", I suppose, for determining how far should the wrapping go. But Wrapfig also provides an optional argument to fix the number of lines that should be short. If you write:

\begin{wrapfigure}[10]{R}{0.3\textwidth}


at the relevant places, then you can manually adjust the number in the square brackets for each figure, in order to get the layout you want. Please note that only limiting the number of lines for the second figure doesn't work, you must do it for both of them.

• Thanks for the suggestion, but it does not work well. When added [10] to all the wrapfigure, now I get this !Mathematica graphics if I changed it to [8], the second figure jumbed to the second page and no wrapping happened. If I have to micro manage this like this, it will not work. Latex was supposed to be about content and let Latex put things in the right place. I think I will not use this package, it is not working for me as I expected. Jun 13, 2014 at 16:58