# Custom built universal wrapfig using cutwin

The environment wrapfiguredoes not work well with theorem like environments. It can possibly be remedied by writing a custom (but restricted) mywrapfig environment using cutwin package. Which works OK but the only annoyance is to manually insert the height of the figure.

In the MWE, {5} are the pesky parameters to manually insert in these lines --

\begin{mywrapfig}{5}{\begin{tikzpicture}
\begin{mywrapfig}{5}{\begin{tabular}{|cc|}


Ideally I would like to do away with it as --

\begin{mywrapfig}{\begin{tikzpicture}
\begin{mywrapfig}{\begin{tabular}{|cc|}


Is there a way to do it?

EDIT : To be specific, is there a way to know the height of \wrapquestionfigure (in terms of number of lines of text) before printing it? If I know I can replace #1 in this line \cutout{0}{\dimexpr \textwidth - \wd\wrapquestionfigure - \columnsep\relax}{0pt}{#1}% by that dimension

The full MWE --

\documentclass{article}
\usepackage{tikz,cutwin,lipsum}

\newsavebox{\wrapquestionfigure}
\opencutright
\newenvironment{mywrapfig}[2]{%
\savebox{\wrapquestionfigure}{ \begin{tikzpicture}\node {#2};\end{tikzpicture} }%
\renewcommand{\windowpagestuff}{\begin{center}#2\end{center}}%
\cutout{0}{\dimexpr \textwidth - \wd\wrapquestionfigure - \columnsep\relax}{0pt}{#1}%
} {%
\endcutout%
}%

\begin{document}
\begin{mywrapfig}{5}{\begin{tikzpicture}
\draw[->] (0,0) -- (0,1.5) node[above] {$a (ms^2)$};
\draw[->] (-.3,0) -- (2,0) node[right] {$t (s)$};
\end{tikzpicture}}
\textbf{Wrapped figure:} \lipsum[1]
\end{mywrapfig}

\begin{mywrapfig}{5}{\begin{tabular}{|cc|}%
$\hat{i}\times\hat{j}=\hat{k}$ & $\hat{j}\times\hat{i}=-\hat{k}$ \\
$\hat{j}\times\hat{k}=\hat{i}$ & $\hat{k}\times\hat{j}=-\hat{i}$ \\
$\hat{k}\times\hat{i}=\hat{j}$ & $\hat{i}\times\hat{k}=-\hat{j}$
\end{tabular}}
\textbf{Wrapped table:} \lipsum[1]
\end{mywrapfig}

\textbf{General text:} \lipsum
\end{document}


• Is the line spacing correct? The picture looks as if there is insufficient vertical space before "Wrapped table" (that is, between the two paragraphs). – Michael Palmer Sep 9 '17 at 21:09
• I am not sure why that is. Not desirable anyway. However \textbf{General text:} \lipsum (not in the included image) further down the line shows no such anomaly. – magguu Sep 9 '17 at 21:12
• why are you putting the saved content in a one node tikzpicture? you don't seem to be using tikz at all? (you also have a space either side of the tikzpicture in the box, is that intentional? – David Carlisle Sep 9 '17 at 21:45
• @DavidCarlisle I am not sure about it, but without it the sizes may not work. I may have to force a tikzpicture even if I want to print a tablular. One of the examples prints a diagram whereas the other prints a tabular to illustrate this. Please see an edit in the question above. The space is not intentional. – magguu Sep 10 '17 at 5:54
• @magguu in the example you definitely don't want the tikz in your box saving code as then you end up with a tikzpicture nested inside a tikz node which is not recommended at all. – David Carlisle Sep 10 '17 at 8:42

The main part of the question is just asking how many standard baselines a box takes up which is

\numexpr
\dimexpr \ht\wrapquestionfigure+\dp\wrapquestionfigure+.5\baselineskip\relax
/
\numexpr\baselineskip\relax
\relax


You already can do that very easily with the plain Tex macro package insbox.tex: it defines two commands: \InsertBoxR{no of lines untouched}{contents} and a similar \InsertBoxL. They also accept an optional argument (to be put as the last argument) which is the number of supplementary shorter lines, in case (La)TeX computes incorrectly the number of shorter lines:

\documentclass{article}
\usepackage{tikz ,lipsum}
\input{insbox}

\begin{document}

\InsertBoxR{0}{\begin{tikzpicture}
\draw[->] (0,0) -- (0,1.5) node[above] {$a (ms^2)$};
\draw[->] (-.3,0) -- (2,0) node[right] {$t (s)$};
\end{tikzpicture}}
\textbf{Wrapped figure:} \lipsum[1]

\InsertBoxR{0}{~\begin{tabular}{|cc|}%
$\hat{i}\times\hat{j}=\hat{k}$ & $\hat{j}\times\hat{i}=-\hat{k}$ \\
$\hat{j}\times\hat{k}=\hat{i}$ & $\hat{k}\times\hat{j}=-\hat{i}$ \\
$\hat{k}\times\hat{i}=\hat{j}$ & $\hat{i}\times\hat{k}=-\hat{j}$
\end{tabular}}[1]
\textbf{Wrapped table:} \lipsum[1]

\textbf{General text:} \lipsum
\end{document}


• what is the purpose of ~? – magguu Sep 10 '17 at 5:59
• @magguu: It's because the spacing w.r.t. the surrounding text is a little too tight, in my opinion (2mm). It is defined by \@InsertBoxMargin=2mm in the package code. If I increase its value, it also increases the vertical margin, and the image is lowered. I didn't want to add some code to compensate for this. – Bernard Sep 10 '17 at 11:07
• This is interesting. Suddenly I am spoilt for choice. Now I have three good solutions for wrapping. – magguu Sep 10 '17 at 14:12
• Well, it depends on the context. It's a little less simple to incorporate a caption with this solution, as it's not a float. On the other hand, to insert a table or a figure within a list, it's easier. – Bernard Sep 10 '17 at 14:35