0

I have a dataframe in R, one column of which contains bibtex citation commands, such as the following

\\cite[265]{delbarrio2014a}, \\cite[423--429]{nieto2009}, \\cite[188, 191, 196--197]{bile1988a}, \\cite[188--189]{bartonek2003}, \\cite[193]{thompson2010}, \\cite{skelton2015}

I want to make an HTML document with these citations in R Markdown. To do that, they need to be in the format @author, page number (if there is a page number). How do I convert the strings above into this format? For instance, \\cite[265]{delbarrio2014a} should become @delbarrio2014a, 265.

Namenlos
  • 475
  • 5
  • 17

1 Answers1

0

Does this help?

data.frame(string = c(
  "\\cite[265]{delbarrio2014a}",
  "\\cite[423--429]{nieto2009}",
  "\\cite[188, 191, 196--197]{bile1988a}"
)) |>
  dplyr::mutate(citation = paste0(
    "@", stringr::str_extract(string, "(?<=\\{).+?(?=\\})"), ", ",
    stringr::str_extract(string, "(?<=\\[).+?(?=\\])")
  ))

Result:

                                 string                       citation
1           \\cite[265]{delbarrio2014a}           @delbarrio2014a, 265
2           \\cite[423--429]{nieto2009}           @nieto2009, 423--429
3 \\cite[188, 191, 196--197]{bile1988a} @bile1988a, 188, 191, 196--197
Julian
  • 6,586
  • 2
  • 9
  • 33