Latex 从R标记中的字数中排除节
我正在用Rmarkdown(通过LaTeX导出为PDF)写一篇论文,我需要计算正文中的字数。对于LaTeX文档,我使用命令行中的Latex 从R标记中的字数中排除节,latex,knitr,r-markdown,Latex,Knitr,R Markdown,我正在用Rmarkdown(通过LaTeX导出为PDF)写一篇论文,我需要计算正文中的字数。对于LaTeX文档,我使用命令行中的texcount,使用我的tex文档中的以下标记指定要从单词计数中排除的部分: %TC:ignore The part that is to be ignored (e.g., Appendix) %TC:endignore 如何在Rmd文件中包含LaTeX注释,以避免每次重新生成tex文件时手动在tex文件中添加%TC行 这是我的MWE.Rmd: --- out
texcount
,使用我的tex文档中的以下标记指定要从单词计数中排除的部分:
%TC:ignore
The part that is to be ignored (e.g., Appendix)
%TC:endignore
如何在Rmd文件中包含LaTeX注释,以避免每次重新生成tex文件时手动在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
答案应该是6个单词。
谢谢
更新1:
在Twitter上,@amesoudi建议使用RStudio加载项(WordCountAddIn)来计算我的Rmd文档中的单词数。加载项在那里可用。然而,这并不是自动的,仍然需要一些指向和点击
更新2:另一个解决办法是
- 使用特定的表达式来标识应该添加哪些注释,例如
文件中的.Rmd
LATEXCOMMENT%TC:ignore
- 在生成的
文档(例如.tex
)中,具有自动删除sed
表达式的脚本LATEXCOMMENT
和
之间的任何文本将不包括在计数中:
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))
}
库(stringr)
图书馆(tidyverse)
RmdWords%
粘贴0(折叠=”)%>%
#拆下YAML收割台
str_替换_all(“^”和“”)
#保存几个不同的结果
字数
字数:`r words$num\u words`\newline
字符计数:`r words$num\u char`
注:部分原始脚本改编自
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))
}
```{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 -->