ropensci / bibtex

bibtex parser for R
https://docs.ropensci.org/bibtex/
35 stars 12 forks source link

Commas added to references when using bibtex in rmarkdown #50

Closed michaelaklin closed 1 year ago

michaelaklin commented 1 year ago

Thanks for this terrific package!

I ran into something that seems like a bug, but I can't quite figure out where it comes from. I'm using bibtex (your package) to generate full citations in the body of the text in RMarkdown. However, when I generate the final document (a pdf, but the problem appears when I try other formats like html), some citations include commas that don't exist in the bib file.

Here's a simple example.

My rmd file (inspired by an earlier stack overflow suggestion here):

---
output: pdf_document
---

`r refs <- bibtex::read.bib("minimalbib.bib")`

`r capture.output(refs["BusemeyerTober2022"])`

minimalbib.bib contains the following:

@article{BusemeyerTober2022,
    author = {Marius R. Busemeyer and Tobias Tober},
    journal = {Comparative Political Studies},
    pages = {00104140221139381},
    title = {Dealing with Technological Change: Social Policy Preferences and Institutional Context},
    year = {2022}}

The pdf (and html) renders the following:

Busemeyer MR, Tober T (2022). "Dealing with Technological Change:, Social Policy Preferences and Institutional Context." Comparative, Political Studies, 00104140221139381.

Note two new commas: one after the colon ("Change:, Social") and one in the name of the journal ("_Comparative, Political").

Yet when I check the citation in the R console, I get the correct markdown code:

[1] "Busemeyer MR, Tober T (2022). “Dealing with Technological Change: Social" [2] "Policy Preferences and Institutional Context.” _Comparative Political"
[3] "Studies_, 00104140221139381."

I would add that it doesn't affect all references (eyeballing my file, I would say about 1 in 4). I don't know if I'm doing something wrong, if rmarkdown is causing the problem, or if there's a bug, but I was wondering if anyone had an idea of what's happening here. Thanks!

coatless commented 1 year ago

@michaelaklin thanks for the report.

I've tested it out and can confirm an issue with the StackOverflow suggestion. However, bibtex does not have a custom print style for citations. Instead, bibtex interprets the bibliography file and places it into the bibentry class, which is part of the utils package.

Thus, I think you can fix the issue by switching from:

`r capture.output(refs["BusemeyerTober2022"])`

to

`r format(refs["BusemeyerTober2022"], style = "text")`

Note, we're using the utils:::format.bibentry(entry, style = "text") to correctly set the output display.

For completeness, please feel free to see the reprex.

Debug RMD file ```` --- title: "Debug Commas" author: "JJB" output: html_document --- ```{r} library("bibtex") my_ref <- "@article{BusemeyerTober2022, author = {Marius R. Busemeyer and Tobias Tober}, journal = {Comparative Political Studies}, pages = {00104140221139381}, title = {Dealing with Technological Change: Social Policy Preferences and Institutional Context}, year = {2022}}" # Need to check by writing first tmp1 <- tempfile(fileext = ".bib") writeLines(my_ref, tmp1) # And reading from the temp file out <- read.bib(tmp1) ``` ```{r} out["BusemeyerTober2022"] ``` ## Capture Output `r capture.output(out["BusemeyerTober2022"])` ## Format Output `r format(out["BusemeyerTober2022"], style = "text")` ````

Please let me know if this works and I'll close out the ticket.

michaelaklin commented 1 year ago

Wonderful, this works like a charm. Thanks a lot!