Am 02. März 2026 um 10:09 Uhr +0100 schrieb Christian Moe:
Here's one approach. It uses the function org-cite-list-keys, which is
called during export, so I've put it into an export hook.
Thank you very much. `org-cite-list-keys' was exactly what I was
looking for, although it is unfortunate that this function is only
available during export. However, I can accept that I have to export
the document first before exporting my subset .bib file, because my
use case is to supply co-authors with the .bib entries I
used. Naturally, I do that after exporting.
Not tested extensively.
I found your suggestion to work nicely and was not able to find a
problem with it.
Below is the new function `my/extract-bibentries' that I came up with
to solve my original problem. It does not depend on Ebib. Instead, it
piggybacks on the functions provided by the built-in `bibtex.el' to
locate the entry corresponding to each citation key and then copies
that one into a result buffer. I am sure this function can be
optimised, but for me, this is enough.
Thank you again.
(require 'bibtex)
(defvar my/org-exported-citekeys nil
"List of citekeys in the last exported document.")
(defun my/org-get-citekeys (string backend info)
(setq my/org-exported-citekeys (org-cite-list-keys info))
string)
(add-hook 'org-export-filter-body-functions
'my/org-get-citekeys)
(defun my/extract-bibentries ()
"Creates a buffer with all bib entries corresponding to the citation keys
used in the current org buffer. The keys are gathered during export, so
export the document first before calling this function."
(interactive)
(let ((bibfiles nil)
(entries nil)
(case-fold-search t))
(unless my/org-exported-citekeys
(error "No citations found. Export the document before calling
`my/extract-bibentries'."))
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^#\\+bibliography:[[:space:]]+\\(.*?\\)$"
nil t)
(push (match-string 1) bibfiles)))
(setq bibfiles (nreverse bibfiles))
(dolist (citekey my/org-exported-citekeys)
(dolist (filename bibfiles)
(with-temp-buffer
(insert-file-contents filename)
(goto-char (point-min))
(when (bibtex-search-entry citekey)
(bibtex-mark-entry)
(push (buffer-substring (region-beginning) (region-end))
entries)))))
(switch-to-buffer "*cited bib entries*")
(erase-buffer)
(dolist (entry (nreverse entries))
(insert entry "\n\n"))
(goto-char (point-min))
(bibtex-mode)))
Best Regards,
--
Dipl.-Jur. M. Gülker | https://mg.guelker.eu | PGP: Siehe Webseite
Stade, Deutschland | [email protected] | O<