knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(purrr)
library(xtable)

Latex macros like \frac{#1}{#2} with arguments are displayed as $\frac{#1}{#2}$.

# escape the "#"-sign for latex macros with arguments
sanitize_command <- function(command) {
  paste0("$", 
         stringr::str_replace_all(
           command, 
           pattern = "#([1-9])",
           replacement = "{\\\\#\\1}"), 
         "$")
}
get_command <- function(line) {
  #see https://www.regular-expressions.info/recurse.html#balanced
  command <- regmatches(line, 
                        gregexpr("\\{(?>[^{}]|(?R))*\\}", 
                                 line, perl = TRUE)
                        )[[1]]
  if (length(command) != 2) return(NULL)
  command[3] <- stringr::str_match(line, 
                          pattern = "\\}\\s*%\\s*(.*)$")[-1]
  names(command) <- c("Macro", "Notation", "Comment")
  command[1] <- substr(command[1], 2, nchar(command[1])-1)
  if (grepl("IGNORE_NOTATION", command[3])) {
    command[2] <- ""
  }
  command[1] <- paste0("\\verb!", command[1], "!")
  command[2] <- sanitize_command(command[2])
  command[3] <- xtable::sanitize(command[3])
  command
}

texfiles <- list.files(pattern = "\\.tex$")
texfiles <- setdiff(texfiles, "latex-math.tex") # avoid itself
macros <- texfiles %>% 
  map(~ readLines(.)) %>% 
  map_depth(.depth = 2, ~ get_command(.)) %>% 
  map(~ discard(., .p = is.null) %>% 
        do.call(rbind, .)) 
names(macros) <- texfiles
for (i in seq_along(macros)) {
  cat("\\section{",  names(macros)[i], "} ")
  null <- print(xtable(macros[[i]]), 
                sanitize.text.function = I, 
                include.rownames = FALSE, 
                floating = FALSE,
                tabular.environment = "longtable", 
                comment = FALSE)
  cat("\\newpage")
}


HerrMo/geo-outlier-framework documentation built on May 5, 2022, 12:30 a.m.