gregsexton / ob-ipython

org-babel integration with Jupyter for evaluation of (Python by default) code blocks
737 stars 111 forks source link

Figure captions (LaTeX export) #193

Open dschrempf opened 6 years ago

dschrempf commented 6 years ago

I am producing LaTeX documents using Org + IPython and I would like to add figure captions (and possibly labels). I can do this, e.g., by adding #+CAPTION: Bla just above the figure in the results block. E.g., with :results raw drawer:

#+RESULTS:
:RESULTS:
# Out[48]:
#+CAPTION: Test.
[[file:myFile.png]]
:END:

However, when reexecuting the source block, the results block gets replaced and the caption vanishes. Do you know a way around that?

Thanks, Dominik

Edit: I just wanted to add, that I now use something like

#+BEGIN_SRC ipython :session :ipyfile FileName.png :exports none :results none
#+END_SRC

#+NAME: fig:file-name
#+CAPTION: Nice caption.
[[file:FileName.png]]

It needs too much user intervention for my taste, but it could be automated quite well. I leave this open, maybe you have a better suggestion, but please feel free to close the issue!

AoifeHughes commented 5 years ago

@dschrempf did you ever get any where with this? I have the exact same problem with adding other latex parameters to my outputs

dschrempf commented 5 years ago

No, I am still using the code in the edit (essentially, I circumvent using the RESULT block, but rather directly link to the image file). Thereby, the caption and name do not get overridden.

AoifeHughes commented 5 years ago

Fair enough - I've found it super useful, so thank you!

I also made a simple snippet if anyone else stumbles on this

https://github.com/SirSharpest/.emacs.d/blob/master/snippets/org-mode/pyimg

dschrempf commented 5 years ago

Hi! Thanks a lot for the comment. I added your idea to the snippet I am using! Just for completeness:

# -*- mode: snippet; require-final-newline: nil -*-
# name: ipython-figure
# key: ipy-fig
# --
#+NAME: ${1:`(make-temp-name "")`}
#+BEGIN_SRC ipython${2: :session ${3:`(file-name-base (buffer-file-name))`}} :ipyfile `(file-name-base (buffer-file-name))`-$1.svg :exports ${4:$$(yas-choose-value '("code" "none"))} :results none
$0
#+END_SRC

#+CAPTION: $6
#+LABEL: fig:$1
[[file:`(file-name-base (buffer-file-name))`-$1.svg]]
moritzschaefer commented 5 years ago

The only way I manage to get a Figure caption(instead of the Figure alone) is to use ":results file" in the header. However, this has the undesired side effect that I get an additional output in my Latex file

[[#file:# Out[]: ]]

has anyone observed this issue? Here is my code:

* section
some text

#+NAME: fig:test_a
#+BEGIN_SRC ipython :session :results file :exports results 
  _=sns.barplot(x=[1,2,3,4,5])
#+END_SRC

#+CAPTION: This is a figure
#+LABEL: fig:test_a
#+ATTR_LaTeX: scale=0.75
#+RESULTS: fig:test_a
:results:
# Out[1094]:
[[file:./obipy-resources/b1mu0b.png]]
:end:

Here is a Figure [[fig:test_a]] and 

and the resulting PDF:

image