format_bibtex_entry {formatBibtex}R Documentation

Format BibTeX Entries in An Opinionated Way

Description

This function tries to format the given BibTeX entries so that

Usage

format_bibtex_entry(
  entry,
  fields = c("title", "author", "journal", "pages"),
  protected_words = NULL,
  ...
)

format_bibtex_file(
  bibtex_file,
  output_file = bibtex_file,
  backup = (output_file == bibtex_file),
  dry_run = FALSE,
  ...
)

Arguments

entry

A bibentry object (created by utils::bibentry) representing BibTeX entries.

fields

A character vector specifying the fields to format. The available options are "title", "author", "journal", and "pages". Multiple choices can be specified.

protected_words

Optional words that needs protection by curly braces from cases changing by BibTeX style.

...

Other arguments passed to format_bibtex_entry.

bibtex_file

A character string presenting the BibTeX file that needs formatting.

output_file

A character string presenting the output BibTeX file. By default, the input BibTeX file will be overwritten with a backup file.

backup

A logical value. If TRUE, a backup file will be created to check and tweak the formatting options.

dry_run

A logical value. If TRUE, the formatted BibTeX entries will be returned without actually (over)writing a BibTeX file to the disk. The default value is FALSE.

Details

When emacs is available in the system, the function format_bibtex_file() will perform additional formatting with the help of the commands bibtex-reformat and bibtex-sort-buffer.

Value

A bibtex object.

Examples

library(formatBibtex)

## example BibTeX file that needs formatting
example_bib <- system.file("examples/example.bib", package = "formatBibtex")
print(readLines(example_bib), quote = FALSE)

## check the default words that need protection by curly braces
format_options$get("protected_words")
## append "SMEM" to the list
format_options$append("protected_words", c("SMEM"))

## needs the package bibtex
if (requireNamespace("bibtex", quietly = TRUE)) {
    ## example of format_bibtex_entry()
    bib <- bibtex::read.bib(example_bib)
    format_bibtex_entry(bib)
    ## example of format_bibtex_file()
    output_file <- tempfile(fileext = ".bib")
    format_bibtex_file(example_bib, output_file = output_file)
    print(readLines(output_file), quote = FALSE)
}

[Package formatBibtex version 0.1.1 Index]