Function: reftex-create-bibtex-file
reftex-create-bibtex-file is an autoloaded, interactive and
byte-compiled function defined in reftex-cite.el.gz.
Signature
(reftex-create-bibtex-file BIBFILE)
Documentation
Create a new BibTeX database BIBFILE with all entries referenced in document.
The command prompts for a filename and writes the collected entries to that file. Only entries referenced in the current document with any \cite-like macros are used. The sequence in the new file is the same as it was in the old database.
Entries referenced from other entries must appear after all referencing entries.
You can define strings to be used as header or footer for the
created files in the variables reftex-create-bibtex-header or
reftex-create-bibtex-footer respectively.
Probably introduced at or before Emacs version 22.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/textmodes/reftex-cite.el.gz
;;;###autoload
(defun reftex-create-bibtex-file (bibfile)
"Create a new BibTeX database BIBFILE with all entries referenced in document.
The command prompts for a filename and writes the collected
entries to that file. Only entries referenced in the current
document with any \\cite-like macros are used. The sequence in
the new file is the same as it was in the old database.
Entries referenced from other entries must appear after all
referencing entries.
You can define strings to be used as header or footer for the
created files in the variables `reftex-create-bibtex-header' or
`reftex-create-bibtex-footer' respectively."
(interactive "FNew BibTeX file: ")
(let ((keys (reftex-all-used-citation-keys))
(files (reftex-get-bibfile-list))
key entries beg end entry string-keys string-entries)
(save-current-buffer
(dolist (file files)
(set-buffer (reftex-get-file-buffer-force file 'mark))
(reftex-with-special-syntax-for-bib
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(while (re-search-forward "^[ \t]*@\\(?:\\w\\|\\s_\\)+[ \t\n\r]*\
[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t)
(setq key (match-string 1)
beg (match-beginning 0)
end (progn
(goto-char (match-beginning 1))
(condition-case nil
(up-list 1)
(error (goto-char (match-end 0))))
(point)))
(when (member key keys)
(setq entry (buffer-substring beg end)
entries (cons entry entries)
keys (delete key keys))
;; check for crossref entries
(let* ((attr-list (reftex-parse-bibtex-entry nil beg end))
(xref-key (cdr (assoc "crossref" attr-list))))
(if xref-key (cl-pushnew xref-key keys)))
;; check for string references
(let* ((raw-fields (reftex-parse-bibtex-entry nil beg end t))
(string-fields (reftex-get-string-refs raw-fields)))
(dolist (skey string-fields)
(unless (member skey string-keys)
(push skey string-keys)))))))))))
;; second pass: grab @string references
(if string-keys
(save-current-buffer
(dolist (file files)
(set-buffer (reftex-get-file-buffer-force file 'mark))
(reftex-with-special-syntax-for-bib
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(while (re-search-forward
"^[ \t]*@[Ss][Tt][Rr][Ii][Nn][Gg][ \t]*{[ \t]*\\([^ \t\r\n]+\\)"
nil t)
(setq key (match-string 1)
beg (match-beginning 0)
end (progn
(goto-char (match-beginning 1))
(condition-case nil
(up-list 1)
(error (goto-char (match-end 0))))
(point)))
(when (member key string-keys)
(setq entry (buffer-substring beg end)
string-entries (cons entry string-entries)
string-keys (delete key string-keys))))))))))
(find-file-other-window bibfile)
(if (> (buffer-size) 0)
(unless (yes-or-no-p
(format "Overwrite non-empty file %s? " bibfile))
(error "Abort")))
(erase-buffer)
(if reftex-create-bibtex-header (insert reftex-create-bibtex-header "\n\n"))
(insert (mapconcat #'identity (reverse string-entries) "\n\n"))
(if string-entries (insert "\n\n\n"))
(insert (mapconcat #'identity (reverse entries) "\n\n"))
(if reftex-create-bibtex-footer (insert "\n\n" reftex-create-bibtex-footer))
(goto-char (point-min))
(save-buffer)
(message "%d entries extracted and copied to new database"
(length entries))))