Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make.bib.entry fails to parse journaltitle from BibLaTeX-oriented files #57

Open
Fifis opened this issue Dec 29, 2024 · 2 comments
Open

Comments

@Fifis
Copy link

Fifis commented Dec 29, 2024

During debugging, I came across another unfortunate instance of bibtex not handling newer bibliographies that are served by large publishers under the labels ‘BIB’, ‘BIBTeX’ etc. but are designed for BIBLaTeX instead. Consider this:

> library(bibtex)
> d <- read.bib
ignoring entry 'dagsvik2011estimating' (line 768) because :
	A bibentry of bibtypeArticlehas to specify the field: journal

However, it had journaltitle, which is virtually the same! Here is how we reproduce the error:

j <- c(author =  "Dagsvik, John K. and Hægeland, Torbjørn and Raknerud, Arvid",
  year = "2011", journaltitle = "Journal of Applied Econometrics",
  title = "Estimating the returns to schooling: a likelihood approach based on normal mixtures",
  doi = "10.1002/jae.1172", issn = "1099-1255", number = "4", pages = "613--640",
  volume = "26", publisher = "Wiley", language = "english", langid = "english")
attributes(j) <- c(attributes(j), list(entry = "Article", key = "dagsvik2011estimating"))

> bibtex:::make.bib.entry(j)

NULL

An easy fix is possible: read journalname as journal:

> names(j) <- gsub("^journaltitle$", "journal", names(j))
> bibtex:::make.bib.entry(j)
Dagsvik JK, Hægeland T, Raknerud A (2011).Estimating the returns to schooling: a
likelihood approach based on normal mixtures._Journal of Applied Econometrics_,
*26*(4), 613-640. ISSN 1099-1255, doi:10.1002/jae.1172
<https://doi.org/10.1002/jae.1172>.

A modification of the function to reflect this change would be as follows – combining it with my previous fix for handling dates:

make.bib.entry <- function (x) {
	type <- attr(x, "entry")
	key <- attr(x, "key")
	y <- as.list(x)
	names(y) <- tolower(names(y))
	err.fun <- function(e) {
		message(sprintf("ignoring entry '%s' (line %d) because :\n\t%s\n", key, attr(x, "srcref")[1], conditionMessage(e)))
		NULL
	}
	if ("author" %in% names(y)) {
		y[["author"]] <- tryCatch(ArrangeAuthors(y[["author"]]), error = err.fun)
		if (is.null(y[["author"]])) return()
	}
	if ("editor" %in% names(y)) {
		y[["editor"]] <- tryCatch(ArrangeAuthors(y[["editor"]]), error = err.fun)
		if (is.null(y[["editor"]])) return()
	}
	fields <- names(y)
	if ("date" %in% fields && !"year" %in% fields) {
		if (grepl("^\\d{4}-\\d{2}$", y$date)) y$date <- paste0(y$date, "-01")  # <-- HERE
		y$year <- tryCatch(format(as.Date(y$date), "%Y"), error = err.fun)  # <-- Also, why not tryCatch?
		if (is.null(y[["year"]])) return()
	}
	if (!is.null(y[["journaltitle"]]) & is.null(y[["journal"]]))  # <-- Here is the fix for the modern day and age
		y[["journal"]] <- y[["journaltitle"]]
	tryCatch(bibentry(bibtype = type, key = key, other = y), 
			 error = err.fun)
}

I hope that it helps withe the essential elements generated in modern bibliography management tools...

Yours sincerely,
Andreï V. Kostyrka

@coatless
Copy link
Collaborator

Hi @Fifis thanks for the report and suggested patch. Do you want to submit a PR or would you like me to?

Regarding the journaltitle vs. journal, we recently moved the backend from C to being parsed completely in R. This may not have been covered by our original unit tests and, thus, why it might see odd for this to be MIA.

@Fifis
Copy link
Author

Fifis commented Dec 29, 2024

I tried adding a PR with unit tests, but it failed due to other errors (some instances of wrong bibliography in other test cases?..). Please help me fix this... this is my first ever PR, and I am willing to learn how not to break things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants