23

This is similar to Org export to latex and HTML references, but for equations.

I like to use LaTeX math snippets in my org-files, as in

* The Quadratic Equation
The roots of $ax^2 + bx + c$ are given by
\begin{equation}
\label{eq:1}
x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}
\end{equation}

According to Equation \ref{eq:1}, ...

When I export to LaTeX, the \ref{eq:1} correctly turns into a hyper link to the equation. But I want to achieve the following additional behavior:

  1. Inside the org file, I can click the text \ref{eq:1} (the text doesn't have to be \ref{eq:1}, I just want a hyperlink) and be taken to to the equation. This functionality is present for headers, as in [[The Quadratic Equation]] becomes clickable in Org mode.

  2. When I export to HTML, I want to the references to be kept (actually, the label (1) doesn't even show up in HTML export).

Is there a way to accomplish this?

Kevin
  • 534
  • 1
  • 5
  • 15
  • Technically... wrapping the equation into `#+begin_src latex` ... `#+end_src` and then, if you place `#+name: eq:1` and `#+label: eq:1`, you should be able to reference this block the way you want, i.e. using a link `[[eq:1][first equation]]`, but for some reason it doesn't work for me (but it could be due to the changes I've made). – wvxvw Aug 21 '15 at 09:27
  • @wvxvw If you changed the behaviour through `advice`s only you can test without changes via `emacs -Q`. Naturally, this does not help if you changed the sources directly. – Tobias Aug 21 '15 at 12:39
  • @Tobias nah, I remember patching Org code, specifically something related to it inserting `\label{}` into verbatim environment, but I'd need to dig up my changes to make sure it's my fault. It would help me though, if someone could confirm that the suggested way with `#+name` etc works. – wvxvw Aug 21 '15 at 13:13
  • @wvxvw At html-export I get `
    `/`
    `-Tags around the `equation` environment. Therefore, firefox shows the source and not the equation.
    – Tobias Aug 21 '15 at 13:26
  • @Tobias, right, I completely forgot about it, you'd also need the `by-backend` macro from here: http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-LaTeX.html#sec-4-3 – wvxvw Aug 21 '15 at 13:38
  • @wvxvw No fun yet. I tried the `backend`-stuff and also the option `:export results` and I also tried to name and label the results. Note: after naming and labeling the results I get the hyperlink back in the generated html-file. – Tobias Aug 21 '15 at 15:18
  • Chiming in on another direction (@wvxvw I will try your suggestions later.) The package [here](https://github.com/jkitchin/org-ref) is promising (it is not yet on MELPA, so you have to manually install). With it, you can type `ref:eq:1` to reference any equation labeled with `\label{eq:1}`. See the picture [here](http://imgur.com/Eg57lCU). Exporting to HTML still doesn't work though, but I will continue investigating (it seems that we just need to enable numbering in MathJax?) – Kevin Aug 21 '15 at 18:51

3 Answers3

27

I had to run some test, but I managed to run a small file that exports correctly to both latex and HTML, I tested only in org 8.2.10, but it should work on other versions.

The answer is actually the same as in Org export to latex and HTML references

Here I have a small example

*  This is a latex experiment
so with the text...
#+NAME: code:1
#+BEGIN_SRC shell-script
echo hello world 
#+END_SRC
[[code:1]] is a reference to src block

#+NAME: eqn:1
\begin{equation}
    f(x) =  \sum\limits_0^\infty(f^{(n)}(x)|_{x=0} \cdot x)
\end{equation}

And this is an equation [[eqn:1]]
Joafigue
  • 773
  • 7
  • 12
  • Nice, this works for me out of the box. – Tobias Aug 22 '15 at 08:37
  • 1
    This works for me too in LaTeX, but it doesn't work for "out of the box" in HTML, since the equation doesn't even have a number (some Mathjax option isn't being set, probably). Now if only RefTeX would support this, but that's another question. I will wait a couple more days before accepting, to see if anyone else will chime in :) – Kevin Aug 23 '15 at 05:56
  • I accepted, but see my own answer below (later version of Org has support out of the box). – Kevin Aug 25 '15 at 00:23
  • I get "undefined references" in LaTeX; doesn't work for me with Emacs 24.5.1 (Org mode 8.2.10) – Reb.Cabin Mar 19 '16 at 02:57
  • 2
    Can this somehow be extended to multi-line AMS environments like ``align`` and ``subequations``, where the label is specific to *parts* of the equation? – kdb Jun 06 '18 at 10:18
  • 1
    Does not work for me and I am at Org version 9.4.6 :-( – Ajned Jun 20 '21 at 11:42
4

Actually, it seems that Org mode 8.3.1 has built-in support for what the question is asking; label and ref work out of the box.

This requires one to update Org mode through the package manager; afterwards, stick (package-initialize) near the top of the initialization file.

EDIT. This does not allow you to click the \ref{...} and go to the label. For that, please see the accepted answer above, or use org-ref.

Kevin
  • 534
  • 1
  • 5
  • 15
  • 1
    But you don't get to click on \ref{.} to go to \label{.}, right? – rvf0068 Sep 02 '15 at 23:45
  • 1
    No I cannot. For that, you have to use either Joafigue's answer above or see my comment above regarding https://github.com/jkitchin/org-ref. I will edit my answer to reflect that. – Kevin Sep 03 '15 at 02:01
1

As is described in Internal links.

You can use <<id>> to set your target id and use [[id]] or [[id][description]](You can use C-c C-l to insert this) to find your target.

This works for me while exporting to HTML.

Ynjxsjmh
  • 273
  • 2
  • 8