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.
Arguments
- entry
A
bibentry
object (created byutils::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 isFALSE
.
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
.
Examples
library(formatBibtex)
## example BibTeX file that needs formatting
example_bib <- system.file("examples/example.bib", package = "formatBibtex")
print(readLines(example_bib), quote = FALSE)
#> [1] @article{andersen1982cox,
#> [2] title = {Cox's regression model for counting processes: a large sample study},
#> [3] author = {Andersen, Per Kragh and Gill, Richard D},
#> [4] journal = {the annals of statistics},
#> [5] volume = {10},
#> [6] number = {4},
#> [7] pages = {1100-1120},
#> [8] year = {1982},
#> [9] publisher = {JSTOR}
#> [10] }
#> [11]
#> [12] @book{chen2012monte,
#> [13] title={Monte Carlo methods in Bayesian computation},
#> [14] author={Chen, Ming-Hui and Shao, Qi-Man and Ibrahim, Joseph G},
#> [15] year={2012},
#> [16] publisher={Springer Science \\& Business Media}
#> [17] }
#> [18]
#> [19] @article{wu1983convergence,
#> [20] title={On the convergence properties of the EM algorithm},
#> [21] author={Wu, C.F. Jeff},
#> [22] journal={The Annals of statistics},
#> [23] pages={95--103},
#> [24] year={1983},
#> [25] publisher={JSTOR}
#> [26] }
## 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"))
}
#> @article{andersen1982cox,
#> title = {{Cox's} Regression Model for Counting Processes: {A} Large Sample Study},
#> author = {Andersen, Per Kragh and Gill, Richard D.},
#> journal = {The Annals of Statistics},
#> volume = {10},
#> number = {4},
#> pages = {1100--1120},
#> year = {1982},
#> publisher = {JSTOR}
#> }
#>
#> @book{chen2012monte,
#> title = {{Monte} {Carlo} Methods in {Bayesian} Computation},
#> author = {Chen, Ming-Hui and Shao, Qi-Man and Ibrahim, Joseph G.},
#> year = {2012},
#> publisher = {Springer Science \& Business Media}
#> }
#>
#> @article{wu1983convergence,
#> title = {On the Convergence Properties of the {EM} Algorithm},
#> author = {Wu, C. F. Jeff},
#> journal = {The Annals of Statistics},
#> pages = {95--103},
#> year = {1983},
#> publisher = {JSTOR}
#> }
## 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)
}
#> [1] @article{andersen1982cox,
#> [2] title = {{Cox's} Regression Model for Counting Processes: {A} Large
#> [3] Sample Study},
#> [4] author = {Andersen, Per Kragh and Gill, Richard D.},
#> [5] journal = {The Annals of Statistics},
#> [6] volume = 10,
#> [7] number = 4,
#> [8] pages = {1100--1120},
#> [9] year = 1982,
#> [10] publisher = {JSTOR}
#> [11] }
#> [12]
#> [13] @book{chen2012monte,
#> [14] title = {{Monte} {Carlo} Methods in {Bayesian} Computation},
#> [15] author = {Chen, Ming-Hui and Shao, Qi-Man and Ibrahim, Joseph G.},
#> [16] year = 2012,
#> [17] publisher = {Springer Science \\& Business Media}
#> [18] }
#> [19]
#> [20] @article{wu1983convergence,
#> [21] title = {On the Convergence Properties of the {EM} Algorithm},
#> [22] author = {Wu, C. F. Jeff},
#> [23] journal = {The Annals of Statistics},
#> [24] pages = {95--103},
#> [25] year = 1983,
#> [26] publisher = {JSTOR}
#> [27] }