I want to create source blocks (in any language including LaTeX), then refer to these in the text using internal links, with descriptions as shown below. This similar post is not working for me.

I have easily been able to created many source blocks using the common structure, for example:

#+BEGIN_SRC python
  for i in 1:10:
      print i

Then I added names to the blocks because I want to talk about them with internal links. Id did this by adding #+NAME:, and so have this:

#+NAME: some-source-code
#+BEGIN_SRC python
  for i in 1:10:
      print i

So the text block is somewhere within an org file (the same one in my case) and I want to insert a link to that code block above using C-c C-l. I have tried this with and without a description, so ending up with both:

[[some-source-code][my description]]



but neither are recognised in the exported PDF file. I simply get a pair of question marks in the PDF file and in the *Org PDF LaTeX output buffer* there is just a message stating:

Hyper reference some-source-code on page 6 undefined on input line 182.

Within the org-file itself, the links are shown and if I click on them I am taken to the code block as expected.

In the babel documentation for such source blocks there was an unfinished-looking sentence regarding the (which I assume to mean the name I provde for a source block), saying:

The name can be 20 characters long, and contain…XXX

Are there actually rules regarding the #+NAME: <label>?

Should I be including a specific #+LaTeX_HEADER in the org-file??

I am able to create links to websites using C-c C-l, with a description - and this is exported as expected to PDF.

I have org-version 8.2.10, emacs version 24.5.

  • Is exporting links to PDF a requirement? – Melioratus Mar 13 '16 at 21:11
  • @Melioratus - yes, I'd like the links available (clickable) in the pdf output. – n1k31t4 Mar 13 '16 at 21:16
  • Bummer the links in my answer won't export to PDF. Sorry. I'll look for another solution. – Melioratus Mar 13 '16 at 21:39
  • I think I found a way to link back to code block when exporting to PDF. I'll post my answer soon. – Melioratus Mar 13 '16 at 22:52
  • Thank you for asking question! I added method 3 which will link back to code block when exported to PDF. – Melioratus Mar 14 '16 at 14:59

If you use org-ref (https://github.com/jkitchin/org-ref), you can do this:

#+caption: test label:some-source-code
#+BEGIN_SRC python
  for i in 1:10:
      print i

See Listing ref:some-source-code

This exports to PDF and HTML with active links.


Try this

#+NAME: some-source-code
#+BEGIN_SRC python
  for i in 1:10:
      print i

Method 1


This should link back to some-source-code.

Caveat: [[file:::some-source-code]] is performing a degenerate search and will look for <<some-source-code>> links before #+NAME: some-source-code.

Method 2

[[file:::/#\+name: +some-source-code/]]

Do a regular expression search for regexp. This uses the Emacs command occur to list all matches in a separate window. If the target file is in Org mode, org-occur is used to create a sparse tree with the matches.

Method 3

When exporting to PDF this should work.

#+NAME: some-source-code
#+BEGIN_SRC python
  for i in 1:10:
      print i

#+BEGIN_SRC latex
  See \hyperlink{some-source-code}{some source code}.

Hope that helped!

This code was tested using
GNU Emacs 24.4.1 (x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14) of 2014-12-25
org-mode: 8.3.2

  • Unfortunately, neither of these work for me. Method 2 throws the error: Not a Tramp file name: (the regex). Method 1 created an empty link, that is zero description and a link to nowhere. If I re-write it to this: [[file:alg-page-scrape][My Description]] then I get a link to nowhere again, but at least with the description appearing in the PDF. I don't mean to sound arrogant, but shouldn't this kind of thing be the 'bread-and-butter' of org-mode? It works for files as expected, but not for source blocks... – n1k31t4 Mar 13 '16 at 19:07
  • @DexterMorgan - When you rewrote the links did you use 3 colons, i.e. file:::alg-page-scrap? The examples only work with 3 colons. So [[file:::alg-page-scrape][My Description]] should work. Please let me know if it doesn't. – Melioratus Mar 13 '16 at 21:10
  • I tried it first with three : and that didn't work. Then I tried it with one afterwards. – n1k31t4 Mar 13 '16 at 21:16
  • @DexterMorgan - When you say it doesn't work you mean when exporting to PDF? – Melioratus Mar 13 '16 at 21:41
  • As I mentioned, there is a normal link appearance (org-link-face??), however no actual link. Clicking on it did not take me to the source block. – n1k31t4 Mar 13 '16 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.