jkitchin / org-ref

org-mode modules for citations, cross-references, bibliographies in org-mode and useful bibtex tools to go with it.
GNU General Public License v3.0
1.35k stars 242 forks source link

Referencing equations added with align environment #1099

Closed Nidish96 closed 8 months ago

Nidish96 commented 8 months ago

Firstly thanks for a great package, I recently started using it and I'm loving it!

I've used it with equations of the form,

      #+name: neweq
      \begin{equation}
        x^2+y^2=z
      \end{equation}

and I can refer to it using [[cref:neweq]] and everything work. However, if I have a multi-line equation that I've typeset using the align environment, it doesn't work. I'll appreciate any assistance, thanks!

Here's an example of something that doesn't work

      \begin{align}
        x^2+y^2&=z\label{eq1}\\
       x^2-y^2&=3\label{eq2}
      \end{align}

Neither [[cref:eq1]] nor [[cref:eq2]] work. 

Thanks! Nidish

jkitchin commented 8 months ago

what does not work mean here? It looks like it works for me: image

Nidish96 commented 8 months ago

Thanks for the response!

I'm sorry for not being clear. I agree, it works as a consistent link within org, but when i export it either to latex or html, it just says nil [[]] in both cases.

Here's a screenshot from my actual use case (top is org, bottom is tex): image

jkitchin commented 8 months ago

that doesn't look right, that is almost impossible if org-ref is working right. If you put your cursor on one of the ref links and type: M-x describe-text-properties

what do you see?

also, what is the output of this src block:

+BEGIN_SRC emacs-lisp

(org-link-get-parameter "cref" :export)

+END_SRC

finally, what is the output of M-x org-ref-debug

Nidish96 commented 8 months ago

Thanks for the quick reply again!

  1. Here's the result of describe-text-properties on one of the links:
    
    Text content at position 5575:

There are text properties here: face org-ref-ref-face font-lock-multiline t fontified t help-echo org-ref-ref-help-echo htmlize-link (:uri "cref:eq1") isearch-open-invisible org-fold-core--isearch-show isearch-open-invisible-temporary org-fold-core--isearch-show-temporary keymap [Show] line-prefix [Show] mouse-face highlight org-fold--spec-org-link-description-global org-link-description org-fold--spec-org-link-global org-link org-ref-ref-label "eq1" rear-nonsticky [Show] wrap-prefix [Show]

2. Here's the result of the src block:
```org
#+begin_src emacs-lisp
  (org-link-get-parameter "cref" :export)
#+end_src      

#+RESULTS:
: #[128 "\302\301\303\300\"\"\207" [("cref") org-ref-ref-export apply append] 6 "
: 
: (fn &rest ARGS2)"]
  1. Here's the result of org-ref-debug:
    
    #+TITLE: org-ref debug

org-ref: Version 3.0

org-ref installed in [[/home/nidish/.emacs.d/elpa/org-ref-20231027.1224/org-ref.elc]].

org-ref-helm (loaded: t) org-ref-ivy (loaded: nil) ** org-ref-pdf (loaded: nil)

You set =pdftotext-executable= to org-ref-pdf not loaded (exists: pdftotext-executable is not bound)

** org-ref-url-utils (loaded: nil)

jkitchin commented 8 months ago

weird, those all look right. do you have any org export processing hooks setup?

+BEGIN_SRC emacs-lisp

(list org-export-before-parsing-hook org-export-before-processing-hook)

+END_SRC

also, how are you doing your export to html/latex? The usual C-c C-e ho/lo?

Nidish96 commented 8 months ago

Here's what I get:

#+BEGIN_SRC emacs-lisp
(list org-export-before-parsing-hook
org-export-before-processing-hook)
#+END_SRC

#+RESULTS:
| org-ref-refproc | org--support-special-blocks-with-args | org-re-reveal-prepare-tts | org-attach-expand-links |

Yeah - I'm just exporting with C-c C-e ho/lo .

Here's a minimal org document which doesn't export properly:

#+title: Minimalex
#+author: Nidish Narayanaa Balaji
#+date: <2023-10-25 Wed>
#+refproc: :abbreviate t
#+latex_class_options: [12pt]
#+bibliography: refs.bib
#+options: h:3 num:t toc:nil
#+latex_header: \usepackage[margin=0.5in]{geometry}
#+latex_header: \usepackage{cancel}
#+latex_header: \usepackage{cleveref}

* Section 1
1. I expect [[cref:eq1]] to work properly.
   #+name: eq1
   \begin{equation}
     x^2+y^2=z
   \end{equation}
2. I don't expect either [[cref:eq1a]] or [[cref:eq2a]] to work.
   \begin{align}
     x^2+y^2&=z\label{eq1a}\\
     x^2-y^2&=3\label{eq2a}
   \end{align}

Here's what the output pdf looks like: image

jkitchin commented 8 months ago

I see what is happening. For latex, you don't need org-ref-refproc. That is what doesn't work for the second example. This function org-ref-refproc-referenceables doesn't currently support array equations. This will be tricky to support, right now it is assumed there is only one label per environment. I will have to think of a way to do that.

Nidish96 commented 8 months ago

Ahh snap. Thanks for looking into it anyways, I hope you figure this out! :)

Maybe support for subequations would be a nice intermediate step? See the example below, where using native org link works but using the org-ref version doesn't (in export).

#+title: Minimalex
#+author: Nidish Narayanaa Balaji
#+date: <2023-10-25 Wed>
#+refproc: :abbreviate t
#+latex_class_options: [12pt]
#+bibliography: refs.bib
#+options: h:3 num:t toc:nil
#+latex_header: \usepackage[margin=0.5in]{geometry}
#+latex_header: \usepackage{cancel}
#+latex_header: \usepackage{amsmath}
#+latex_header: \usepackage{cleveref}

* Section 1
1. I expect [[cref:eq1]] to work properly.
   #+name: eq1
   \begin{equation}
     x^2+y^2=z
   \end{equation}
2. I don't expect either [[cref:eq1a]] or [[cref:eq2a]] to work.
   #+name: eqname
   \begin{subequations}
     \begin{align}
       x^2+y^2&=z\label{eq1a}\\
       x^2-y^2&=3\label{eq2a}
     \end{align}
   \end{subequations}
3. [[cref:eqname]] also doesnt work, but [[eqname]] works.
jkitchin commented 8 months ago

These all work for pure latex export (just remove org-ref-refproc from the hook).

I pushed a fix for the first example. The second one I think won't work in mathjax for html, but now the first one does. you do need to (setq org-html-prefer-user-labels t) first.

Nidish96 commented 8 months ago

Thanks! I just tried the following and it works. It doesn't detect labels, however.

#+title: Minimalex
#+author: Nidish Narayanaa Balaji
#+date: <2023-10-25 Wed>
#+refproc: :abbreviate t
#+latex_class_options: [12pt]
#+bibliography: refs.bib
#+options: h:3 num:t toc:nil
#+latex_header: \usepackage[margin=0.5in]{geometry}
#+latex_header: \usepackage{cancel}
#+latex_header: \usepackage{amsmath}
#+latex_header: \usepackage{cleveref}

* Section 1
1. I expect [[cref:eq1]] to work properly.
   #+name: eq1
   \begin{equation}
     x^2+y^2=z
   \end{equation}
2. [[cref:eqname]] works in latex export, but =[[cref:eq1a]]= doesn't (give error: =unable to resolve "nil"=).
   In HTML export, the subequations environment is not detected. This is not an org-ref issue.
   #+name: eqname
   \begin{subequations}
     \begin{align}
       x^2+y^2&=z\label{eq1a}\\
       x^2-y^2&=3\label{eq2a}
     \end{align}
   \end{subequations}
3. [[cref:new]] works.
   #+name: new
   \begin{align}
     x^2+y^2&=z\\
     x^2-y^2&=2\label{bab}
   \end{align}
jkitchin commented 8 months ago

You are still mixing things that work and don't work. If you add this to your org file and use it to build a pdf, everything should work:


* build :noexport:
#+BEGIN_SRC emacs-lisp :exports none
(let ((org-export-before-parsing-hook nil))
  (org-open-file (org-latex-export-to-pdf)))
#+END_SRC

It is a mistake for making html to combine #+name and internal labels. Only the name reference will be found in that case. this snippet:

3. [[cref:new]] works. and cref:bab does too.
   \begin{align}
     x^2+y^2&=z\label{new}\\
     x^2-y^2&=2\label{bab}
   \end{align}

* build :noexport:

HTML build
#+BEGIN_SRC emacs-lisp :exports none
(let ((org-html-prefer-user-labels t)
      (org-export-before-parsing-hook '(org-ref-refproc)))
  (org-open-file (org-html-export-to-html)))
#+END_SRC

seems to work.

Nidish96 commented 8 months ago

Great, thank you! The fix works perfectly now :)