Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caption with cite results in cite appearing in list of figures #956

Closed
mcraveiro opened this issue Dec 9, 2021 · 7 comments
Closed

Caption with cite results in cite appearing in list of figures #956

mcraveiro opened this issue Dec 9, 2021 · 7 comments

Comments

@mcraveiro
Copy link
Contributor

mcraveiro commented Dec 9, 2021

Hi org-ref developers,

thanks very much for an amazing tool. I am on the process of updating my org-mode files to v3 and I stumbled into a problem. I am sure its something I am doing wrong, but can't quite see what I'm afraid. The long and short of it is, my captions are showing up as "cites" in the list of figures. At first, I thought my problems were related to #945, but now I don't think so.

For example, if we take:

#+NAME: Figure1
#+CAPTION[test]: Some text cite:&johnson-2010-great-paper some more text.
#+ATTR_ORG: :width 300px
#+ATTR_LATEX: :height 20cm
[[./omg_four_layer_metamodel_architecture.png]]

This produces the following latex:

\begin{figure}[htbp]
\centering
\includegraphics[height=20cm]{./omg_four_layer_metamodel_architecture.png}
\caption[\&johnson-2010-great-paper]{\label{fig:org02a7b84}Some text \cite{johnson-2010-great-paper} some more text.}
\end{figure}
\end{document}

Please notice how the caption contains the cite and ignores the text in square brackets (e.g. test). However, if I remove the cite, I get:

\begin{figure}[htbp]
\centering
\includegraphics[height=20cm]{./omg_four_layer_metamodel_architecture.png}
\caption[test]{\label{fig:orgc3c81b3}Some text some more text.}
\end{figure}
\end{document}

I think I am not doing something related to citations, but not yet sure what. I will keep reading the manual, but if you have any pointers it would be very helpful.

@mcraveiro
Copy link
Contributor Author

Apologies, forgot to provide some version details.

* Miscellaneous

- org-latex-prefer-user-labels = nil
- bibtex-dialect = biblatex
- biblatex is not required.
- biblatex is not used.
- emacs-version = GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.30, cairo version 1.16.0)
 of 2021-11-27, modified by Debian
- org-version = 9.3
- org-ref: Version 3.0
- org-ref.el installed at [HOME].emacs.d/elpa/org-ref-20211204.1322/org-ref.el
- org-ref-insert-cite-function = org-ref-insert-cite-link
- org-ref-insert-label-function = org-ref-insert-label-link
- org-ref-insert-ref-function = org-ref-insert-ref-link
- org-ref-cite-onclick-function = #[257 \300 \207 [org-ref-citation-hydra/body] 2 

@mcraveiro
Copy link
Contributor Author

mcraveiro commented Dec 9, 2021

I have managed to progress my investigation on this, and I think the problem may be related to org-mode itself and potentially the new citations. I found a way of dumping the org-mode tree, like so:

(setq test/my_results (org-element-parse-buffer))

It shows org-mode is already confused about captions after parse:

(org-data nil
          (section
           (:begin 1 :end 253 :contents-begin 1 :contents-end 253 :post-blank 0 :post-affiliated 1 :parent #0)
           (paragraph
            (:begin 1 :end 12 :contents-begin 1 :contents-end 11 :post-blank 1 :post-affiliated 1 :parent #1)
            #("some text\n" 0 10
              (:parent #2)))
           (paragraph
            (:begin 12 :end 203 :contents-begin 154 :contents-end 202 :post-blank 1 :post-affiliated 154 :name "Figure1" :caption
                    (((#("Some text " 0 10
                         (:parent #6))
                       (link
                        (:type "cite" :path "&johnson-2010-great-paper" :format plain :raw-link "cite:&johnson-2010-great-paper" :application nil :search-option nil :begin 55 :end 86 :contents-begin nil :contents-end nil :post-blank 1 :parent #6))
                       #("some more text." 0 15
                         (:parent #6)))
                      #("&johnson-2010-great-paper" 0 25
                        (:parent
                         (#6)))))
<snip>

In particular, the (:type "cite" :path "&johnson-2010-great-paper" got my attention. I'll keep on investigating this under this ticket as I suspect I won't be the last org-ref user to be bitten by this.

@mcraveiro
Copy link
Contributor Author

Short summary of my experimentation thus far.

Case 1: no cite

Org-mode:

#+CAPTION[test]: Some text some more text.

Tree:

           (paragraph
            (:begin 12 :end 172 :contents-begin 123 :contents-end 171 :post-blank 1 :post-affiliated 123 :name "Figure1" :caption
                    (((#("Some text some more text." 0 25
                         (:parent #6)))
                      #("test" 0 4
                        (:parent
                         (#6)))))
                    :attr_org
                    (":width 300px")
                    :attr_latex
                    (":height 20cm")
                    :parent #1)

Latex:

\caption[test]{\label{fig:org625be63}Some text some more text.}

This seems to do what is expected. test correctly shows up in the list of figures, and Some text some more text. as the label.

Case 2: cite as a org-mode link

Org-mode:

#+CAPTION[test]: Some text [[cite:&johnson-2010-great-paper]] some more text.

Tree:

            (:begin 12 :end 207 :contents-begin 158 :contents-end 206 :post-blank 1 :post-affiliated 158 :name "Figure1" :caption
                    (((#("Some text " 0 10
                         (:parent #6))
                       (link
                        (:type "cite" :path "&johnson-2010-great-paper" :format bracket :raw-link "cite:&johnson-2010-great-paper" :application nil :search-option nil :begin 55 :end 90 :contents-begin nil :contents-end nil :post-blank 1 :parent #6))
                       #("some more text." 0 15
                         (:parent #6)))))
                    :attr_org
                    (":width 300px")
                    :attr_latex
                    (":height 20cm")
                    :parent #1)

Latex:

\caption{\label{fig:org4aca667}Some text \cite{johnson-2010-great-paper} some more text.}

Citation is processed correctly, but list of figures now contains the same text as the figure, e.g. Some text CITATION some more text.

Case 3: "plain" cite

Org-mode:

#+CAPTION[test]: Some text cite:&johnson-2010-great-paper some more text.

Tree:

            (:begin 12 :end 203 :contents-begin 154 :contents-end 202 :post-blank 1 :post-affiliated 154 :name "Figure1" :caption
                    (((#("Some text " 0 10
                         (:parent #6))
                       (link
                        (:type "cite" :path "&johnson-2010-great-paper" :format plain :raw-link "cite:&johnson-2010-great-paper" :application nil :search-option nil :begin 55 :end 86 :contents-begin nil :contents-end nil :post-blank 1 :parent #6))
                       #("some more text." 0 15
                         (:parent #6)))
                      #("&johnson-2010-great-paper" 0 25
                        (:parent
                         (#6)))))
                    :attr_org
                    (":width 300px")
                    :attr_latex
                    (":height 20cm")
                    :parent #1)

Latex:

\caption[\&johnson-2010-great-paper]{\label{fig:org85c4803}Some text \cite{johnson-2010-great-paper} some more text.}

"Plain" cite now shows up in the list of figures (e.g. johnson-2010-great-paper), figure label looks correct.

@mcraveiro
Copy link
Contributor Author

mcraveiro commented Dec 9, 2021

I did some googling, and this issue seems rather similar to this one:

Doing a quick blame to see what the fix was, and if its not in org-mode 9.3...

Update: Actually the commit is fairly old:

https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/

My blame says:

  • Follows: release_9.3.4 (14)
  • Precedes: release_9.3.5 (3)

Maybe a red herring.

@mcraveiro
Copy link
Contributor Author

Argh, I forget just how far behind mainline Emacs is from latest :-) OK it turns out I was right. This has been patched in org-mode a very long time ago, but if like me you are running Emacs 27.1 then you do not have the fix. My solution was to manually update org-element--collect-affiliated-keywords with the following patch:

diff --git a/test.el b/test.el
index e8f86d3..0ee1511 100644
--- a/test.el
+++ b/test.el
@@ -36,7 +36,8 @@ When PARSE is non-nil, values from keywords belonging to
 			     (skip-chars-backward " \t")
 			     (point))))
 		  (if parsed?
-		      (org-element--parse-objects beg end nil restrict)
+                      (save-match-data
+		        (org-element--parse-objects beg end nil restrict))
 		    (org-trim (buffer-substring-no-properties beg end)))))
 	       ;; If KWD is a dual keyword, find its secondary value.
 	       ;; Maybe parse it.

After applying this patch, I now get:

Org-mode:

#+CAPTION[test]: Some text cite:&johnson-2010-great-paper some more text.

Latex:

\caption[test]{\label{fig:org5b272e4}Some text \cite{johnson-2010-great-paper} some more text.}

For anyone facing a similar issue, your best bet is to update Emacs or org-mode. Else you can patch the function like I did.

@mcraveiro
Copy link
Contributor Author

For completeness, here is a proper (appliable) patch for Emacs 27.1. Just copy this one file locally, apply the patch and read in the lisp.

diff --git a/org-element.el b/org-element.el
index 4b5f9a1..ca7be16 100644
--- a/org-element.el
+++ b/org-element.el
@@ -4024,7 +4024,8 @@ When PARSE is non-nil, values from keywords belonging to
 			     (skip-chars-backward " \t")
 			     (point))))
 		  (if parsed?
-		      (org-element--parse-objects beg end nil restrict)
+                      (save-match-data
+		        (org-element--parse-objects beg end nil restrict))
 		    (org-trim (buffer-substring-no-properties beg end)))))
 	       ;; If KWD is a dual keyword, find its secondary value.
 	       ;; Maybe parse it.

@jkitchin
Copy link
Owner

jkitchin commented Dec 9, 2021

Best issue report of the year!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants