2

I'm stuck on a trial and error loop testing the latex export settings to no avail.

What I want:

+---------+
|         |
| Pic     |
|         |
+----+----+---+
|Pic |Txt     |
|    |        |
+----+--------+
|Txt |Pic     |
|    |        |
+----+--------+

What I have (pdf screenshot): pdf screenshot

#+options: toc:nil
* Foo
#+NAME: fig:top
#+CAPTION: Section intro
[[./kitten.jpg]]
Text below ref: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 ref:fig:left

#+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 ref:fig:right

The kitten source: https://commons.wikimedia.org/wiki/File:Cute_grey_kitten.jpg

jjk
  • 705
  • 4
  • 16
  • 2
    This is very much a LaTeX question and you should probably ask it in the TeX SE. FWIW, the above works fine, *if you add enough text to fill the gaps*. But `wrapfig` is a finicky environment (if you don't believe me, read the [documentation](https://mirror.mwt.me/ctan/macros/latex/contrib/wrapfig/wrapfig-doc.pdf) ). – NickD Mar 11 '22 at 21:40
  • 1
    @NickD You might be interested in PeterWilson's comment on my copy of this question on [tex](https://tex.stackexchange.com/questions/636960/should-i-keep-trying-to-write-a-portfolio-in-org-mode?noredirect=1#comment1587319_636960) ;-) – jjk Mar 13 '22 at 21:11
  • I am not surprised.The point of my comment above is that you cannot solve LaTeX problems using Org mode: you have to solve them in LaTeX and then somehow figure out how to produce that LaTeX code from Org mode (which may not be possiible, in which case, you'll have to include the LaTeX code in your Org mode file). But if you dump Org mode into a question on TeX SE, what did you expect to happen? What you should have done (and what I thought you would do) is to take the TeX file that Org mode produces and post *that* in your question, not the Org mode file itself. – NickD Mar 13 '22 at 23:18
  • By the way, did you try out the suggestion in my first comment to add enough text to fill the gaps? Since that seems to work, the question would be why LaTeX mishandles the situation when not enough text is present. *That* would have been the question to ask on TeX SE (and by posting the LaTeX file there, instead of the Org mode file). – NickD Mar 13 '22 at 23:21
  • @NickD Yes, filling up with text works. Apparently I must surround every image with a [macro](https://tex.stackexchange.com/a/418986/156605). Any hints on tackling that obstacle in org mode or should I post a new question? – jjk Mar 14 '22 at 17:40
  • Good detective work! Given this mechanism, it is easy to retrofit it into the Org mode file: see my answer below. Note that you do *not* surround the image with the new macro: the macro is called when you want to end the marginal text and go on with the rest of the file. – NickD Mar 14 '22 at 18:15
  • This discussion triggered a similar discussion in [this question](https://emacs.stackexchange.com/questions/70943/how-to-increase-font-size-of-equation-from-a-latex-export-block), about where Org mode ends and where LaTeX begins. It might be of interest. – NickD Mar 14 '22 at 18:42

2 Answers2

3

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...

NickD
  • 27,023
  • 3
  • 23
  • 42
  • Posted another [question](https://tex.stackexchange.com/questions/637116/what-is-causing-the-vertical-distance-between-a-float-picture-and-the-text-along) a couple of seconds after your edit, thank you so much for your elaborations – jjk Mar 14 '22 at 18:51
  • See the latest edit for a hair-raisingly ugly hack that works :-) BTW, you can always close the new question if nobody has answered yet (although if it's not a duplicate, it might be useful to leave it there: *somebody* wil come up with the same question at some point...) – NickD Mar 14 '22 at 19:12
2

You haven't specified the export backend. I will assume that you are OK with ODT backend, specifically the enhanced ODT backend.

The enhanced ODT backend is not available with standard Emacs or Org mode, and you need to install it separately. See Getting Started with ODT export (OpenDocument Text Exporter for Emacs’ Org Mode for instructions.

The snippet below uses

  1. Transcluded Tables.

    Transcluded Tables are exclusive to the enhanced ODT backend.

    (The transcluded table feature is not enabled by default. So, before exporting, remember to execute the =emacs lisp= source block (C-c C-c) you find at the start of the snippet)

  2. Cell Spans

See List of features that are exclusive to The Enhanced OpenDocument Exporter for Org mode for more information.

A Bird's eye view of =Transcluded Table= and =Cell Spans=

A Bird's eye view of =Transcluded Table= and =Cell Spans=

If you want this

Animal Pictures (laid out via a =transcluded table=)

... do this

Do this

#+odt_preferred_output_format: pdf
#+odt_prettify_xml: 


#+begin_src emacs-lisp :exports results :results silent
(add-to-list 'org-odt-experimental-features 'transclude-sole-footnote-references-in-a-table)
#+end_src

* Animal Pictures

#+ATTR_ODT: :style "GriddedTable"
#+ATTR_ODT: :widths "1,1"
#+ATTR_ODT: :span "@1$1{1:2}"
| [fn:top]                   |                           |
| [fn:left]                  | There is a dog to my left |
| There is a cat to my right | [fn:right]                |

[fn:top]
#+ATTR_ODT: :height 3
#+NAME: fig:top
#+CAPTION: A Unicorn
[[./org-mode-unicorn.png]]

There is a unicorn above me.

[fn:left]
#+ATTR_ODT: :height 3
#+NAME: fig:left
#+CAPTION: A Dog
#+ATTR_LATEX: :float wrap :width 0.38\textwidth :placement {l}{0.4\textwidth}
[[./org-mode-unicorn.png]]

[fn:right]
#+ATTR_ODT: :height 3
#+NAME: fig:right
#+CAPTION: A Cat
#+ATTR_LATEX: :float wrap :width 0.38\textwidth :placement {r}{0.4\textwidth}
[[./org-mode-unicorn.png]]