[Tex/LaTex] How to correctly use geometry package with wrapfig package to avoid wrong layout

geometrywrapfigure

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}

Mathematica graphics

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

Mathematica graphics

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}

Best Answer

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.

enter image description here

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}
Related Question