【问题标题】:Exclude sections from word count in R Markdown从 R Markdown 中的字数中排除部分
【发布时间】:2017-09-20 09:13:59
【问题描述】:

我正在用 Rmarkdown 写一篇论文(通过 LaTeX 导出为 PDF),我需要计算正文中的单词数。对于 LaTeX 文档,我在命令行中使用 texcount,在我的 tex 文档中使用以下标签指定要从字数统计中排除的部分:

%TC:ignore 
The part that is to be ignored (e.g., Appendix)
%TC:endignore 

如何在我的 Rmd 文件中包含 LaTeX cmets 以避免每次重新生成它时在我的 tex 文件中手动添加 %TC 行?

这是我的 MWE .Rmd:

---
output:
  pdf_document: 
    keep_tex: yes
---

Words that I want to count. 

<!-- TC:ignore -->
Words that I want to exclude but cannot, 
because the comments do not appear in the `.tex` file. 
<!-- TC:endignore --> 

%TC:ignore 
Other words that I want to exclude but this does not work either 
because `%` is not interpreted as \LaTeX comment. 
%TC:endignore 

#%TC:ignore
Another attempt; it does not work because `#` is for sections, 
not comments. 
#%TC:endignore

一旦我编织了 .Rmd 文件并输出了 .tex 文件,我会输入:

texcount MWE.tex

答案应该是 6 个字。 谢谢!

更新 1:

在 Twitter 上,@amesoudi 建议使用 RStudio 插件 (WordCountAddIn) 来计算我的 Rmd 文档中的单词。该插件在https://github.com/benmarwick/wordcountaddin 处可用。但是,这不是自动化的,仍然涉及一些指向和单击。

更新 2:
另一种解决方案是

  • 使用特定的表达式来识别什么应该是 LaTeX cmets,例如LATEXCOMMENT%TC:ignore.Rmd 文件中,

  • 有一个脚本可以自动删除生成的.tex 文档中的LATEXCOMMENT 表达式(例如sed

【问题讨论】:

    标签: latex knitr r-markdown


    【解决方案1】:

    我们实际上可以直接使用 .Rmd 文件,而不是对输出的 LaTex 文件进行字数统计。

    此代码与您提到的wordcountaddin 的方法相似,但标签&lt;!---TC:ignore---&gt;&lt;!---TC:endignore---&gt; 之间的任何文本都不会包含在计数中:

    library(stringr)
    library(tidyverse)
    
    RmdWords <- function(file) {
    
      # Creates a string of text
      file_string <- file %>%
        readLines() %>%
        paste0(collapse = " ") %>%
        # Remove YAML header
        str_replace_all("^<--- .*?--- ", "") %>%    
        str_replace_all("^--- .*?--- ", "") %>%
        # Remove code
        str_replace_all("```.*?```", "") %>%
        str_replace_all("`.*?`", "") %>%
        # Remove LaTeX
        str_replace_all("[^\\\\]\\$\\$.*?[^\\\\]\\$\\$", "") %>%
        str_replace_all("[^\\\\]\\$.*?[^\\\\]\\$", "") %>%
        # Deletes text between tags
        str_replace_all("TC:ignore.*?TC:endignore", "") %>%
        str_replace_all("[[:punct:]]", " ") %>%
        str_replace_all("  ", "") %>%
        str_replace_all("<", "") %>%
        str_replace_all(">", "")
    
      # Save several different results
      word_count <- str_count(file_string, "\\S+")
      char_count <- str_replace_all(string = file_string, " ", "") %>% str_count()
    
       return(list(num_words = word_count, num_char = char_count, word_list = file_string))
    }
    

    函数返回列表中的三个项目:

    • num_words:文件中的字数
    • num_char:字符数
    • word_list:文档中所有单词的列表

    如果要在编译后的报告中显示结果,可以将r代码内联如下:

    ```{r}
    words <- RmdWords("MWE.Rmd")
    ```
     There are seven words with 34 characters.
    
    <!-- TC:ignore -->
    Words that I want to exclude but cannot, 
    because the comments do not appear in the `.tex` file. 
    <!-- TC:endignore --> 
    
    <!-- TC:ignore -->
    Word Count: `r words$num_words` \newline
    Character Count: `r words$num_char`
    <!-- TC:endignore --> 
    

    注:部分原脚本改编自http://www.questionflow.org/2017/10/13/how-to-scrape-pdf-and-rmd-to-get-inspiration/

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      相关资源
      最近更新 更多