Skip to content

Instantly share code, notes, and snippets.

@HughParsonage
Created September 26, 2017 15:52
Show Gist options
  • Save HughParsonage/37dd61adf04be0772ba6db5d37216dcf to your computer and use it in GitHub Desktop.
Save HughParsonage/37dd61adf04be0772ba6db5d37216dcf to your computer and use it in GitHub Desktop.
tex-parser
parse2 <- function(x) {
# x <- "ab{c{f}}d{e}f{g}h{ij{{k}}}lm"
x_split <- unlist(strsplit(x, "", fixed = TRUE))
tex_group <- cumsum(x_split == "{") - cumsum(x_split == "}")
max_tex_group <- max(tex_group)
out <- setDT(list(char = x_split, tex_group = tex_group))
out[, I := .I]
for (j in seq_len(max_tex_group)) {
tgj <- paste0("tg", j)
out[tex_group >= j, (tgj) := cumsum(char == "{" & tex_group == j)]
}
out
}
footnote_opens <- function(DT) {
c("\\", "f", "o", "o", "t", "n", "o", "t", "e", "{")
for (k in 0:9) {
DT[, (paste0("v", k)) := shift(char, k + 1)]
}
DT %>%
.[char == "{"] %>%
.[v0 == "e"] %>%
.[v1 == "t"] %>%
.[v2 == "o"] %>%
.[v3 == "n"] %>%
.[v4 == "t"] %>%
.[v5 == "o"] %>%
.[v6 == "o"] %>%
.[v7 == "f"] %>%
.[v8 == "\\"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment