ropensci / RefManageR

R package RefManageR
https://docs.ropensci.org/RefManageR
Other
114 stars 24 forks source link

Allow leading "@" symbol in the bib keys (pandoc-style) #80

Open dmi3kno opened 3 years ago

dmi3kno commented 3 years ago

I am using rbbt add-in to paste keys from Zotero and they get pasted together with @ symbol in front. These symbols are also used by rbbt::bbt_detect_citations() to extract citation keys from text, which I use to automatically create .bib file.

I would like to be able to pass they keys with @ symbol into RefManageR::Cite() (and by extension into Citet() and Citep()). This requires modifying RefManageR::SearchBib() with something like:

  keys <- gsub("^@", "", keys)

P.S. I am aware of the Zotero workflow offered by {RefManageR}, but I still prefer interactivity of rbbt and the fact that I do not have to export references by hand.

mwmclean commented 3 years ago

Since bibtex::read.bib() (and hence RefManageR::ReadBib() which uses bibtex::do_read_bib()) doesn't support "@" symbols in keys, you may want to open an issue there instead. ReadZotero will have the same issue since it also uses ReadBib. Can you please provide a simple, reproducible example demonstrating what error(s) you are getting? For example the following works fine for me.

bib <- RefManageR::BibEntry(bibtype = "Misc", title = "Hello World!", author = "John Wick", year = 2012, key = "a@b")
bib[["a@b"]]
dmi3kno commented 3 years ago

Sorry I did not make myself clear. I want to be able to do this

bib <- RefManageR::BibEntry(bibtype = "Misc", title = "Hello World!", 
                            author = "John Wick", year = 2012, 
                            key = "wick2012HelloWorld")
bib[["@wick2012HelloWorld"]]
#> Warning message:
#> In `[[.BibEntry`(bib, "@wick2012HelloWorld") : subscript out of bounds
RefManageR::Citep(bib, "@wick2012HelloWorld")
#> [1] ""
RefManageR::Citet(bib, "@wick2012HelloWorld")
#> [1] ""

You would say that I am misspecifying the key and you will be correct. But I need my key to look like that (pandoc-style) because I use {rbbt} to rbbt::bbt_detect_citations() and write the .bib file for me automatically. And in that function the @ symbol acts like a hook.

What I am asking is, could RefManageR allow leading @ symbol in the key and look away from it (disregard it) when querying the bibentry object?

Your objection might be, "well, what if user has specified bibentry key with leading @ ?" Fair enough but I wonder how would pandoc specification look like for those entries [@@wick2012HelloWorld]. Is that legal?

One more idea then. How about we make options(RefManageR.key.style="pandoc") which would turn on this key handling I am asking for?

kamapu commented 3 years ago

I was looking for a similar functionality and started this discussion. At the end I came out with the use of stringr::str_extract(), where I just paste the "@"-symbol for the search.

I'm working in an own package to interact with bibtex libraries and implemented this search in the function match_keys(), where you don't need to add the "@" to bibtexkeys. I will implement a "character-method" to allow the interaction with RefManageR, although I'm wondering why this project is currently out of CRAN.

Of course, the function and the package are experiments for the moment and will be happy to get any comments.

dmi3kno commented 3 years ago

That's what rbbt::bbt_detect_citations() is doing, but it a bit more intelligent way (look at regex here)

kamapu commented 3 years ago

I'm sorry for intruding in this discussion, but I don't really see a necessity of including the @ symbol in the names of bib-items since you can easily use paste0("@", bibtexkey) when required. Additionally, rbbt::bbt_detect_citations() will retrieve bibtexkeys without the @-symbol.

Here an example comparing the usage of biblio (I just wrote a method for character values).

library(biblio)
library(RefManageR)
library(rbbt)

Bib <- ReadBib(file=file.path(path.package("biblio"), "LuebertPliscoff.bib"))
match_keys(x=names(Bib), rmd_file=file.path(path.package("biblio"), "document.Rmd"))

If you just need the list of the bibtexkeys:

unique(match_keys(x=names(Bib), rmd_file=file.path(path.package("biblio"), "document.Rmd"))$bibtexkey)

Of course, the rbbt option is farther elegant and faster:

bbt_detect_citations(path=file.path(path.package("biblio"), "document.Rmd"))
dmi3kno commented 3 years ago

The big difference is that rbbt does not assume the existence of a .bib file. In fact bbt_detect_cittation() is used to scrape keys which will then be used for (just-in-time) fetching of the records from Zotero and exporting the .bib entries (thus composing the said .bib file).

The workflow between biblio and rbbt is different. I need to find words in .Rmd which "look like" references ("detect" phase), so that I can compose a .bib file for them from the database in Zotero ("fetch" and "write" phases). The "@" prefix signals to rbbt that the following sequence of characters may be a valid key. Once .bib file is ready, biblio and RefManageR will kick in ("read" and "render" phase).

In pandoc there's no issue with references, because it will strip the [@key] entries of the "@" sign in the process of knitting the document, query the .bib file and style the citation with appropriate csl. When we are doing "pre-rendering" of citation (as the case is with RefManageR) or when pandoc workflow is unavailable (as in my case with xaringan), the "detect-fetch-write-read-render" workflow falls apart because there are no "hooks" in text to detect citations by.

You could argue that rbbt could try and detect Cite?(?+, " (i.e. detect the "RefManageR" call), but if the function is overloaded (wrapped) then the detection will fail.

TL;DR: All I am asking, could we please harmonize the UX of RefManageR with pandoc, so that the user could seamlessly switch between [@wick2012HelloWorld] in pandoc-style .Rmd and r Citep(my_bib, "@wick2012HelloWorld") in xaringan? Note that the key is exactly the same, wrapped into [] in one case or some R code in another.