Skip to content

Instantly share code, notes, and snippets.

@moritzpschwarz
Last active January 22, 2023 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moritzpschwarz/1b42d687662ab63dd1ce206c35c8f038 to your computer and use it in GitHub Desktop.
Save moritzpschwarz/1b42d687662ab63dd1ce206c35c8f038 to your computer and use it in GitHub Desktop.
When using biblatex, most citation styles do not retain capitalization of titles. This little R script reads in an existing bib file, changes the titles and then saves another bib file.
# read in the initial bib file
tex <- readLines("test.bib")
# get the location of the titles (NOTE: also will include fields of the nature 'booktitle')
title_loc <- grepl("title", tex, fixed = TRUE)
# Change the opening bracket from { to {{
# the full regex looks for strings containing 'title = {,' or 'title={' and replaces it with 'title = {'
tex[title_loc] <- gsub("title = \\{|title=\\{","title = {{",tex[title_loc])
# Change the closing bracket from } to }}
# the full regex looks for strings ending '},' or '} ,' and replaces it with '}},'
tex[title_loc] <- gsub("\\},$|\\} ,$","}},",tex[title_loc])
# show result
tex
# write out the final adjusted file
writeLines(tex, con = "test1.bib")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment