我经常在Lyx中使用knitr来编写WYSIWYG LATEX方程。完成之后,将整个内容转换为R Markdown会很好。是否有捷径可寻?我尝试了此操作:https://duncanjg.wordpress.com/2012/09/25/sweave-to-markdown/,但是失败了。我的外部块名称消失了,标题结尾处有一个悬空的}。我也尝试了这个http://www.lemmster.de/lyx-export-to-markdown.html,效果甚至更糟!几乎一切都消失了。

最佳答案

我写了一个简短的R程序来做到这一点。第一步是使用Lyx将文件导出为.Rnw(knitr)文件。然后,该文件由下面的程序处理。这真的很粗糙,因为它只处理LaTEX功能的一小部分,而且处理方式确实不完整。

setwd("/home/haldane/Dropbox/PhD")

library(magrittr)
library(dplyr)
library(tidyr)
library(stringi)
library(rex)

last_carried_forward =
  . %>%
  zoo::na.locf(na.rm = FALSE)

chunk_frame = function(line_frame, start_string, end_string)
  line_frame %>%
  mutate(chunk_ID.raw =
           text %in%
           c(start_string, end_string) %>%
           cumsum %>%
           last_carried_forward,
         chunk_ID =
           chunk_ID.raw %>%
           mod(2) %>%
           `==`(1) %>%
           ifelse(chunk_ID.raw, NA) %>%
           `/`(2) %>%
           `+`(0.5) ) %>%
  select(-chunk_ID.raw) %>%
  filter(text %in% c(start_string, end_string) %>% `!`)

section_regex =
  rex("\\",
      capture(or("section",
                 "subsection",
                 "subsubsection",
                 "subsubsection",
                 "paragraph",
                 "subparagraph") ),
      "{",
      capture(anything),
      "}")

citation_regex =
  rex("\\citep{",
      capture(anything),
      "}")

fix_citation_regex = rex("[@",
                         capture_group(1),
                         "]")

backslash_regex = rex(start, "\\")

heading =
  data_frame(heading = c("section",
                         "subsection",
                         "subsubsection",
                         "paragraph",
                         "subparagraph"),
             heading_code = c("#",
                              "##",
                              "###",
                              "####",
                              "#####"))

line.pre_definition =
  "input.Rnw" %>%
  readLines %>%
  data_frame(text = .) %>%
  chunk_frame("\\begin{document}", "\\end{document}") %>%
  filter(chunk_ID == 1) %>%
  filter(text %in%
           c("\\end{itemize}",
             "\\printbibliography",
             "\\SweaveOpts{concordance=TRUE}",
             "\\maketitle",
             "\\tableofcontents{}",
             "\\appendix") %>%
           `!` ) %>%
  mutate(line__ID = 1:n()) %>%
  extract(text,
          c("heading",
            "heading_content"),
          section_regex,
          remove = FALSE) %>%
  left_join(heading) %>%
  mutate(fixed_line =
           heading %>%
           is.na %>%
           ifelse(text,
                  paste(heading_code,
                        heading_content) ) %>%
           stri_replace_all_fixed("\\item ",
                                  "- ") %>%
           stri_replace_all_regex(citation_regex,
                                  "\\[\\@$1\\]") ) %>%
  chunk_frame("\\begin{defn}", "\\end{defn}")

line.definition =
  line.pre_definition %>%
  filter(chunk_ID %>% is.na %>% `!`) %>%
  group_by(chunk_ID) %>%
  summarize(line__ID = first(line__ID),
            fixed_line = paste0(
              "\n**",
              first(fixed_line),
              "**: ",
              fixed_line[-(1:2)] %>% paste(collapse = "\n"),
              "\n"))

line.pre_table =
  line.pre_definition %>%
  filter(chunk_ID %>% is.na) %>%
  bind_rows(line.definition ) %>%
  arrange(line__ID) %>%
  chunk_frame("\\[", "\\]") %>%
  mutate(fixed_line =
           chunk_ID %>%
           is.na %>%
           ifelse(fixed_line,
                  paste0("$$",
                         fixed_line,
                         "$$") ) ) %>%
  chunk_frame("\\begin{table}[H]", "\\end{table}")

line.final =
  line.pre_table %>%
  filter(chunk_ID %>% is.na %>% `!`) %>%
  filter(text %>%
           stri_detect_regex("^\\\\") %>%
           not) %>%
  mutate(new_text =
           text %>%
           stri_replace_all_regex("\\\\tabularnewline",
                                  "")) %>%
  group_by(chunk_ID) %>%
  summarize(line__ID =
              first(line__ID),
            fixed_line =
              new_text %>%
              paste(collapse = "\n") %>%
              read.csv(text = ., sep = "&", check.names = FALSE ) %>%
              knitr::kable() %>%
              paste(collapse = "\n") %>%
              paste("\n", .) ) %>%
  bind_rows(line.pre_table %>% filter(chunk_ID %>% is.na)) %>%
  arrange(line__ID) %>%
  mutate(final_line =
           fixed_line %in%
           c("<<message = FALSE>>=", "@") %>%
           ifelse("```", fixed_line) %>%
           plyr::mapvalues("\\begin{itemize}", "") )

line.final %>%
  use_series(final_line) %>%
  writeLines("output.Rmd")

07-24 09:51