Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdarcus/0f9c545ba91fd40f8a9300c138f4c44b to your computer and use it in GitHub Desktop.
Save bdarcus/0f9c545ba91fd40f8a9300c138f4c44b to your computer and use it in GitHub Desktop.

Converting my bibliography file to roam format

[2020-02-18 Tue]

I use this function to create a note file from a bibtex entry (the point is on the bibtex entry).

;;** Open notes from bibtex entry
;;;###autoload
(defun aah/org-ref-open-bibtex-notes ()
  "From a bibtex entry, open the notes if they exist.
If the notes do not exist, then create a heading.

This function is forked from the original to support storing
each note in its own file.
"
  (interactive)

  (bibtex-beginning-of-entry)
  (let* ((cb (current-buffer))
         (bibtex-expand-strings t)
         (entry (cl-loop for (key . value) in (bibtex-parse-entry t)
                         collect (cons (downcase key) (s-collapse-whitespace value))))
         (key (reftex-get-bib-field "=key=" entry))
	 (note-path (expand-file-name (concat key ".org") org-roam-directory)))

    ;; save key to clipboard to make saving pdf later easier by pasting.
    (with-temp-buffer
      (insert key)
      (kill-ring-save (point-min) (point-max)))

    ;; now look for entry in the notes file
    (save-restriction
      (find-file-other-window note-path)
      (when (not (file-exists-p note-path))
	;; no entry found, so add one
	(goto-char (point-max))
	(insert (org-ref-reftex-format-citation
		 entry org-ref-note-title-format))
	(mapc (lambda (x)
		(save-restriction
		  (save-excursion
		    (funcall x))))
	      org-ref-create-notes-hook)
	(save-buffer)))))

I changed the following in my org-ref setup:

(use-package org-ref
  (... snipped ... )
  :config
  (setq org-ref-notes-function 'org-ref-notes-function-many-files)
  (setq org-ref-notes-directory "~/org/roam/")
  (setq bibtex-completion-notes-path "~/org/roam/")
  (setq org-ref-note-title-format "#+TITLE: Notes for %k
#+SUBTITLE: @@latex: %t @@
#+AUTHOR: @@latex: %9a @@

#+SETUPFILE: ../bib_notes_common.org

|---------+--------------|
| *Meta*  |              |
| Journal | %j           |
| Year    | %y           |
| Volume  | %v           |
| Pages   | %p           |
| DOI     | %D           |
| Url     | %U           |
| Added   | TIME_ADDED   |
|---------+--------------|

- Tags ::

")
  (setq org-ref-create-notes-hook nil)
  (add-hook 'org-ref-create-notes-hook
            (lambda ()
              (goto-char (point-max))
              (search-backward "TIME_ADDED")
              (kill-line)
              (org-time-stamp '(16) t)	;; add current time
              (insert " |")		;; finish row
              (org-cycle)		;; layout table
              (move-beginning-of-line nil)
              (kill-line 1)
              (goto-char (point-max)))))

I used a macro to convert each heading to a separate file and perform a git commit with a reasonable message.

This macro allowed me to move roughly 300 papers in 45 minutes, which at 6 papers/minute is a reasonable score.

In each header, you put the point on a cite:year-title link that describes the current header. The macro

  1. stores the point in register SPC,
  2. copies the subtree,
  3. moves back to the link,
  4. opens the bib entry,
  5. creates a new note,
  6. yanks the copied subtree into the note,
  7. moves back to the link
  8. deletes the subtree
  9. search-forwards for the next cite: occurence
  10. Opens the magit status window
  11. Compiles C-x C-m the command git add bibliography.org roam
  12. Compiles C-x C-m the command =git commit -m “Move year-title notes to own file”=.
  13. Moves back to link
;; Keyboard Macro Editor.  Press C-c C-c to finish; press C-x k RET to cancel.
;; Original keys: C-r c i t e : RET C-x r SPC SPC C-s c i t e : RET C-SPC C-s SPC RET C-b C-x r s k C-r c i t e : RET C-c @ C-x r s z C-x r j SPC C-c C-o RET C-c b n RET C-x r i z C-x C-s C-x r j SPC C-c @ C-w C-x C-s C-s c i t e : RET C-x g C-x RET C-a C-k g i t SPC a 2*d SPC b i b l i o g r a p h y . o r g SPC r o a m RET C-x RET M-n g i t SPC c o 2*m i t SPC - m SPC " M o v e SPC C-x r i k DEL SPC M-\ SPC n o t e s SPC t o SPC o w n SPC f i l e 2*" DEL RET C-x r j SPC C-s c i t e : RET

Command: last-kbd-macro
Key: none

Macro:

C-r			;; isearch-backward
c			;; self-insert-command
i			;; self-insert-command
t			;; self-insert-command
e			;; self-insert-command
:			;; self-insert-command
RET			;; org-return
C-x r SPC		;; point-to-register
SPC			;; self-insert-command
C-s			;; isearch-forward
c			;; self-insert-command
i			;; self-insert-command
t			;; self-insert-command
e			;; self-insert-command
:			;; self-insert-command
RET			;; org-return
C-SPC			;; set-mark-command
C-s			;; isearch-forward
SPC			;; self-insert-command
RET			;; org-return
C-b			;; backward-char
C-x r s			;; copy-to-register
k			;; self-insert-command
C-r			;; isearch-backward
c			;; self-insert-command
i			;; self-insert-command
t			;; self-insert-command
e			;; self-insert-command
:			;; self-insert-command
RET			;; org-return
C-c @			;; org-mark-subtree
C-x r s			;; copy-to-register
z			;; self-insert-command
C-x r j			;; jump-to-register
SPC			;; self-insert-command
C-c C-o			;; org-open-at-point
RET			;; org-return
C-c b			;; org-iswitchb
n			;; self-insert-command
RET			;; org-return
C-x r i			;; insert-register
z			;; self-insert-command
C-x C-s			;; save-buffer
C-x r j			;; jump-to-register
SPC			;; self-insert-command
C-c @			;; org-mark-subtree
C-w			;; kill-region
C-x C-s			;; save-buffer
C-s			;; isearch-forward
c			;; self-insert-command
i			;; self-insert-command
t			;; self-insert-command
e			;; self-insert-command
:			;; self-insert-command
RET			;; org-return
C-x g			;; magit-status
C-x RET			;; compile
C-a			;; move-beginning-of-line
C-k			;; kill-line
g			;; self-insert-command
i			;; self-insert-command
t			;; self-insert-command
SPC			;; self-insert-command
a			;; self-insert-command
2*d			;; self-insert-command
SPC			;; self-insert-command
b			;; self-insert-command
i			;; self-insert-command
b			;; self-insert-command
l			;; self-insert-command
i			;; self-insert-command
o			;; self-insert-command
g			;; self-insert-command
r			;; self-insert-command
a			;; self-insert-command
p			;; self-insert-command
h			;; self-insert-command
y			;; self-insert-command
.			;; self-insert-command
o			;; self-insert-command
r			;; self-insert-command
g			;; self-insert-command
SPC			;; self-insert-command
r			;; self-insert-command
o			;; self-insert-command
a			;; self-insert-command
m			;; self-insert-command
RET			;; org-return
C-x RET			;; compile
M-n
g			;; self-insert-command
i			;; self-insert-command
t			;; self-insert-command
SPC			;; self-insert-command
c			;; self-insert-command
o			;; self-insert-command
2*m			;; self-insert-command
i			;; self-insert-command
t			;; self-insert-command
SPC			;; self-insert-command
-			;; self-insert-command
m			;; self-insert-command
SPC			;; self-insert-command
"			;; self-insert-command
M			;; self-insert-command
o			;; self-insert-command
v			;; self-insert-command
e			;; self-insert-command
SPC			;; self-insert-command
C-x r i			;; insert-register
k			;; self-insert-command
DEL			;; delete-backward-char
SPC			;; self-insert-command
M-\			;; delete-horizontal-space
SPC			;; self-insert-command
n			;; self-insert-command
o			;; self-insert-command
t			;; self-insert-command
e			;; self-insert-command
s			;; self-insert-command
SPC			;; self-insert-command
t			;; self-insert-command
o			;; self-insert-command
SPC			;; self-insert-command
o			;; self-insert-command
w			;; self-insert-command
n			;; self-insert-command
SPC			;; self-insert-command
f			;; self-insert-command
i			;; self-insert-command
l			;; self-insert-command
e			;; self-insert-command
2*"			;; self-insert-command
DEL			;; delete-backward-char
RET			;; org-return
C-x r j			;; jump-to-register
SPC			;; self-insert-command
C-s			;; isearch-forward
c			;; self-insert-command
i			;; self-insert-command
t			;; self-insert-command
e			;; self-insert-command
:			;; self-insert-command
RET			;; org-return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment