Anake
Anake

Reputation: 7649

org mode - side by side figure captions for latex export

I have found how to do side by side figures for latex export as described here: http://permalink.gmane.org/gmane.emacs.orgmode/52978

But the problem is that when I try to attach a #+CAPTION or #+NAME / #+LABEL, the output gets garbled with the caption moved across multiple lines as well as the pictures.

#+CAPTION: Some desription.
#+LABEL: mylabel
#+ATTR_LATEX: width=5cm
| [[./test.png]] | [[./test2.png]] |

I tried the same syntax with a single figure and it works fine:

#+CAPTION: Some desription.
#+LABEL: mylabel
#+ATTR_LATEX: width=5cm
[[./test.png]]

Does anyone know how to get this to work?

Upvotes: 16

Views: 7254

Answers (3)

Jordan He
Jordan He

Reputation: 350

Figured out a neat solution inspired by this post:

#+caption: Caption shared by both figures
#+BEGIN_EXPORT html
<img src="./fig1.png" width="48%"> <img src="./fig2.png" width="48%">
#+END_EXPORT

I later realized it only works on Hugo site when displaying as HTML and does not work on a normal org file.

Upvotes: 2

Reb.Cabin
Reb.Cabin

Reputation: 5567

Instead of putting images in a table, I centered a pair of images using this question from the TeX site on StackExchange..

I made a 1x1, transparent pixel at this web site and saved it to a file named empty_fig.png. I told org-mode to make it really tiny with #+ATTR_LATEX: :height 0.0001in. I then captioned that figure, right under the pair that I was not able to caption.

#+begin_center
#+ATTR_LATEX: :height 0.4\textwidth :center nil
[[file:Screenshot from 2019-04-30 18-25-36.png]]
#+ATTR_LATEX: :height 0.4\textwidth :center nil
[[file:Screenshot from 2019-04-30 18-25-46.png]]
#+end_center
#+CAPTION: Example frames 
#+LABEL: fig:video_pair
#+ATTR_LATEX: :height 0.0001in
[[file:empty_fig.png]]

I realize that this is hacky and it is exposed to having the image pair separated from the empty image when LaTeX re-flows the page, but that will be a different bridge to cross. It's an alternative that doesn't entail BIND, i.e., changing the internal state variables of org-mode.

Upvotes: 3

NickD
NickD

Reputation: 6412

AFAICT, the hack described in the link above (and I'm allowed to call it that, since I came up with it in the first place :-) ) works in current versions of org (9.0.3+ or thereabouts) with some customizations:

  • By default, images are centered (so the \includegraphics is wrapped inside a center environment), but that breaks images in tables. In the example below, I turn off the centering using a #+BIND to set org-latex-images-centered to nil.

  • The #+ATTR_LATEX (which, if I'm not mistaken, should look like this in "modern" org syntax: #+ATTR_LATEX: :width 5cm) applies to the table, so the images get the default size of 0.9\ linewidth, and with two of them, that's more than the width of the page. So in the example below, I use another #+BIND to set the default size of the images, but I think there is a bug: they are not set to 5cm as specified, but to their "natural" size. In any case, even if this is a bug, it is not really germane to the question.

Note that the caption is attached to the table that contains the two images: there might be a way to attach separate captions to the images, but I don't know for sure.

With all that in place, the following example file works for me (and it also shows a way to get a single centered image, even if you've disabled centering globally, as I do with the first #+BIND):

#+BIND: org-latex-images-centered nil
#+BIND: org-latex-image-default-width 5cm

* side by side figures in latex

#+CAPTION: An image
#+NAME: myimage
#+ATTR_LATEX: :width 8cm :center t
[[./images/test1.png]]

#+CAPTION: Some description
#+NAME: mytable
| [[./images/test1.png]] | [[./images/test2.png]] |

EDIT: There is no bug - I made a mistake in the #+BIND - it should be

#+BIND: org-latex-image-default-width "5cm"

The quotes are necessary!

Upvotes: 10

Related Questions