| Title: | R Interface to 'Zotero' via Better BibTeX |
|---|---|
| Description: | Connects R to 'Zotero' through the 'Better BibTeX' for 'Zotero' connector <https://retorque.re/zotero-better-bibtex/>. Provides functions to insert in-text citations and bibliography entries directly into documents, detect citations already present in R Markdown and Quarto files, and synchronise bibliography files. Includes an 'RStudio addin' for interactive use. |
| Authors: | Roshan Paladugu [aut, cre] |
| Maintainer: | Roshan Paladugu <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.0.0 |
| Built: | 2026-05-21 10:07:04 UTC |
| Source: | https://github.com/cran/zoteR |
These functions define what happens to the text retrieved from Zotero:
zt_return(text) zt_print(text) zt_insert(text) zt_copy(text)zt_return(text) zt_print(text) zt_insert(text) zt_copy(text)
text |
A character string. |
zt_return() returns the text invisibly (useful in scripts).
zt_print() writes the text to the console via cat().
zt_insert() inserts the text at the cursor position in RStudio, falling
back to zt_print() when RStudio is not available.
zt_copy() copies the text to the system clipboard.
All functions return text invisibly.
zt_print("[@doe2020]") zt_return("[@doe2020]")zt_print("[@doe2020]") zt_return("[@doe2020]")
Returns bibliography data for a vector of Better BibTeX citation keys.
zt_bib( keys, format = getOption("zoter.default.format", "biblatex"), library_id = getOption("zoter.default.library_id", 1L), .action = zt_return )zt_bib( keys, format = getOption("zoter.default.format", "biblatex"), library_id = getOption("zoter.default.library_id", 1L), .action = zt_return )
keys |
A character vector of Better BibTeX citation keys. |
format |
Bibliography format. One of |
library_id |
Integer library ID. Use |
.action |
What to do with the result. One of |
Bibliography text, returned according to .action.
Use "json" (CSL-JSON) unless you are using a raw TeX citation engine.
R Markdown converts other formats to CSL-JSON, and the conversion is lossy
for non-article reference types.
## Not run: zt_bib(c("doe2020", "smith2019"), format = "json") ## End(Not run)## Not run: zt_bib(c("doe2020", "smith2019"), format = "json") ## End(Not run)
Opens the Better BibTeX CAYW picker in Zotero and returns bibliography data for the chosen items in the requested format.
zt_bib_cayw( format = getOption("zoter.default.format", "biblatex"), .action = zt_return )zt_bib_cayw( format = getOption("zoter.default.format", "biblatex"), .action = zt_return )
format |
Bibliography format. One of |
.action |
What to do with the result. See |
Bibliography text, returned according to .action.
## Not run: zt_bib_cayw(format = "json", .action = zt_print) ## End(Not run)## Not run: zt_bib_cayw(format = "json", .action = zt_print) ## End(Not run)
Reads the YAML front matter of an R Markdown or Quarto document and returns
the value of the bibliography field. Relative paths are resolved relative
to the document's directory.
zt_bib_path(path_doc, encoding = "UTF-8")zt_bib_path(path_doc, encoding = "UTF-8")
path_doc |
Path to an |
encoding |
File encoding passed to |
An absolute path to the bibliography file.
Returns bibliography data for the items currently highlighted in the Zotero window.
zt_bib_selected( format = getOption("zoter.default.format", "biblatex"), .action = zt_return )zt_bib_selected( format = getOption("zoter.default.format", "biblatex"), .action = zt_return )
format |
Bibliography format. One of |
.action |
What to do with the result. One of |
Bibliography text, returned according to .action.
## Not run: zt_bib_selected(format = "json", .action = zt_print) ## End(Not run)## Not run: zt_bib_selected(format = "json", .action = zt_print) ## End(Not run)
Opens the Better BibTeX cite-as-you-write (CAYW) picker in Zotero and returns or inserts the formatted citation.
zt_cite(format = c("pandoc", "latex", "cite"), .action = zt_return)zt_cite(format = c("pandoc", "latex", "cite"), .action = zt_return)
format |
Citation format. One of |
.action |
What to do with the result. One of |
The citation string, returned according to .action.
## Not run: # Returns citation string zt_cite(format = "pandoc", .action = zt_return) ## End(Not run)## Not run: # Returns citation string zt_cite(format = "pandoc", .action = zt_return) ## End(Not run)
Scans one or more files for pandoc-style citation keys (@key) and returns
a character vector of unique keys found. Text inside code chunks, inline
R expressions, and URLs is intentionally excluded to avoid false positives.
zt_detect_citations(path = zt_knitr_input()) zt_detect_citations_text(text)zt_detect_citations(path = zt_knitr_input()) zt_detect_citations_text(text)
path |
A character vector of file paths to scan. When called inside a
knitting session, defaults to |
text |
A character string of document text (for programmatic use). |
To force a key to appear in the output (e.g. for a reference you mention
only in a caption), add it to an HTML comment or to the nocite front
matter field.
A character vector of unique citation keys (without the leading
@).
tmp <- tempfile(fileext = ".Rmd") writeLines(c( "See [@doe2020] and @smith2019.", "Not this one: \\@escaped or `r '@inline'`" ), tmp) zt_detect_citations(tmp) unlink(tmp) zt_detect_citations_text("See [@doe2020] and @smith2019.")tmp <- tempfile(fileext = ".Rmd") writeLines(c( "See [@doe2020] and @smith2019.", "Not this one: \\@escaped or `r '@inline'`" ), tmp) zt_detect_citations(tmp) unlink(tmp) zt_detect_citations_text("See [@doe2020] and @smith2019.")
Returns "latex" for .tex files and "pandoc" for everything else.
zt_guess_citation_format(path)zt_guess_citation_format(path)
path |
A file path. |
A character string, either "pandoc" or "latex".
zt_guess_citation_format("paper.Rmd") zt_guess_citation_format("paper.tex")zt_guess_citation_format("paper.Rmd") zt_guess_citation_format("paper.tex")
Inspects the file extension of path and returns the corresponding Better
BibTeX translator name.
zt_guess_format(path, dot_bib = getOption("zoter.default.bib", "biblatex"))zt_guess_format(path, dot_bib = getOption("zoter.default.bib", "biblatex"))
path |
A file path ending in |
dot_bib |
Format to use for |
A character string: one of "json", "biblatex", "bibtex", or
"yaml".
zt_guess_format("refs.json") zt_guess_format("refs.bib") zt_guess_format("refs.yaml")zt_guess_format("refs.json") zt_guess_format("refs.bib") zt_guess_format("refs.yaml")
zt_is_running() returns TRUE if Zotero is open and the Better BibTeX
add-on is active, FALSE otherwise. zt_assert_running() throws an
informative error if it is not.
zt_is_running() zt_assert_running()zt_is_running() zt_assert_running()
zt_is_running() returns a single logical value.
zt_assert_running() is called for its side-effect and returns NULL
invisibly.
zt_is_running()zt_is_running()
A thin wrapper around knitr::current_input() that throws an informative
error when called outside a knitr session.
zt_knitr_input()zt_knitr_input()
The path to the current knitr input file.
Returns a data frame with the id and name of all Zotero libraries
(personal library and any group libraries) accessible through Better BibTeX.
zt_libraries()zt_libraries()
A data frame with columns id (integer) and name (character).
## Not run: zt_libraries() ## End(Not run)## Not run: zt_libraries() ## End(Not run)
Convenience wrapper around zt_libraries() that returns the integer ID
for a named library. Useful when you need to pass library_id to
zt_bib() or zt_write_bib() in a way that is portable across machines
(where numeric IDs may differ).
zt_library_id(name)zt_library_id(name)
name |
A character vector of library names. |
An integer vector of library IDs, the same length as name.
Returns NA_integer_ for NA inputs.
## Not run: zt_library_id("My Library") ## End(Not run)## Not run: zt_library_id("My Library") ## End(Not run)
Detects citation keys in path_doc, then calls zt_write_bib() to
refresh the bibliography file declared in the document's YAML front matter
(or path_bib if supplied explicitly).
zt_update_bib( path_doc, path_bib = zt_bib_path(path_doc), ignore = character(), format = zt_guess_format(path_bib), library_id = getOption("zoter.default.library_id", 1L), overwrite = TRUE, filter = identity, quiet = FALSE )zt_update_bib( path_doc, path_bib = zt_bib_path(path_doc), ignore = character(), format = zt_guess_format(path_bib), library_id = getOption("zoter.default.library_id", 1L), overwrite = TRUE, filter = identity, quiet = FALSE )
path_doc |
Path to an |
path_bib |
Path to the bibliography file to update. Defaults to the
path declared in the document's |
ignore |
A character vector of keys to exclude. |
format |
Bibliography format (guessed from |
library_id |
Integer library ID (see |
overwrite |
Passed to |
filter |
Passed to |
quiet |
Use |
path_bib, invisibly.
## Not run: zt_update_bib("paper.Rmd") ## End(Not run)## Not run: zt_update_bib("paper.Rmd") ## End(Not run)
Fetches bibliography data for the given keys from Zotero and writes it to
path. Intended for use in YAML front matter to keep bibliography files in
sync automatically:
zt_write_bib( path, keys = zt_detect_citations(), ignore = character(), format = zt_guess_format(path), library_id = getOption("zoter.default.library_id", 1L), overwrite = FALSE, filter = identity )zt_write_bib( path, keys = zt_detect_citations(), ignore = character(), format = zt_guess_format(path), library_id = getOption("zoter.default.library_id", 1L), overwrite = FALSE, filter = identity )
path |
Path to the output file. The extension determines the default
|
keys |
A character vector of citation keys. Defaults to
|
ignore |
A character vector of keys to exclude from the output. |
format |
Bibliography format. One of |
library_id |
Integer library ID (see |
overwrite |
Use |
filter |
A function applied to the raw bibliography string before writing (e.g. to sort or post-process entries). |
bibliography: "`r zoteR::zt_write_bib('refs.json', overwrite = TRUE)`"
path, invisibly (for use in YAML front matter).
## Not run: zt_write_bib("refs.json", overwrite = TRUE) zt_write_bib("refs.bib", overwrite = TRUE) ## End(Not run)## Not run: zt_write_bib("refs.json", overwrite = TRUE) zt_write_bib("refs.bib", overwrite = TRUE) ## End(Not run)