Skip to content

Commit

Permalink
Add argument to toBiblatex and toBibtex
Browse files Browse the repository at this point in the history
* Add argument encoded.names.to.latex to toBibTeX and toBibLaTeX to
toggle whether tools::encoded_text_to_latex is called on name list fields
* Closes #105
  • Loading branch information
mwmclean committed Sep 23, 2024
1 parent 5ca9858 commit 02f43d2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: RefManageR
Version: 1.4.1
Version: 1.4.2
Title: Straightforward 'BibTeX' and 'BibLaTeX' Bibliography Management
Authors@R: person(c("Mathew", "W."), "McLean", role = c("aut", "cre"),
email = "[email protected]",
Expand Down Expand Up @@ -39,4 +39,4 @@ Depends:
VignetteBuilder: knitr
BugReports: https://github.com/ropensci/RefManageR/issues
URL: https://github.com/ropensci/RefManageR/
RoxygenNote: 7.2.1
RoxygenNote: 7.3.2
13 changes: 9 additions & 4 deletions R/toBiblatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#' @param extra.fields character vector; fields that are not supported in standard BibTeX styles are by default dropped
#' in the result return by the toBibtex function.
#' Any fields specified in extra.fields will \emph{not} be dropped if present in an entry.
#' @param encoded.names.to.latex if \code{TRUE} (the default) then name list fields
#' such as \sQuote{author} and \sQuote{editor} will have non-ASCII characters
#' translated to LaTeX escape sequences by \code{\link{encoded_text_to_latex}}.
#' @param ... ignored
#' @export
#' @return an object of class \dQuote{Bibtex} - character vectors where each element holds one line of a BibTeX or BibLaTeX file
Expand Down Expand Up @@ -60,14 +63,16 @@
#' toBiblatex(bib[70:72])
#' toBibtex(bib[70:72])
#' }
toBiblatex <- function(object, ...){
toBiblatex <- function(object, encoded.names.to.latex = TRUE, ...){
format_bibentry1 <- function(object) {
object <- unclass(object)[[1L]]
rval <- paste0("@", attr(object, "bibtype"), "{", attr(object,
"key"), ",")
nl.ind <- which(names(object) %in% .BibEntryNameList)
for (i in nl.ind)
object[i] <- EncodedNameListToLaTeX(object[[i]])
if (encoded.names.to.latex) {
nl.ind <- which(names(object) %in% .BibEntryNameList)
for (i in nl.ind)
object[i] <- EncodedNameListToLaTeX(object[[i]])
}
rval <- c(rval, vapply(names(object), function(n) paste0(" ",
n, " = {", object[[n]], "},"), ""), "}", "")
return(rval)
Expand Down
23 changes: 15 additions & 8 deletions R/toBibtex.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
toBibtex.BibEntry <- function(object,
note.replace.field = c('urldate', "pubsate",
"addendum"),
extra.fields = NULL, ...){
extra.fields = NULL,
encoded.names.to.latex = TRUE,
...){


object <- .BibEntry_expand_crossrefs(unclass(object), to.bibtex = TRUE)
if (length(object)) {
object$.index <- NULL
rval <- head(unlist(lapply(object, ConvertToBibtex,
note.replace.field, extra.fields)),
note.replace.field, extra.fields,
encoded.names.to.latex)),
-1L)
}
else rval <- character()
Expand All @@ -21,16 +24,20 @@ toBibtex.BibEntry <- function(object,
}

#' @noRd
ConvertToBibtex <- function(object, note.replace.field, extra.fields){
ConvertToBibtex <- function(object,
note.replace.field,
extra.fields,
encoded.names.to.latex) {
object <- unclass(object)[[1L]]
bibtype <- tolower(attr(object, "bibtype"))
obj.names <- names(object)
if ("author" %in% obj.names)
object$author <- EncodedNameListToLaTeX(object$author)
if ("editor" %in% obj.names)
object$editor <- EncodedNameListToLaTeX(object$editor)
if (encoded.names.to.latex) {
if ("author" %in% obj.names)
object$author <- EncodedNameListToLaTeX(object$author)
if ("editor" %in% obj.names)
object$editor <- EncodedNameListToLaTeX(object$editor)
}
# see 2.3 Usage Notes p. 28

if (bibtype == "article" && 'journaltitle' %in% obj.names &&
is.null(object$journal))
object$journal <- object$journaltitle
Expand Down
4 changes: 3 additions & 1 deletion inst/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Changes in Version 1.4.1 (2024-09-23)
* Fix for `toBibLaTeX` and `toBibTeX` non-ASCII name list fields. Add escape
hatch if `tools::encoded_text_to_latex` fails to convert name lists to valid
LaTeX (observed for Japanese names in #106 h/t @kijinosu).

* Add argument `encoded.names.to.latex` to `toBibTeX` and `toBibLaTeX` to
toggle whether `tools::encoded_text_to_latex` is called on name list fields
(requested in #105)

Changes in Version 1.4.0 (2022-09-30)
========================================================
Expand Down
7 changes: 6 additions & 1 deletion man/toBiblatex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/testthat/test-tobiblatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,27 @@ test_that("toBibtex doesn't replace CJK character name lists with ???? (#106)",
out <- toBibtex(bib)
expect_false(any(grepl("[?]", out)))
})


bib <- BibEntry(bibtype = "Article", key = "smith2014", title = "An Article Title",
author = "Smith, Joé", journaltitle = "The Journal Title",
date = "2014-02-06", pubstate = "forthcoming")
test_that("toBiblatex can toggle encoding names to LaTeX (#105)",
{
out <- toBiblatex(bib, encoded.names.to.latex = TRUE)
out.author = out[3]
expect_true(grepl("{\\a'e}", out.author, fixed = TRUE))
out <- toBiblatex(bib, encoded.names.to.latex = FALSE)
out.author = out[3]
expect_false(grepl("{\\a'e}", out.author, fixed = TRUE))
})

test_that("toBibtex can toggle encoding names to LaTeX (#105)",
{
out <- toBibtex(bib, encoded.names.to.latex = TRUE)
out.author = out[3]
expect_true(grepl("{\\a'e}", out.author, fixed = TRUE))
out <- toBibtex(bib, encoded.names.to.latex = FALSE)
out.author = out[3]
expect_false(grepl("{\\a'e}", out.author, fixed = TRUE))
})

0 comments on commit 02f43d2

Please sign in to comment.