# Wrapping image with multiple theorem-like environments and displayed math

I'm struggling to put an image into a page with multiple theorem-like environments, some of which include displayed math, but I can't get the text to wrap correctly around the image and caption: either there is a lot of blank space below the image (which goes on even on the following page), or the text overlays the image. I have found lots of different solutions to this problem, but none seems to work in my case; the current best solution I'm using is a combination of minipage and parpic, which worked in other parts of the document but isn't working properly in this particular point.

Here is an example (which I hope is working), though it isn't exactly minimal: seeing that solutions that worked in other cases don't work in this particular case, I think it's better to put the "real" code where I have the problem (the image GraficiBarre.png, which is a simple 600x512 png, is loaded via the \addsspic command defined in the preamble). Here is a link to the compiled result.

\documentclass [12pt]{article}
\usepackage {amsthm,amssymb}
\usepackage {amsmath,amsfonts}
\usepackage {graphicx}
\usepackage {lipsum}
\usepackage {fullpage}
\usepackage [utf8]{inputenc}

\DeclareMathOperator{\Var}{Var}

\DeclareGraphicsExtensions{.pdf,.png,.jpg}

\theoremstyle{plain}
\newtheorem{teorema}{Teorema}
\newtheorem{prop}{Proposizione}
\theoremstyle{definition}
\newtheorem{definizione}{Definizione}
\newtheorem{esempio}{Esempio}
\theoremstyle{remark}
\newtheorem{osservazione}{Osservazione}

\RequirePackage{xparse, graphicx, caption, picins}
\begin{minipage}{#1}
\vspace{0.25cm}
\includegraphics[width=\textwidth]{#2}%
\IfNoValueTF{#3}{}{\captionof{figure}{\footnotesize #3}}
\end{minipage}
}}

\begin{document}
\begin{prop}
Disuguaglianza di Chebyshev. Sia $X : \Omega \rightarrow \mathbb{R}$ una variabile aleatoria reale, con $X \in L^2$. Allora:
$$\mathbb{P} \left ( \left \{ \omega \in \Omega : \left |X \left (\omega \right ) - \mathbb{E} \left [X \right] \right | \geq \varepsilon \right \} \right ) \leq \frac{\Var \left (X \right )}{\varepsilon^2} \ \forall \ \varepsilon > 0$$
\end{prop}
\begin{proof}
Sia $\tilde{X} = X - \mathbb{E} \left [X \right ]$ e sia $Z = \left | \tilde{X}\right |^2 = \left |X -\mathbb{E}\left [X\right ] \right |^2 \Rightarrow \mathbb{E} \left [Z \right ] = \Var \left (Z\right)$. Ricordiamo che, se $Z$ è una variabile aleatoria, la disugualianza di Markov (Teorema 8, Osservazione 33) ci dice che $\varepsilon \mathbb{P} \left ( \left \{ \omega \in \Omega : Z \left (\omega \right ) \geq \varepsilon \right \} \right ) \leq \int_\Omega \! Z \ \mathrm{d}\mathbb{P} = \mathbb{E} \left [Z \right ]$, quindi abbiamo che:
$$\varepsilon^2 \mathbb{P} \left ( \left \{ \omega \in \Omega : Z \left (\omega \right ) \geq \varepsilon^2 \right \} \right ) \leq \mathbb{E} \left [Z \right ] \Rightarrow \mathbb{P} \left ( \left \{ \omega \in \Omega : \left | X \left (\omega \right ) - \mathbb{E} \left [X \right ] \right |^2 \geq \varepsilon^2 \right \} \right ) \leq \frac{\Var \left(X\right )}{\varepsilon^2} \Rightarrow$$
$$\Rightarrow \mathbb{P} \left ( \left \{ \omega \in \Omega : \left | X \left (\omega \right ) - \mathbb{E} \left [X \right ] \right |\geq \varepsilon \right \} \right ) \leq \frac{\Var \left(X\right )}{\varepsilon^2} \qedhere$$
\end{proof}
\addsspic[6cm]{GraficiBarre}{Grafico a barre. L'altezza delle barre rappresenta il valore atteso della variabile, la lunghezza delle stanghette rappresenta il doppio della deviazione standard}
\begin{esempio}
Grafici a barre. Riscriviamo Chebyshev prendendo $\varepsilon = \delta \sigma \left (X \right )$:
$$\mathbb{P} \left ( \left \{ \omega \in \Omega : \left | X \left (\omega \right ) - \mathbb{E} \left [X \right ] \right |\geq \delta \sigma \left (X\right ) \right \} \right ) \leq \frac{1}{\delta^2}$$
Questa disuguaglianza è molto stretta, perciò di solito se si hanno informazioni sul tipo di legge seguito dalla variabile aleatoria si preferisce usare le tavole.
\end{esempio}
\begin{definizione}
\textbf{Convergenza in probabilità}. Sia $\left (X_n \right )_{n \in \mathbb{N}} : \Omega \rightarrow \mathbb{R}$ una successione di variabili aleatorie e sia $X : \Omega \rightarrow \mathbb{R}$ una variabile aleatoria. Allora:
$$X_n \rightarrow X \textnormal{ in probabilità } \Leftrightarrow$$
$$\Leftrightarrow \lim\limits_{n \rightarrow \infty} \mathbb{P} \left (\left \{ \omega \in \Omega : \left |X_n \left (\omega \right ) - X \left (\omega \right ) \right | > \varepsilon \right \} \right ) = 0 \ \forall \ \varepsilon \geq 0$$
Sostanzialmente, quindi, $X_n \rightarrow X$ in probabilità se la misura dell'insieme su cui $X_n$ si discosta da $X$ va a zero.
\end{definizione}
\begin{osservazione}
Questo è un criterio di convergenza piuttosto debole: infatti, è possibile che le $X_n$ assumano un valore diverso da $X$ in numerabili punti, pur tendendo a $X$ in probabilità.
\end{osservazione}
\begin{teorema} Legge debole dei grandi numeri. Sia $\left (X_n \right )_{n \in \mathbb{N}} : \Omega \rightarrow \mathbb{R}$ una successione di variabili aleatorie reali, con $X_n \in L^2$. Supponiamo inoltre che le variabili aleatorie siano scorrelate e che $\Var \left (X_n \right ) \leq c \in \left ]0, + \infty \right [ \wedge \mathbb{E} \left [X \right ] = m \in \mathbb{R} \ \forall \ n \in \mathbb{N}$; sia infine $S_n = \sum\limits_{k=1}^{n} X_k$. Allora, $\lim\limits_{n \rightarrow \infty} \frac{S_n}{n} = m$ in probabilità.
\end{teorema}
\begin{proof}
Abbiamo innanzi tutto che $\mathbb{E} \left [\frac{S_n}{n} \right ] = \frac{1}{n} \sum\limits_{k=1}^{n} \mathbb{E} \left [X_k\right] = m \ \forall \ n$ e che $\Var \left (\frac{S_n}{n} \right ) = \frac{1}{n^2} \sum\limits_{k=1}^{n} \Var\left (X_k\right ) \leq \frac{c n}{n^2} = \frac{c}{n}$.
\end{proof}
\lipsum[1-4]
\end{document}

• none of the "figure wrapping" packages are really well behaved when used in conjunction with list structures (which is what theorems are based on). also, picins is ancient, created for latex 2.09 and never upgraded. however, here are a couple of questions that might help: How to wrap text around theorem box? and wrapfigure in a newtheorem environment: Why not put in correct place? – barbara beeton Feb 2 '14 at 16:23
• Replace \textwidth with \linewidth inside the minipage. I tried it with your MWE and my own image. – John Kormylo Feb 2 '14 at 16:32
• Never mind. You have many other problems (18 errors in the MWE), and \textwidth works just as well (the key was using an local image). I had a similar problem I solved by "hand wrapping" the text through several minipages. – John Kormylo Feb 2 '14 at 16:42
• @barbarabeeton I had found something like those answers suggest, but the problem is they work beautifully when there's only one theorem environment (possibly without displayed math), but they fail miserably when there's more than one theorem involved. – arklumpus Feb 2 '14 at 17:02
• @JohnKormylo In fact I tried to put \linewidth, but there was no visible change to the output... [That code gives me no error when I compile it through TeXnicCenter, maybe you miss some packages?] Anyways, I found I can use \parbox to set the paragraph width manually (which I think is what you suggested last), but it is cumbersome, and moreover it puzzles me that such an advanced typesetting language as LaTeX doesn't have a "proper" way of adding images... – arklumpus Feb 2 '14 at 17:05

This is intended to illustrate how I handled this sort of problem, updated somewhat. The only thing cumbersome about it is figuring out where to end each minipage. The cutwin package seems easier, but it requires the number of lines of text instead of the height.

\documentclass[draft]{article}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools}
\usepackage{tikz}

\newcommand{\nopar}% obviates end-of-paragraph effects
{\rule{\linewidth}{0pt}\vspace*{-\baselineskip}}

\makeatletter
\newcommand{\PgfMarginSouth}[1]% #1 = bottom margin size (length)
\makeatother

\newsavebox{\boxA}
\newlength{\heightA}
\newlength{\widthA}
\newlength{\width}

\begin{document}

\newtheorem{theorem}{Theorem}

\medskip\begin{theorem}
A triangle inscribed in a circle with one of the sides passing
through the center of the circle forms a right triangle.
\end{theorem}

\savebox{\boxA}{% to get width and height
\begin{tikzpicture}
\coordinate (P) at (0,0);
\coordinate (A) at (-1,0);
\coordinate (B) at (.5,.867);
\coordinate (C) at (1,0);
\draw (P) circle(1);
\draw (A) -- (B) -- (C) -- cycle;
\draw[color=red] (P) -- (B);
\fill
(A) circle(2pt)
(B) circle(2pt)
(C) circle(2pt)
(P) circle(2pt);
\path
(A) node[left]{A}
(B) node[above]{B}
(C) node[right]{C}
(P) node[below]{P};
\PgfMarginSouth{5pt}
\end{tikzpicture}
}
\settoheight{\heightA}{\usebox{\boxA}}
\settowidth{\widthA}{\usebox{\boxA}}

\addtolength{\heightA}{-.7\baselineskip}% to actual top of minipage

\setlength{\width}{\textwidth}

\noindent
\begin{minipage}[t]{\width}
\textbf{Proof:}
Construct line segment $\overline{PB}$ from the center of the circle to the
remaining vertex.  Since the distance from the center to any point on a
circle is constant,
$\overline{PA} \cong \overline{PB} \cong \overline{PC}$,
which means that triangles $\triangle APB$ and $\triangle BPC$ are
isoceles, and therefore
$\angle A \cong \angle PBA \quad\hbox{and}\quad \angle C \cong \angle PBC \quad.$
\nopar% paragraph continues outside minipage
\end{minipage}
\hfill
\raisebox{-\heightA}{\usebox{\boxA}}
%
From the diagram we also see that $m\angle B = m\angle PBA + m\angle PBC$
and therefore $m\angle B = m\angle A + m\angle C$.

Finally, summming the internal angles of $\triangle ABC$ we get
$m\angle A + m\angle B + m\angle C = 2m\angle B = 180^\circ$
and therefore $m\angle B = 90^\circ$.  Q.E.D.
\end{document}


• the diagram is a bit "tight" at the bottom. but since the next line won't break very nicely at that point, maybe the diagram could be moved up just a bit -- there's space above, and visually, the top of the diagram is rather sparse. also, shifting the "B" down and to the right would make that less questionable. (obviously, this doesn't remove the need for manual placement.) – barbara beeton Feb 5 '14 at 17:08
• Or I could shrink it a little. Esthetics ALWAYS requires manual adjustments. – John Kormylo Feb 5 '14 at 17:20
• The most general solution is to add increase the bottom margin inside the tikzpicture using \path (current bounding box.south) node[below]{}; – John Kormylo Feb 5 '14 at 18:27
• I have modified the answer to add more margin to the bottom of the tikzpicture. – John Kormylo Feb 6 '14 at 16:44