I am trying to run a source code block and have the outputted figure go into a LaTeX document as a figure. Basically I am doing this in a way slightly similar to Jupyter notebooks, but with the ability to work in org
and have access to LaTeX export.
I have already come across this answer which does an example for Python, which does not seem to resolve my issue and I still have a problem with the Python version of this answer.
My MWE for the org file is:
* Testing source
#+NAME: fig:20170117
#+BEGIN_SRC R :file test.png :session org_org :results graphics
x <- c(0, 3, 8, 9, 0)
y <- c(1, 4, 5, 6, 7)
plot.new()
plot(x, y, xlab="test_x", ylab="test_y")
mtext("Some plot")
#+END_SRC
#+CAPTION: Output caption
#+LABEL: fig:20170117
#+ATTR_LATEX: :width 0.75\textwidth
#+RESULTS: fig:20170117
[[file:test.png]]
#+NAME: fig:org_fig
#+BEGIN_SRC python :file org.png :session org_py :results graphics
clf()
plot(np.sin(np.arange(0,7,0.1)))
plt.savefig('org.png', transparent=True, bbox_inches='tight')
'org.png'
#+END_SRC
#+CAPTION: Org Fig
#+LABEL: fig:org_fig
#+ATTR_LATEX: :width 0.5\textwidth
#+RESULTS: fig:org_fig
[[file:org.png]]
And exporting both of these gives the LaTeX body with the code but not the correct labels or either of the figures. If I comment out the #+RESULTS:
line I will get the figures, but without the captions and labels.:
\section{Testing source}
\label{sec:orge034bc3}
\lstset{language=r,label=org47910a7,caption= ,captionpos=b,numbers=none}
\begin{lstlisting}
x <- c(0, 3, 8, 9, 0)
y <- c(1, 4, 5, 6, 7)
plot.new()
plot(x, y, xlab="test_x", ylab="test_y")
mtext("Some plot")
\end{lstlisting}
\lstset{language=Python,label=org677e653,caption= ,captionpos=b,numbers=none}
\begin{lstlisting}
clf()
plot(np.sin(np.arange(0,7,0.1)))
plt.savefig('org.png', transparent=True, bbox_inches='tight')
'org.png'
\end{lstlisting}