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
The “title” field is written in the title case with exceptations such as common prepositions with four or fewer letters. Special words such as “Bayesian” and “Markov” are protected by curly braces from case changing by BibTeX sytle.
The “author” field follows a consistent fashion: “family name, given name”. A period will be added to single letter acronym.
The “journal” field (if any) is written in the title case.
The “pages” field should use “–” instead of “-” between pages.
Usage
format_bibtex_entry(
entry,
fields = c("title", "author", "journal", "pages"),
protected_words = getOption("formatBibtex.protected_words"),
...
)
format_bibtex_file(
bibtex_file,
output_file = bibtex_file,
backup = (output_file == bibtex_file),
dry_run = FALSE,
...
)
Arguments
entry |
A |
fields |
A character vector specifying the fields to format. The
available options are |
protected_words |
Optional words that needs protection by curly braces from cases changing by BibTeX style. |
... |
Other arguments passed to |
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 |
dry_run |
A logical value. If |
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 bibentry
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)
## needs the package bibtex
has_bibtex <- requireNamespace("bibtex", quietly = TRUE)
## example of format_bibtex_entry()
if (has_bibtex) {
bib <- bibtex::read.bib(example_bib)
## check the default words that need protection by curly braces
(default_words <- getOption("formatBibtex.protected_words"))
format_bibtex_entry(bib, protected_words = c(default_words, "SMEM"))
}
## example of format_bibtex_file()
if (has_bibtex) {
output_file <- tempfile(fileext = ".bib")
format_bibtex_file(example_bib,
output_file = output_file,
protected_words = c(default_words, "SMEM"))
print(readLines(output_file), quote = FALSE)
}