Skip to content

Commit

Permalink
Use dplR::latexify to translate names to latex
Browse files Browse the repository at this point in the history
* Switch from tools::encoded_text_to_latex to dplR::latexify in
toBiblatex and toBibtex to fix translation of accented i characters
to latex
* Fixes #102, #106
  • Loading branch information
mwmclean committed Sep 25, 2024
1 parent 48f1e74 commit 766fd00
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: RefManageR
Version: 1.4.2
Version: 1.4.3
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 @@ -28,7 +28,8 @@ Imports:
lubridate (>= 1.5.0),
stringr,
methods,
bibtex (>= 0.4.1)
bibtex (>= 0.4.1),
dplR
Suggests:
knitr,
markdown,
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export(fields)
export(is.BibEntry)
export(toBiblatex)
importFrom(bibtex,do_read_bib)
importFrom(dplR,latexify)
importFrom(httr,GET)
importFrom(httr,POST)
importFrom(httr,add_headers)
Expand Down Expand Up @@ -82,7 +83,6 @@ importFrom(tools,Rd2txt)
importFrom(tools,Rd2txt_options)
importFrom(tools,bibstyle)
importFrom(tools,deparseLatex)
importFrom(tools,encoded_text_to_latex)
importFrom(tools,getBibstyle)
importFrom(tools,latexToUtf8)
importFrom(tools,loadPkgRdMacros)
Expand Down
8 changes: 4 additions & 4 deletions R/toBiblatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' 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}}.
#' translated to LaTeX escape sequences by \code{\link{latexify}}.
#' @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 @@ -87,15 +87,15 @@ toBiblatex <- function(object, encoded.names.to.latex = TRUE, ...){
rval
}

#' Wrapper for tools:: encoded_text_to_latex that returns original
#' Wrapper for dplr::latexify that returns original
#' text if translation to LaTeX fails
#' @importFrom tools encoded_text_to_latex
#' @importFrom dplR latexify
#' @noRd
#' @seealso \url{https://github.com/ropensci/RefManageR/issues/106}
EncodedNameListToLaTeX <- function(name.list, encoding = "UTF-8")
{
formatted.text <- format_author(name.list)
out <- encoded_text_to_latex(formatted.text, encoding)
out <- latexify(formatted.text, encoding)
if (grepl("^[{]?[?]", out))
return(formatted.text)
return(out)
Expand Down
2 changes: 1 addition & 1 deletion man/toBiblatex.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/test-tobiblatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ test_that("toBibtex doesn't replace CJK character name lists with ???? (#106)",
})


test_that("toBiblatex preserves correct i accent #102",
{
bib.str <- r"[@Article{Ryan-Hamaker-2021,
author = {Ryan Ois{\'i}n and Ellen L. Hamaker},
date = {2021-06},
journaltitle = {Psychometrika},
title = {Time to intervene: A continuous-time approach to network analysis and centrality},
doi = {10.1007/s11336-021-09767-0},
number = {1},
pages = {214--252},
volume = {87},
publisher = {Springer Science and Business Media {LLC}},
}]"

tfile <- tempfile(fileext = ".bib")
writeLines(bib.str, tfile)
bib <- ReadBib(tfile)
out <- toBiblatex(bib)
expect_true(grepl("\\'{\\i}", out[2], fixed = TRUE))
out <- toBibtex(bib)
expect_true(grepl("\\'{\\i}", out[2], fixed = TRUE))
})

bib <- BibEntry(bibtype = "Article", key = "smith2014", title = "An Article Title",
author = "Smith, Joé", journaltitle = "The Journal Title",
date = "2014-02-06", pubstate = "forthcoming")
Expand Down

0 comments on commit 766fd00

Please sign in to comment.