[Tex/LaTex] Text wrapping around truly floating content

floats

I've read a lot about the wrapfig package (and some others, like picinpar, picins), but I wonder if there's any possibility to have text wrapping around floating content that gets automatically positioned using the usual specifyers like [t] or [b]?

Here's an illustration of what I'd like to achieve:

+-------------------+-------------------+
|                   |                   |
|     ------------  |  -----  +-------+ |
|     ------------  |  ---    | [t]   | |
|     -----         |  -----  |       | |
|     ------------  |  -----  +-------+ |
|            -----  |  -----   ---      |
| +-------+  -----  |  -----            |
| | [b]   |  -----  |  ------------     |
| |       |  -----  |  ------------     |
| |       |  ---    |  ------------     |
| |       |  -----  |  ---------        |
| +-------+  -----  |  ------------     |
|  ------    -----  |  ------------     |
|                   |                   |
+-------------------+-------------------+

As you can see, it I'd additionally love the wrapped content to (optionally) extend into the margin.

I've made a handfull of books with this kind of page layout using OpenOffice Writer and even (please, don't beat me!) MS Word, but so far I haven't found any solution to this in LaTeX.

Here's a minimum (non-)working example, featuring two regular floating figures (figs. 1 and 2), which behave nicely, and two wrapfigures (figs. 3 and 4), which extend into the margin, but do not float in any way:

\documentclass[twoside]{article}

\usepackage{calc}
\usepackage{mwe}
\usepackage{wrapfig}

\begin{document}

\section{Some Section}

\blindtext

\begin{figure}[!b]
\includegraphics[width=\linewidth]{example-image.png}
\caption{This floats nicely to the bottom of the page}
\end{figure}

\blindtext

\begin{figure}[!t]
\includegraphics[width=\linewidth]{example-image.png}
\caption{And this floats nicely to the top of the page}
\end{figure}

\blindtext
\blindtext

\begin{wrapfigure}{O}[\marginparsep+\marginparwidth]{\linewidth}
    \includegraphics[width=\linewidth]{example-image.png}
    \caption{This {\em should} float to the bottom of the page}
\end{wrapfigure}

\blindtext

\begin{wrapfigure}{O}[\marginparsep+\marginparwidth]{\linewidth}
    \includegraphics[width=\linewidth]{example-image.png}
    \caption{This {\em should} float to the top of the page}
\end{wrapfigure}

\lipsum

\end{document}

The task is to make the two wrapped figures (figs. 3 and 4) float to the top or bottom, respectively — be it using the wrapfig package, be it by some other means.

Best Answer

The message on the question says

I'd love to see either a general recipe for the kind of layout I'm after or a definite explanation why this is impossible in (La)TeX.

So here's a (definitely non-definitive) explanation of why/how this is (im)possible in (La)TeX, and a potential recipe for this kind of layout.

To a first level of approximation, this is how TeX works:

  • As TeX reads your input, each paragraph is turned into a sequence of boxes (the individual characters in each word), glue (between words, between sentences), and penalties (potential hyphenation points, etc.).
  • This sequence is broken into lines of the specified width: each line becomes a horizontal box. (By default each such box has equal width, that of the text area, but TeX specifying the "paragraph shape": the width of each line.)
  • A vertical list is accumulated, consisting of these horizontal boxes. At some point this vertical list is broken off and shipped out as a page, along with things like headers and footers.

Floats (in the traditional sense, not the sense of this question) enter into the last step in some complicated way in LaTeX.

In your case, suppose we have decided, based on the specification, where to place your floating content (which I'll call insets). (This may require two passes, to include your floating insets that need to be placed before the paragraph in which they are mentioned.) Extending something into the margin is no problem; placing something at an absolutely specified position on the page is not difficult; there are standard ways to do it. The only thing that remains is that for each paragraph, as we walk down the page, we need to decide the appropriate “shape”: what its width should be, on the left and right, so that it doesn't run over the inset. (Something like this BTW is also what wrapfig does: look for mentions of parshape in wrapfig.sty.)

As @DavidCarlisle said in the comment, this is “not implemented as far as I know”, so you would have to implement it yourself.

This is the kind of thing that is easier to program with Lua(La)TeX. A working proof-of-concept (working, say, for documents where you have only plain running text and insets, nothing else) would be at most a few hours’ work. For example, before you typeset each paragraph (breaking it into lines) you can keep track of how far you've come on the page, where you are relative to the inset figure, and therefore which and how many lines need to have their widths changed in what ways. Then you specify that “shape” and let TeX typeset that paragraph. Then start over with the next paragraph, etc.

But turning it into something of production quality (e.g. dealing with multiple insets whose specification may overlap, dealing with the stretch or shrink between paragraphs, and most of all doing things in “standard” ways so as to work with the menagerie of existing LaTeX packages) may take an indefinite amount of time.

Related Question