R/cites_extract.R

Defines functions cites_extract single_cites_extract

Documented in cites_extract single_cites_extract

#' Extract Single Citation Keys from LaTeX Text
#'
#' \code{single_cites_extract} function extracts all citation keys from a LaTeX string based on specific citation commands.
#'
#' @param text A character vector containing LaTeX content.
#' @return A character vector of citation keys, or `NULL` if no citations are found.
#' @details
#' The function identifies citation commands such as \code{cite}, \code{upcite}, \code{citep}, and \code{citet}.
#' It extracts the content within braces following these commands, and splits multiple keys separated by commas.
#' @importFrom stringr str_extract_all str_trim
#' @export
#' @rdname cites_extract
#'

single_cites_extract = function(text){
  latex_prefix=c("cite", "upcite", "citep", "citet")
  pattern <- sprintf("(?<=\\\\(%s)\\{)([^}]+)(?=\\})", paste(latex_prefix, collapse = "|"))

  cites = stringr::str_trim(unlist(str_extract_all(text, pattern)))
  if(length(cites) == 0){
    return(NULL)
  }
  cites = stringr::str_trim(unlist(strsplit(cites, ",")))
  return(cites)
}



#' Extract Unique Citation Keys from LaTeX Document
#'
#' \code{cites_extract} function extracts all unique citation keys from a LaTeX document.
#'
#' @param tex A character vector where each element represents a line or section of LaTeX content.
#' @return A unique character vector of citation keys.
#' @details
#' This function uses \code{single_cites_extract} to extract citation keys from each line
#' or section of the LaTeX document. It removes duplicates and trims unnecessary whitespace.
#' @example inst/example/cites_extract_example.R
#' @importFrom stringr str_trim
#' @export
#' @rdname cites_extract
#'
cites_extract = function(tex){
  cites =stringr::str_trim(unlist(lapply(tex, single_cites_extract)))
  cites = unique(cites)
  return(cites)
}
zoushucai/journalabbr documentation built on Dec. 6, 2024, 4:41 p.m.