Subject: bug#38762: 26.3; reftex-citation-format doesn't know biblatex fields

From: Arash Esbati <[email-masked]>
Date: Mon, 06 May 2024 22:25:50 +0200

Andrew Francis Swann <[email-masked]> writes:

> For local citations outside a tex file I have
>
>   (setq reftex-cite-format
>         "[%4a, \\textit{%t}, %b %e, %u, %r %h %j \\textbf{%v} (%y), %p %<]" )
>
> but when the database file is in biblatex format, the journal name is
> not in the field journal, accessed by %j, but rather in the field
> journaltitle, that is not detected.  E.g. the entry
>
> @Article{Abels:slice,
>   author =	 {Abels, H.},
>   title =	 {Parallelizability of proper actions, global
>                   {$K$}-slices and maximal compact subgroups},
>   journaltitle = {Math. Ann.},
>   year =	 1974,
>   volume =	 212,
>   pages =	 {1--19}
> }
>
> produces the local citation 
>
> [Abels, \textit{Parallelizability of proper actions, global
> {$K$}-slices and maximal compact subgroups}, \textbf{212} (1974),
> 1--19]
>
> with no journal name.

(I'm going through old bug reports that unfortunately weren't resolved
at the time.)

Can you please eval this function and try it again?

--8<---------------cut here---------------start------------->8---
(defun reftex-format-citation (entry format)
  "Format a citation from the info in the BibTeX ENTRY according to FORMAT."
  (unless (stringp format) (setq format "\\cite{%l}"))

  (if (and reftex-comment-citations
           (string-match "%l" reftex-cite-comment-format))
      (error "reftex-cite-comment-format contains invalid %%l"))

  (while (string-match
          "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
          format)
    (let ((n (string-to-number (match-string 4 format)))
          (l (string-to-char (match-string 5 format)))
          rpl b e)
      (save-match-data
        (setq rpl
              (cond
               ((= l ?l) (concat
                          (reftex-get-bib-field "&key" entry)
                          (if reftex-comment-citations
                              reftex-cite-comment-format
                            "")))
               ((= l ?a) (reftex-format-names
                          (reftex-get-bib-names "author" entry)
                          (or n 2)))
               ((= l ?A) (car (reftex-get-bib-names "author" entry)))
               ((= l ?b) (reftex-get-bib-field "booktitle" entry "in: %s"))
               ((= l ?B) (reftex-abbreviate-title
                          (reftex-get-bib-field "booktitle" entry "in: %s")))
               ((= l ?c) (reftex-get-bib-field "chapter" entry))
               ((= l ?d) (reftex-get-bib-field "edition" entry))
               ((= l ?e) (reftex-format-names
                          (reftex-get-bib-names "editor" entry)
                          (or n 2)))
               ((= l ?E) (car (reftex-get-bib-names "editor" entry)))
               ((= l ?h) (reftex-get-bib-field "howpublished" entry))
               ((= l ?i) (reftex-get-bib-field "institution" entry))
               ((= l ?j) (cond ((not (string-empty-p
                                      (reftex-get-bib-field "journal" entry)))
                                (reftex-get-bib-field "journal" entry))
                               ((not (string-empty-p
                                      (reftex-get-bib-field "journaltitle" entry)))
                                (reftex-get-bib-field "journaltitle" entry))
                               (t "")))
               ((= l ?k) (reftex-get-bib-field "key" entry))
               ((= l ?m) (reftex-get-bib-field "month" entry))
               ((= l ?n) (reftex-get-bib-field "number" entry))
               ((= l ?N) (reftex-get-bib-field "note" entry))
               ((= l ?o) (reftex-get-bib-field "organization" entry))
               ((= l ?p) (reftex-get-bib-field "pages" entry))
               ((= l ?P) (car (split-string
                               (reftex-get-bib-field "pages" entry)
                               "[- .]+")))
               ((= l ?s) (reftex-get-bib-field "school" entry))
               ((= l ?u) (reftex-get-bib-field "publisher" entry))
               ((= l ?U) (reftex-get-bib-field "url" entry))
               ((= l ?r) (reftex-get-bib-field "address" entry))
               ((= l ?t) (reftex-get-bib-field "title" entry))
               ((= l ?T) (reftex-abbreviate-title
                          (reftex-get-bib-field "title" entry)))
               ((= l ?v) (reftex-get-bib-field "volume" entry))
               ((= l ?y) (reftex-get-bib-field "year" entry)))))

      (if (string= rpl "")
          (setq b (match-beginning 2) e (match-end 2))
        (setq b (match-beginning 3) e (match-end 3)))
      (setq format (concat (substring format 0 b) rpl (substring format e)))))
  (while (string-match "%%" format)
    (setq format (replace-match "%" t t format)))
  (while (string-match "[ ,.;:]*%<" format)
    (setq format (replace-match "" t t format)))
  format)
--8<---------------cut here---------------end--------------->8---

This is the corresponding diff:

--8<---------------cut here---------------start------------->8---
diff --git a/lisp/textmodes/reftex-cite.el b/lisp/textmodes/reftex-cite.el
index 34f40ba689f..5d7c93a4717 100644
--- a/lisp/textmodes/reftex-cite.el
+++ b/lisp/textmodes/reftex-cite.el
@@ -1048,7 +1048,13 @@ reftex-format-citation
                ((= l ?E) (car (reftex-get-bib-names "editor" entry)))
                ((= l ?h) (reftex-get-bib-field "howpublished" entry))
                ((= l ?i) (reftex-get-bib-field "institution" entry))
-               ((= l ?j) (reftex-get-bib-field "journal" entry))
+               ((= l ?j) (cond ((not (string-empty-p
+                                      (reftex-get-bib-field "journal" entry)))
+                                (reftex-get-bib-field "journal" entry))
+                               ((not (string-empty-p
+                                      (reftex-get-bib-field "journaltitle" entry)))
+                                (reftex-get-bib-field "journaltitle" entry))
+                               (t "")))
                ((= l ?k) (reftex-get-bib-field "key" entry))
                ((= l ?m) (reftex-get-bib-field "month" entry))
                ((= l ?n) (reftex-get-bib-field "number" entry))
--8<---------------cut here---------------end--------------->8---

TIA.  Best, Arash