Title: R Interface to 'Zotero' via Better BibTeX
Version: 1.0.0
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.
License: GPL (≥ 3)
Encoding: UTF-8
RoxygenNote: 7.3.3
Depends: R (≥ 4.1.0)
Imports: httr2, jsonlite, rstudioapi, clipr, yaml, tools
Suggests: knitr, rmarkdown
NeedsCompilation: no
Packaged: 2026-04-19 15:12:47 UTC; roshan
Author: Roshan Paladugu [aut, cre]
Maintainer: Roshan Paladugu <rpaladugu@sassyspace.xyz>
Repository: CRAN
Date/Publication: 2026-04-21 20:10:02 UTC

Handle bibliography or citation text

Description

These functions define what happens to the text retrieved from Zotero:

Usage

zt_return(text)

zt_print(text)

zt_insert(text)

zt_copy(text)

Arguments

text

A character string.

Details

Value

All functions return text invisibly.

Examples

zt_print("[@doe2020]")
zt_return("[@doe2020]")

Fetch bibliography data for specific citation keys

Description

Returns bibliography data for a vector of Better BibTeX citation keys.

Usage

zt_bib(
  keys,
  format = getOption("zoter.default.format", "biblatex"),
  library_id = getOption("zoter.default.library_id", 1L),
  .action = zt_return
)

Arguments

keys

A character vector of Better BibTeX citation keys.

format

Bibliography format. One of "json" (CSL-JSON; recommended), "biblatex", "bibtex", or "yaml" (CSL-YAML). Defaults to the value of getOption("zoter.default.format", "biblatex").

library_id

Integer library ID. Use zt_library_id() to look up the ID by name. Defaults to getOption("zoter.default.library_id", 1L).

.action

What to do with the result. One of zt_return(), zt_print(), zt_insert(), or zt_copy().

Value

Bibliography text, returned according to .action.

Recommended format

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.

Examples

## Not run: 
zt_bib(c("doe2020", "smith2019"), format = "json")

## End(Not run)

Fetch a bibliography via cite-as-you-write

Description

Opens the Better BibTeX CAYW picker in Zotero and returns bibliography data for the chosen items in the requested format.

Usage

zt_bib_cayw(
  format = getOption("zoter.default.format", "biblatex"),
  .action = zt_return
)

Arguments

format

Bibliography format. One of "json" (CSL-JSON; recommended), "biblatex", "bibtex", or "yaml" (CSL-YAML). Defaults to the value of getOption("zoter.default.format", "biblatex").

.action

What to do with the result. See zt_return().

Value

Bibliography text, returned according to .action.

Examples

## Not run: 
zt_bib_cayw(format = "json", .action = zt_print)

## End(Not run)

Extract the bibliography file path from a document's YAML front matter

Description

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.

Usage

zt_bib_path(path_doc, encoding = "UTF-8")

Arguments

path_doc

Path to an .Rmd or .qmd file.

encoding

File encoding passed to readLines().

Value

An absolute path to the bibliography file.


Fetch bibliography for currently selected Zotero items

Description

Returns bibliography data for the items currently highlighted in the Zotero window.

Usage

zt_bib_selected(
  format = getOption("zoter.default.format", "biblatex"),
  .action = zt_return
)

Arguments

format

Bibliography format. One of "json" (CSL-JSON; recommended), "biblatex", "bibtex", or "yaml" (CSL-YAML). Defaults to the value of getOption("zoter.default.format", "biblatex").

.action

What to do with the result. One of zt_return(), zt_print(), zt_insert(), or zt_copy().

Value

Bibliography text, returned according to .action.

Examples

## Not run: 
zt_bib_selected(format = "json", .action = zt_print)

## End(Not run)

Insert a Zotero citation using cite-as-you-write

Description

Opens the Better BibTeX cite-as-you-write (CAYW) picker in Zotero and returns or inserts the formatted citation.

Usage

zt_cite(format = c("pandoc", "latex", "cite"), .action = zt_return)

Arguments

format

Citation format. One of "pandoc" (default; produces ⁠[@key]⁠), "latex" (produces ⁠\cite{key}⁠), or "cite" (produces ⁠\cite{key}⁠ for LaTeX documents).

.action

What to do with the result. One of zt_return(), zt_print(), zt_insert(), or zt_copy(). Defaults to zt_insert() when called from the RStudio addin and zt_return() otherwise.

Value

The citation string, returned according to .action.

Examples

## Not run: 
# Returns citation string
zt_cite(format = "pandoc", .action = zt_return)

## End(Not run)

Detect pandoc-style citation keys in a document

Description

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.

Usage

zt_detect_citations(path = zt_knitr_input())

zt_detect_citations_text(text)

Arguments

path

A character vector of file paths to scan. When called inside a knitting session, defaults to zt_knitr_input().

text

A character string of document text (for programmatic use).

Details

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.

Value

A character vector of unique citation keys (without the leading @).

Examples

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.")

Low-level HTTP helpers for the Better BibTeX API

Description

zt_get() sends a GET request to a Better BibTeX endpoint and returns the response body as a character string. zt_json_rpc() sends a JSON-RPC 2.0 POST request and returns the parsed response list.

Usage

zt_get(endpoint, ...)

zt_json_rpc(method, params = list())

Arguments

endpoint

A Better BibTeX endpoint name (appended to the base URL).

...

Named or unnamed query parameters. Named parameters are encoded as key=value pairs; a single unnamed parameter is appended directly to the query string.

method

A JSON-RPC method name (e.g. "item.export").

params

A list of positional parameters for the JSON-RPC call.

Value

zt_get() returns a character string. zt_json_rpc() returns a list with elements result (on success) or error (on failure).


Guess the citation format from a file path

Description

Returns "latex" for .tex files and "pandoc" for everything else.

Usage

zt_guess_citation_format(path)

Arguments

path

A file path.

Value

A character string, either "pandoc" or "latex".

Examples

zt_guess_citation_format("paper.Rmd")
zt_guess_citation_format("paper.tex")

Guess the bibliography file format from a file path

Description

Inspects the file extension of path and returns the corresponding Better BibTeX translator name.

Usage

zt_guess_format(path, dot_bib = getOption("zoter.default.bib", "biblatex"))

Arguments

path

A file path ending in .json, .bib, or .yaml/.yml.

dot_bib

Format to use for .bib files: "biblatex" (default) or "bibtex". Override the default with options(zoter.default.bib = "bibtex").

Value

A character string: one of "json", "biblatex", "bibtex", or "yaml".

Examples

zt_guess_format("refs.json")
zt_guess_format("refs.bib")
zt_guess_format("refs.yaml")

Check whether the Better BibTeX HTTP endpoint is reachable

Description

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.

Usage

zt_is_running()

zt_assert_running()

Value

zt_is_running() returns a single logical value. zt_assert_running() is called for its side-effect and returns NULL invisibly.

Examples

zt_is_running()

Return the current knitr input file

Description

A thin wrapper around knitr::current_input() that throws an informative error when called outside a knitr session.

Usage

zt_knitr_input()

Value

The path to the current knitr input file.


List available Zotero libraries

Description

Returns a data frame with the id and name of all Zotero libraries (personal library and any group libraries) accessible through Better BibTeX.

Usage

zt_libraries()

Value

A data frame with columns id (integer) and name (character).

Examples

## Not run: 
zt_libraries()

## End(Not run)

Look up a Zotero library ID by name

Description

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).

Usage

zt_library_id(name)

Arguments

name

A character vector of library names.

Value

An integer vector of library IDs, the same length as name. Returns NA_integer_ for NA inputs.

Examples

## Not run: 
zt_library_id("My Library")

## End(Not run)

Update a bibliography file from an R Markdown / Quarto document

Description

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).

Usage

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
)

Arguments

path_doc

Path to an .Rmd or .qmd file.

path_bib

Path to the bibliography file to update. Defaults to the path declared in the document's bibliography front matter field, resolved relative to path_doc.

ignore

A character vector of keys to exclude.

format

Bibliography format (guessed from path_bib by default).

library_id

Integer library ID (see zt_library_id()).

overwrite

Passed to zt_write_bib().

filter

Passed to zt_write_bib().

quiet

Use TRUE to suppress the success message.

Value

path_bib, invisibly.

Examples

## Not run: 
zt_update_bib("paper.Rmd")

## End(Not run)

Write a bibliography file from Zotero citation keys

Description

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:

Usage

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
)

Arguments

path

Path to the output file. The extension determines the default format (.json"json", .bib"biblatex", .yaml"yaml").

keys

A character vector of citation keys. Defaults to zt_detect_citations() when called inside a knitr session.

ignore

A character vector of keys to exclude from the output.

format

Bibliography format. One of "json", "biblatex", "bibtex", or "yaml". Guessed from path by default via zt_guess_format().

library_id

Integer library ID (see zt_library_id()).

overwrite

Use TRUE to overwrite an existing file.

filter

A function applied to the raw bibliography string before writing (e.g. to sort or post-process entries).

Details

bibliography: "`r zoteR::zt_write_bib('refs.json', overwrite = TRUE)`"

Value

path, invisibly (for use in YAML front matter).

Examples

## Not run: 
zt_write_bib("refs.json", overwrite = TRUE)
zt_write_bib("refs.bib",  overwrite = TRUE)

## End(Not run)