2017-09-20 1 views
1

私はRmarkdown(LaTeX経由でPDFにエクスポート)で論文を執筆しており、本文の単語数を数えなければなりません。Rマークダウンの単語数からセクションを除外する

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

どのように私は私のRMDファイルにLaTeXのコメントを含めることができます:LaTeX文書では、私は私が私のTEX文書に次のタグを使用してワードカウントから除外したい部分を指定して、コマンドラインからtexcountを使用します再作成するたびに、手動で%TC行をtexファイルに追加しないでください。

texcount MWE.tex 

をし、答えは次のようになります。私は.Rmdファイルを編成し、出力.texファイルを持っていたら

--- 
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 

は、私が入力したい:ここ

は私のMWE .Rmdです6ワード ありがとう!

UPDATE 1:Twitterの上で

、@amesoudiはRStudioアドイン(WordCountAddIn)私のRMDの文書内の単語をカウントするために使用して提案しました。アドインはそこにありますhttps://github.com/benmarwick/wordcountaddin。しかし、これは自動化されておらず、指差しとクリックがまだまだあります。

UPDATE 2:
別の解決策は、

  • 例えば、コメントをラテックスすべきかを識別するための特定の表現を使用することであろう.RmdファイルでLATEXCOMMENT%TC:ignore

  • は、我々はできる、自動的に生成さ.tex文書(例えばsed

答えて

0

むしろより出力されたLaTeXファイルにワードカウントを行う際にLATEXCOMMENT表現を削除するスクリプトを持っています実際には.Rmdファイルを直接使用してください。

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)) 
} 

機能がリストに三つの項目を返します:

このコードは、それが似ているが、あなたが言及したwordcountaddinへのアプローチが、タグ<!---TC:ignore---><!---TC:endignore--->のカウントに含まれませんとの間に任意のテキストです

  • 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 --> 

enter image description here

注:起源のいくつかは、アルスクリプトは、http://www.questionflow.org/2017/10/13/how-to-scrape-pdf-and-rmd-to-get-inspiration/

関連する問題