{"word_count": 831, "character_count": 6946}
Last week, with @3wen, we were working a the revised version of our work on smoothing densities of spatial processes (with edge correction). Usually, once you have revised the paper, some references were added, others were droped. But you need to spend some time, to check that all references are actually mentioned in the paper. For instance, consider the following compiled tex file :Only three references are actually mentioned in the document, so we need to update the reference list (by removing the first three). If you use a bib file, it is very simple, and only cited references will appear in the list. The problem here is that we used bibitems,I wanted to work on that manually this week-end, but @3wen suggested to write a simple R function to scan the tex f file (as well as the aux file actually) to remove uncited references. The idea is the following. First, let us scan the two files&#62; library(stringr)&#62; setwd("/home/tex/")&#62; file_tex &#60;- scan("file_test.tex", what = "character", sep = "\n")Read 15 items&#62; file_aux &#60;- scan("file_test.aux", what = "character", sep = "\n")Read 21 itemsThen, we extract only parts related to the bibliography,&#62; beg_file &#60;- which(str_detect(string = file_tex, pattern = "\\\\begin\\{thebibliography\\}"))&#62; end_file &#60;- which(str_detect(string = file_tex, pattern = "\\\\end\\{thebibliography\\}"))References here are the following lines&#62; biblio &#60;- file_tex[seq(beg_file+1, end_file-1)]&#62; biblio[1] "\\bibitem[Cressie(1991)]{Cressie} Cressie, N. (1991). Statistics for Spatial Data. New York: John Wiley \\&#38; Sons"                                  [2] "\\bibitem[Diggle (2002)]{Diggle} Diggle, P., Heagerty, P., Liang, K.Y. \\&#38; Zeger, S. 2002. Analysis of Longitudinal Data. Oxford University Press."[3] "\\bibitem[Ripley(1981)]{Ripley} Ripley, B. 1981. Spatial Statistics, Wiley, New York."                                             [4] "\\bibitem[Scott(1992)]{Scott} Scott, D W 1992 Multivariate Density Estimation: Theory, Practice, and Visualization. New York, John Wiley and Sons."[5] "\\bibitem[Silverman(2004)]{Silverman} Silverman B W 1986 Density Estimation for Statistics and Data Analysis."[6] "London, Chapman \\&#38; Hall."                                             [7] "\\bibitem[Wand \\&#38; Jones(1995)]{Wand} Wand, M.P; Jones, M.C. (1995). Kernel Smoothing. London: Chapman \\&#38; Hall/CRC. "If you look carefully at the output, you can observe that the fifth reference is on two lines. Which might happend frequently. So we need to check precisely when a reference starts, and when it ends.&#62; beg_bibitem &#60;- which(str_detect(string = biblio, pattern = "\\\\bibitem"))&#62; go_through &#60;- cbind(beg_bibitem, c(beg_bibitem[-1]-1,length(biblio)))&#62; go_through     beg_bibitem  [1,]           1 1[2,]           2 2[3,]           3 3[4,]           4 4[5,]           5 6[6,]           7 7Actually, we should also check if a reference is cited. Sometimes, there are references with a comment sign.&#62; go_through &#60;- data.frame(beg = beg_bibitem, end = rep(NA, length(beg_bibitem)))&#62; for(i in seq_len(length(beg_bibitem))-1){+   go_through[i,2] &#60;- beg_bibitem[i+1]-1+ }&#62; go_through[nrow(go_through), 2] &#60;- length(biblio)&#62; go_through$comment &#60;- str_detect(biblio[beg_bibitem], "^%")&#62; go_through  beg end comment1   1   1   FALSE2   2   2   FALSE3   3   3   FALSE4   4   4   FALSE5   5   6   FALSE6   7   7   FALSELet us now extract the labels of all the references (%).&#62; extract_ref_cite &#60;- function(bibitem, file){+   entree &#60;- file[bibitem]+   if(str_detect(entree, "bibitem\\[.*\\]\\{")){+     nom_citation &#60;- str_extract(entree, "]\\{(.*?)\\}")+   }else{+     nom_citation &#60;- str_extract(entree, "\\{(.*?)\\}")+   }+   str_replace_all(string = nom_citation, pattern = "\\{|\\}|]", replacement = "")+ }&#62; bibitems_ref &#60;- unlist(lapply(beg_bibitem, extract_ref_cite, biblio))&#62; bibitems_ref[1] "Cressie"   "Diggle"    "Ripley"    "Scott"     "Silverman" "Wand"We have six references, with those labels (as expected).Now, if we look at the aux file, to see which references are cited in the text,&#62; ind_cite &#60;- which(str_detect(string = file_aux, pattern = "\\\\citation"))&#62; bibitems_cite_names &#60;- unlist(lapply(ind_cite, extract_ref_cite, file_aux))&#62; bibitems_cite_names[1] "Scott"     "Scott"     "Silverman" "Silverman" "Wand"      "Wand"     [7] "Scott"     "Scott"Note that references are mentioned twice (at least): once for the author's name, once for the year of publication. Since we just need to see which one actually appears in the aux file, we can use&#62; bibitems_cite_names &#60;- unique(bibitems_cite_names)&#62; bibitems_cite_names[1] "Scott"     "Silverman" "Wand"Now, we can see which references are cited,&#62; go_through$keep &#60;- bibitems_ref %in% bibitems_cite_names&#62; go_through  beg end comment  keep1   1   1   FALSE FALSE2   2   2   FALSE FALSE3   3   3   FALSE FALSE4   4   4   FALSE  TRUE5   5   6   FALSE  TRUE6   7   7   FALSE  TRUEBased on that table, we can use a simple code: references that we do not need will be seen as comments, while those that are cited will appear in the reference list.&#62; return_cite &#60;- function(one_ligne){+   citation &#60;- str_c(biblio[one_ligne[1,"beg"]:one_ligne[1,"end"]], collapse = "\n")+   if(!one_ligne[1,"keep"] &#38; !str_detect(citation, "^%")){+     citation &#60;- str_replace_all(citation, pattern = "\n", replacement =  "\n%")+   }+   citation+ }For instance,&#62; return_cite(go_through[1,])[1] "%\\bibitem[Cressie(1991)]{Cressie} Cressie, N. (1991). Statistics for Spatial Data. New York: John Wiley \\&#38; Sons"since the first reference does not appear in the text, while&#62; return_cite(go_through[4,])[1] "\\bibitem[Scott(1992)]{Scott} Scott, D W 1992 Multivariate Density Estimation: Theory, Practice, and Visualization. New York, John Wiley and Sons."Now, we can easily generate our bibliography, in LaTeX&#62; cat(unlist(lapply(1:nrow(go_through), function(x) return_cite(go_through[x,]))), sep = "\n\n")%\bibitem[Cressie(1991)]{Cressie} Cressie, N. (1991). Statistics for Spatial Data. New York: John Wiley \&#38; Sons%\bibitem[Diggle (2002)]{Diggle} Diggle, P., Heagerty, P., Liang, K.Y. \&#38; Zeger, S. 2002. Analysis of Longitudinal Data. Oxford University Press.%\bibitem[Ripley(1981)]{Ripley} Ripley, B. 1981. Spatial Statistics, Wiley, New York.\bibitem[Scott(1992)]{Scott} Scott, D W 1992 Multivariate Density Estimation: Theory, Practice, and Visualization. New York, John Wiley and Sons.\bibitem[Silverman(2004)]{Silverman} Silverman B W 1986 Density Estimation for Statistics and Data Analysis. London, Chapman \&#38; Hall.\bibitem[Wand \&#38; Jones(1995)]{Wand} Wand, M.P; Jones, M.C. (1995). Kernel Smoothing. London: Chapman \&#38; Hall/CRC.We simply need to copy that list and paste it in our LaTeX file. Nice, isn't it?