The Org mode file that the OP posted works fine if there is enough text to wrap around the figure completely. The question is how to deal with the case where there is not enough text to fill the margin of the figure.
The OP found a question on TeX SE where this question is answered: the gist of the method is that one has to define a macro \clearwrapfig
to basically "fill the margin". A call to the macro after the text in the margin is done makes sure that the margin is filled, so that the next wrapfigure
environment starts with a clean slate.
Given this LaTeX mechanism, it is easy to retrofit it into the Org mode file:
#+LATEX_HEADER: \makeatletter\def\clearwrapfig{\ifnum\c@WF@wrappedlines>\@ne\mbox{}\par\expandafter\clearwrapfig\fi}\makeatother
#+options: toc:nil
* Foo
#+NAME: fig:top
#+CAPTION: Section intro
[[./kitten.jpg]]
Text below [[fig:top]].
#+NAME: fig:left
#+CAPTION: left figure
#+ATTR_LATEX: :float wrap :width 0.38\textwidth :placement {l}{0.4\textwidth}
[[./kitten.jpg]]
I must be on the same line as figure [[fig:left]].
#+LATEX: \clearwrapfig
#+NAME: fig:right
#+CAPTION: right figure
#+ATTR_LATEX: :float wrap :width 0.38\textwidth :placement {r}{0.4\textwidth}
[[./kitten.jpg]]
I must be on the same line as figure [[fig:right]].
#+LATEX: \clearwrapfig
This is text after the images.
The new stuff is the #+LATEX_HEADER:
that defines the \clearwrapfig
macro (copied verbatim from the TeX SE answer, except that it is made a single line) and the #+LATEX: \clearwrapfig
lines at the end of the text which is supposed to go in the margin: these call the macro at the appropriate places.
Note that the alignment is not perfect: the OP wants the marginal text to align with the top of the figure, but apparerently there is a border around the figure, so the text aligns with the border. But that is also a LaTeX question and needs to be asked on TeX SE, not here.
EDIT: Actually, that is answered in the OP's original posting of this question to Tex SE: all you have to do is add \vspace{-\baselineskip}
inside the wrapfigure
environment to move the image up by one line, thereby aligning it with the marginal text. That is easy to do in LaTeX, not so easy to do in Org mode: the generated figure environment will have to be told somehow to add this. One way to do that is to define your own derived exporter (see the doc string of org-export-define-derived-backend
), derived from the LaTeX one and identical to it, except for one modification: the definition of org-latex--inline-image
. There might be a simpler way to do it, but I can't think of one off the top of my head. If I come up with something, I'll edit the answer.
EDIT 2: This is a hack of the worst sort:
#+ATTR_LATEX: :float wrap :width 0.38\textwidth :placement {l}{0.4\textwidth} \vspace{-\baselineskip}
Adding the \vspace{-\baselineskip}
at the end of the :placement
parameter "works", because of how Org mode generates the code for a wrapfigure
. It's just added at the end of the \begin{wrapfigure}
environment opening:
\begin{wrapfigure}{l}{0.4\textwidth} \vspace{-\baselineskip}
and that works because LaTeX does not really care whether it's in its own line or not. Yuck! But it seems to work...