# Best way to programatically create new bib note in org-roam v2?

**URL:** <https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954>\
**Category:** Development\
**Created:** [August 17, 2021, 3:01pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954 "2021-08-17T15:01:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [August 17, 2021, 3:01pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/1 "2021-08-17T15:01:03Z")

</div>

I’m currently in the process of removing (or at least making substantial progress towards removing) the dependency on bibtex-completion in bibtex-actions, and am wondering what the best way is to create a new bib note for org-roam v2?

Basically, I have a `bibtex-actions-open-notes` command, which now opens a note if it exists. But I want it (at least optionally) to create a new note if it doesn’t exist.

Any tips?

I guess I’d use `org-capture`?

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [August 17, 2021, 6:19pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/2 "2021-08-17T18:19:53Z")

</div>

Doesn’t `orb-edit-note` do the job? At least you can look at its code to see how it implements the desired behaviour. It checks whether the note exists and prepares and calls `org-roam-capture-` otherwise.

---

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [August 17, 2021, 6:24pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/3 "2021-08-17T18:24:52Z")

</div>

> [@mshevchuk](#):
>
> Doesn’t `orb-edit-note` do the job?

I hadn’t checked, but ideally I could indeed just do something like this:

```elisp
(setq bibtex-actions-file-edit-note-function 'orb-edit-note)

```

EDIT: yes, that’s exactly what I need!

Any chance you’ll be removing the `org-ref` dependency soon?

> <https://github.com/org-roam/org-roam-bibtex/pull/201>
>
> I decided to open this as a draft PR. It includes two commits ATM; to:
> 
> 1. to …replace \`org-ref\` in \`package-requires with \`org-mode "9.5"\` (though maybe better to just remove it?)
> 2. comments, prefaced with "BD ", on the rest of the code of what I think would be required to adapt ORB to org-cite.
> 
> I was thinking could be a basis of discussion, and I could drop the second commit if you wanted to remove the \`org-ref\` requirement in the meantime.
> 
> \--------------------
> 
> \## Follow processors?
> 
> The most interesting question is WTD about a possible follow processor, which is the UI that would integrate with \`org-open-at-point\`.
> 
> EDIT: actually, there are two questions; whether to do one (or more), and for what targets? I was assuming citations here, but could also be note links or some such.
> 
> For sure you \_could\_ do an ORB one.
> 
> But I wonder if we could find a way to have your action extension capability available in other obvious follow processors:
> 
> 1. \`bibtex-actions\` has one for \`embark-act\`
> 2. \`org-ref-cite\` has a hydra
> 3. I think @tmalsburg is working on one for \`helm-bibtex\`, I believe using helm actions
> 
> For \*\*1\*\*, I have an embark "target-finder" for org-cite citations and citation-references, so I am pretty sure all that is required is to write your own keymap and register that with embark.
> 
> \`\`\`elisp
> (add-to-list 'embark-keymap-alist '(oc-citation . oc-orb-map))
> \`\`\`
> That should expose the commands in \`embark-act\`, along with any other commands registered with that \`oc-citation\` category.
> 
> For \*\*2\*\*, see https://github.com/jkitchin/org-ref-cite/issues/4.

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [August 17, 2021, 6:28pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/4 "2021-08-17T18:28:34Z")

</div>

> [@bruce](#):
>
> EDIT: yes, that’s exactly what I need!

Good!

> [@bruce](#):
>
> Any chance you’ll be removing the `org-ref` dependency soon?

Not until `org-cite` citations scraping is implemented in Org-roam.

---

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [August 17, 2021, 6:31pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/5 "2021-08-17T18:31:14Z")

</div>

OK, I’'ll set it up so folks can do that, and note the option in the docstring.

Thanks!

---

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [August 20, 2021, 12:19am UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/6 "2021-08-20T00:19:21Z")

</div>

A related followup:

What’s your current thinking on how to best identify a bib note?

I have examples like this in updated v2 notes files:

```auto
:ROAM_REFS: cite:wallace-wells2019

```

So if I understand right, `ROAM_REFS` replaces `ROAM_KEY`, and there can be multiple.

What about the value of that for bib notes in v2, and with org-cite?

The above is I guess `org-ref` syntax. It turns out org-cite won’t (currently at least) allow a citation to be inserted there. Should it?

What is the ideal way to do this going forward?

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [August 20, 2021, 6:54pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/7 "2021-08-20T18:54:17Z")

</div>

> What’s your current thinking on how to best identify a bib note?

Through an external API. Either Org-ref or Org-cite or some other way. One should be able to take the string after fetching it from `ROAM_REFS` and prove it with the functions provided by the backend that it is indeed what Org-roam calls a `ref`. If it’s a `ref`, it should be cached in the sqlite database in the respective column. What is an Org-roam `ref` should be decided by Org-roam.

It’s then up to ORB to insert a valid string into `ROAM_REFS`—when the note is created through ORB—so that it can then be recognized as a `ref` by Org-roam and cached into the database. Currently, it inserts an `cite:citekey` link. It is trivial to make it insert a `[cite:@citekey]`. It is also up to ORB to be able to recognize a valid “bib note” in a `ROAM_REFS` string given to it, for `orb-note-actions` for example. Currently, ORB checks whether the string matches against `org-ref-cite-re` and recognizes it’s a bib note if if does match.

Org-roam actually doesn’t care much about Org-ref. What it calls a `ref` is just a valid `link` element. So any link in `ROAM_REFS` is recognized as a valid `ref`. Org-roam uses `org-element` to determine what’s in `ROAM_REFS`. If it is a link, it additionally checks it with `org-ref-cite-types`. If it is a member of that list, it does some processing to possibly split the cite keys and that’s it, nothing special about Org-ref. It does basically the same for other link types, so Org-roam recognizes a link as a valid `ref` as long as `org-element` (external API) is able to parse the string as an Org link.

To support `org-cite`, however, Org-roam should be able to recognize the Org element `citation` as a valid `ref` — to decide whether this should be cached into the database. It should be fairly easy to do though.

> So if I understand right, `ROAM_REFS` replaces `ROAM_KEY` , and there can be multiple.

Right. Also, by an unspoken convention ORB assumes that there is only one `cite:link` there. It doesn’t cover multiple.

> What about the value of that for bib notes in v2, and with org-cite?

A valid `citation` element that can be parsed by `org-element`.

> It turns out org-cite won’t (currently at least) allow a citation to be inserted there. Should it?

It cannot be inserted there by the interactive command `org-cite-insert`. Nothing prevents it to be inserted there programmatically by other means, nor for the string be read from there. There is one consequence, however. Currently, if one calls `org-open-at-point` while on a `cite:link` within `ROAM_REFS`, one gets the Org-ref hydra with familiar options to open PDF, go to the BibTeX entry, etc. Many users find this useful. With the current implementation of Org-cite in Org-mode, it seems that not only one cannot “officially” insert `org-cite` elements into property drawers, but they are also not recognized as such by `org-element`. This means that `org-cite-activate` or `org-cite-follow` processors don’t work there.

---

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [August 20, 2021, 7:25pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/8 "2021-08-20T19:25:13Z")

</div>

> [@mshevchuk](#):
>
> It cannot be inserted there by the interactive command `org-cite-insert` . Nothing prevents it to be inserted there programmatically by other means, nor for the string be read from there. There is one consequence, however. Currently, if one calls `org-open-at-point` while on a `cite:link` within `ROAM_REFS` , one gets the Org-ref hydra with familiar options to open PDF, go to the BibTeX entry, etc. Many users find this useful. With the current implementation of Org-cite in Org-mode, it seems that not only one cannot “officially” insert `org-cite` elements into property drawers, but they are also not recognized as such by `org-element` . This means that `org-cite-activate` or `org-cite-follow` processors don’t work there.

There’s another problem ATM, which there are other org-cite functions around export functionality based on these citations.

We wouldn’t want `org-cite-list-citations`, for example, to return these citations, as it could potentially screw up formatting. Basically, they should be ignored.

So if the constraints were loosened on this to allow them in property drawers, then other code would have to be adapted to ignore them.

Right?

If I understand all this correctly, I might ask on the org-mode list about this though.

EDIT: I just posted the question on the org list.

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [August 20, 2021, 10:24pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/9 "2021-08-20T22:24:24Z")

</div>

> [@bruce](#):
>
> So if the constraints were loosened on this to allow them in property drawers, then other code would have to be adapted to ignore them.

It depends on how exactly citations are collected for export. It’s possible to export without drawers, moreover people typically do not export drawers I guess. It’s actually easy to check.

But as I said, nothing prevents Org-roam from implementing support for `org-cite` citations. It will be fully working in terms of note indexing, backlinks and so on.

---

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [August 20, 2021, 10:47pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/10 "2021-08-20T22:47:59Z")

</div>

> It depends on how exactly citations are collected for export.

A function that iterates though the citation elements, and returns the unique ones. It doesn’t have to consider property drawers ATM because they’re not allowed there; hence no associated functionality. That’s all I’m saying.

> But as I said, nothing prevents Org-roam from implementing support for `org-cite` citations. It will be fully working in terms of note indexing, backlinks and so on.

Yes, I understand, but I’m asking a broader question here; about interop and such. I just wanted to confirm we wanted cites allowed in the property drawers before asking for it.

[https://lists.gnu.org/archive/html/emacs-orgmode/2021-08/msg00317.html](https://lists.gnu.org/archive/html/emacs-orgmode/2021-08/msg00317.html)

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [August 20, 2021, 11:24pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/11 "2021-08-20T23:24:30Z")

</div>

> [@bruce](#):
>
> I just wanted to confirm we wanted cites allowed in the property drawers before asking for it.

I didn’t say that. There may be a good reason why citations are not allowed in property drawers, and the export difficulties as you said may be one such reason. What I wanted to say is that neither Org-roam nor ORB need this as a technical requirement. The only reason this may be desirable is that some users, not sure how many, may find it useful to be able to activate a citation in the property drawer. I personally don’t need that, because I use the `orb-note-actions` interface, which in a broader sense acts as an “activate” processor for the citation key associated with the note.

---

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [August 21, 2021, 4:34pm UTC](https://org-roam.discourse.group/t/best-way-to-programatically-create-new-bib-note-in-org-roam-v2/1954/12 "2021-08-21T16:34:28Z")

</div>

> [@mshevchuk](#):
>
> What I wanted to say is that neither Org-roam nor ORB need this as a technical requirement. The only reason this may be desirable is that some users, not sure how many, may find it useful to be able to activate a citation in the property drawer.

Yes, I understand. But the latter point seems reason enough to ask the question, particularly given that links are allowed there. There have been a few other instances where the constraints have been loosened as a consequence.
