4

I would like to wrap text around a figure in a ntheorem like environment. Currently, I am trying to use the cutwin package, which was suggested e.g. in this question: How to wrap around a figure in a theorem-like environment? (If there is no easy answer to the problem below, I would also be thankful for alternative solutions using different packages.)

It seems to me that there are some issues with cutwin in font size 11. Consider the following example.

\documentclass[10pt]{book}
\usepackage{cutwin}
\usepackage{lipsum}
\usepackage{ntheorem}

\newtheorem{theorem}{Theorem}
\opencutleft
\renewcommand\windowpagestuff{%
\rule{4cm}{6cm}
}

\begin{document}
\lipsum[1]
\begin{theorem}
\begin{cutout}{1}{0pt}{0.65\linewidth}{15}
\lipsum[1]
\end{cutout}
\end{theorem}
\end{document}

10pt font on the left, 11pt font on the right

Note that for a 10pt font (left picture) one gets the expected result, but for a 11pt font (right picture) the Theorem overlaps the last paragraph. (And also the first line below the cutout is too close to the line before.) This overlap depends on the actual height of the cutout. Is this a known issue? I couldn't find anything relating the misalignment to the font size on the internet or in the package documentation.

Is there an easy way to fix this? I guess that it might suffice to redefine some constants in the cutwin package, but couldn't figure out how by myself.

Note also that the solution given in Misalignment with cutwin only works for plain texts and not in a ntheorem environment.

Philipp
  • 43

1 Answers1

4

This answer provides two solutions.

The essential elements for the first approach were posted here: Will it ever be possible to use wrapfig with an enumerate or itemize environment?

This solution uses a strut, which places the baseline of the first text line at a specified distance from the top of the minipage. Then adjustbox is used to place the top of the minipage exactly that amount above the outer baseline. There must be a better approach for defining the strutheight because I had to use \vspace{-0.5cm} to get the correct alignment.

This effectively sets the image and the text as two independent items within the minipage, but with the result that the text will wrap around the image. A constraint is that, as far as I am aware, it is not possible to set text above the image. That is, the image will be in row 1, or have blank space above it if, say, vspace is placed in front of the figure.

The second approach builds on OP's example, by putting the theorem environment inside an adjustbox. Three examples are provided.

This approach is flexible with respect to placement of the figures on the left or right and commencing and finishing at different row levels in the theorem environment.

Each theorem environment interacts with other page elements whether it is text above or below the theorem, or as in this case where there is no text between theorems, the ajoining theorem environments. This makes each theorem sensitive to font size and the surround page elements, and may lead to overwriting of prior page content. This is addressed by using the adjustbox key vspace. vspace places the nominated space before and after the box created by adjustbox. By setting the theorem in the adjustbox, the vspace key can be used to correct any misplacement of the theorem. As illustrated in the MWE, vspace may take values of 0bp, \baselineskip or indeed any other permitted value to achieve appropriate setting of the theorems on the page. If the size of the font is changed, or content is added between each theorem, it may be necessary to adjust the vspace values.

enter image description here

This is the MWE:

\documentclass[a4paper,11pt]{book}
\usepackage[margin=3.5cm]{geometry}
\usepackage{lipsum}
\usepackage[english]{babel}
\usepackage[pangram]{blindtext}
\usepackage{cutwin}
\usepackage{ntheorem}
\usepackage[calc]{adjustbox}
\usepackage{graphicx}
\usepackage{wrapfig}

\newtheorem{theorem}{Theorem}
\opencutleft
\renewcommand\windowpagestuff{%
%    \rule{4cm}{3cm}
\includegraphics[width=0.8\linewidth]{example-image-b.png}
}

\newlength{\strutheight}
\settoheight{\strutheight}{\strut}
\setlength{\parskip}{\baselineskip}
\pagestyle{empty}
\raggedbottom

\begin{document}

% Wrapfigure in adjustbox
\begin{adjustbox}{valign=C,raise=\strutheight,minipage={1.0\linewidth}}
\begin{wrapfigure}{r}{0.15\linewidth} % picture on the right
    \centering    
    \includegraphics[width=1.0\linewidth]{example-image-b.png}
\end{wrapfigure}% 
\strut{}
\vspace*{-0.5cm} % This is a fudge to align the top of the theorem environment with the image
\begin{theorem}
     \Blindtext[1][10]
\end{theorem}%
\end{adjustbox}

%cutout in adjustbox, cutout on the left
\begin{adjustbox}{valign=C,vspace=0bp,minipage={1.0\linewidth}}
\begin{theorem}
    % {3} = Cutout starts in row 3
    % {0pt} = Cutout is flush left (0pt from left edge)
    % {0.70\linewidth} = text covers 0.65\linewidth relative to right margin
    % {8) = cutout extends 8 rows
    \begin{cutout}{2}{0pt}{0.70\linewidth}{6} 
     \Blindtext[1][12]   
    \end{cutout}
\end{theorem}
\end{adjustbox}

% move the cutout to the right and use example-image-b
\opencutright 
\renewcommand\windowpagestuff{% setup the image to be placed in the cutout
    \centering
    \includegraphics[width=0.80\linewidth]{example-image-b.png}
}

% cutout in adjustbox, cutout on the left
\begin{adjustbox}{valign=C,vspace=0bp,minipage={1.0\linewidth}}
    \begin{theorem}
        \begin{cutout}{2}{0.70\linewidth}{0pt}{6}
         \Blindtext[1][12]
        \end{cutout}
    \end{theorem}
\end{adjustbox}

\opencutcenter
\renewcommand\windowpagestuff{% setup the image to be placed in the cutout
    \centering
    \includegraphics[width=0.80\linewidth]{example-image-b.png}
}
%\blindtext[1]

    \adjustbox{valign=C,vspace=\baselineskip,minipage=1.0\linewidth}{
    \begin{theorem}
        \begin{cutout}{2}{0.30\linewidth}{0.30\linewidth}{8}
            \Blindtext[1][12]
        \end{cutout}
    \end{theorem}
}

\end{document}
Ross
  • 5,656
  • 2
  • 14
  • 38
  • Thanks for this solution, which works for me. Just two more questions, that could also be relevant for future readers:
    1. Can the picture also be placed lower (i.e. really in the bulk of the Theorem text, this might be important if the Theorem is a long proof)?
    2. cutwin doesn't work with a 11pt font and ntheorem at all?

    By the way, your link in the answer above is broken.

    – Philipp May 25 '17 at 10:56
  • You could use \vspace{measurement} before the image, but this would leave blank space at the top. 2. Appears to be solved by putting cutout inside an adjustbox environment. 3. Link fixed.
  • – Ross May 25 '17 at 14:43
  • @Phillip you might consider replacing the title with something like "Placement of figures inside an ntheorem environment" – Ross May 25 '17 at 14:47
  • 1
    Thanks again! This looks perfectly. I also replaced the title. – Philipp May 26 '17 at 08:59
  • My last comment was a bit too fast, I didn't look at the font size. The first solution works fine, but the second one still has the problem that it doesn't work properly with an 11pt font. For a bigger cutout the misalignment won't be much better than in the original example without the adjustbox. – Philipp May 26 '17 at 12:18
  • @Phillip I did some more testing and the solution appears to be the use of vspace as an adjustbox setting. Note the comment in my post that its use will depend on the font size and particular content of the page. If you add text between the theorem environments, it will likely be necessary to change the vspace measure. This might be another option (https://tex.stackexchange.com/questions/127465/wrap-figure-in-theorem-environment) but I have not tried it. – Ross May 27 '17 at 00:54
  • Ok, I accepted this answer. The first approach (having the picture on top) works for me and looks nice, the other solutions for having the picture inside the theorem are still not completely satisfactory as the first line below the picture is too close to the line before. I guess it is simply best not to put pictures in the bulk of a ntheorem at all. – Philipp Jun 01 '17 at 07:57
  • 1
    The strut height (=/= total height) can be accessed using \ht\strutbox. Depth is then \dp\strutbox. – Martin Scharrer Nov 29 '17 at 16:47