title: Exclude sections from word count in R Markdown
body: <p>I am writing a paper with Rmarkdown (exported to PDF via LaTeX) and I need to count the number of words in the main text. With LaTeX documents, I use <code>texcount</code> from the command line, specifying the section that I want to exclude from word count using the following tags in my tex document:</p>
  
  <pre><code>%TC:ignore 
  The part that is to be ignored (e.g., Appendix)
  %TC:endignore 
  </code></pre>
  
  <p>How can I include LaTeX comments in my Rmd file to avoid manually adding the <code>%TC</code> lines in my tex file each time that I regenerate it?  </p>
  
  <p>Here is my MWE <code>.Rmd</code>:</p>
  
  <pre><code>---
  output:
    pdf_document: 
      keep_tex: yes
  ---
  
  Words that I want to count. 
  
  &lt;!-- TC:ignore --&gt;
  Words that I want to exclude but cannot, 
  because the comments do not appear in the `.tex` file. 
  &lt;!-- TC:endignore --&gt; 
  
  %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
  </code></pre>
  
  <p>Once I have knitted the .Rmd file and have the output .tex file, I'd type:</p>
  
  <pre><code>texcount MWE.tex
  </code></pre>
  
  <p>and the answer should be 6 words. 
  Thanks! </p>
  
  <p><strong>UPDATE 1:</strong>  </p>
  
  <p>On Twitter, @amesoudi suggested using a RStudio add-in (WordCountAddIn) to count words in my Rmd document. The add-in is available there <a href="https://github.com/benmarwick/wordcountaddin" rel="nofollow noreferrer">https://github.com/benmarwick/wordcountaddin</a> . However, this is no automated and there is still some pointing and clicking involved. </p>
  
  <p><strong>UPDATE 2:</strong><br>
  Another solution would be to </p>
  
  <ul>
  <li><p>use a specific expression to identify what should be LaTeX comments, e.g. <code>LATEXCOMMENT%TC:ignore</code> in the <code>.Rmd</code> file,</p></li>
  <li><p>have a script that automatically removes the <code>LATEXCOMMENT</code> expressions in the generated <code>.tex</code> document (e.g. <code>sed</code>) </p></li>
  </ul>