# org links inside source code blocks

In my org file I have something like:

#+NAME: fig:figure
\begin{figure}
\includegraphics{figure}
\end{figure}


and then I want to reference that figure inside a code block:

#+BEGIN_SRC r
# code ...
x <- 3
# more code for fig. [[fig:figure]]
plot(1:x)
#+END_SRC


[[fig:figure]] is not a link in the org document and does not get resolved when exported, it simply stays verbatim in the text. Is it somehow possible to make it reference the figure?

I think you can do it like this. They key is the mathescape option for minted, and using minted for src block fontification.

#+BEGIN_SRC emacs-lisp
(setq org-latex-minted-options
'(("frame" "lines") ("linenos=true") ("mathescape")))
#+END_SRC

#+caption: figure for testing \label{fig-1}
[[./test.png]]

#+BEGIN_SRC ipython
5
# see figure $\ref{fig-1}$
4
#+END_SRC

#+RESULTS:
:results:
# Out [1]:
# text/plain
: 4
:end:

• Is it right that this will only work for LaTeX export, i.e., no link-fontification in the org-buffer and the method does not work for other export targets? – Tobias Apr 30 '19 at 14:37
• that works, pretty cool! Is it possible to set the use of minted file local? – gdkrmr Apr 30 '19 at 16:10
• I think you can use org-latex-listings to locally control which package is used. You want to set it to 'minted – John Kitchin Apr 30 '19 at 18:38
• Also this will only affect LaTeX exports. It won't fix anything in the org-buffer, or work for other export backends. – John Kitchin Apr 30 '19 at 18:38
• an even better way is the option texcl, it only works in comments but does not switch to the math font! – gdkrmr May 2 '19 at 15:11