Dynamic Word Count in .rnw Documents”

1 minute read

Published:

In this post, I explain two methods to do a word count in your .rnw document.

Perl, write18 and TexCount Approach

  • Install Perl: curl -L http://xrl.us/installperlosx | bash
  • Install texcount in TeX Live Utility.
  • Enable write18:
    • Find where texmf.cnf is. In terminal, kpsewhich texmf.cnf.
    • Then go to that directory. Finder > Go > Go to Folder.
    • Open the texmf.cnf file in any text editor.
    • Within texmf.cnf, write shell_escape = t
  • Add this to your preamble:
\newcommand\wordcount{
\immediate\write18{texcount -sub=section \jobname.tex | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'count.txt'}
(\input{count.txt}words)}
  • Insert the following into your .rnw file, changing myfile to the name of your .rnw file:
\makeatletter\@@input|"echo `texcount -1 myfile.tex`| cut -c1-4"\makeatother

Thanks to Lelkes for making these scripts available.

Self-contained Approach

Here is another one that does not need any extra things. Just copy/paste the code below at the end of your .rnw document.

\clearpage
<<wordcount, echo=FALSE, cache=FALSE>>=
library(knitr)

comma <- function (x, ...) {
  format(x, ..., big.mark = ",", scientific = FALSE, trim = TRUE)
}

# To dynamically extract name of the current file, use code below
nameoffile <- current_input() # get name of file
nof2 <-  strsplit(nameoffile,"\\.")[[1]][1] # extract name, drop extension
noftex <- paste(nof2, ".tex", sep="") # add .tex extension
systemcall <- paste("system('texcount -inc -incbib -total -sum ", noftex, "', intern=TRUE)", sep="") # paste together texcount system command
texcount.out <- eval(parse(text=systemcall)) # run texcount on current last compiled .tex file

sum.row <- grep("Sum count", texcount.out, value=TRUE) # extract row
pattern <- "(\\d)+" # regex pattern for digits

count <- regmatches(sum.row, regexpr(pattern, sum.row) )
# extract digits

count <- comma(as.numeric(count)) # add comma
@


\begin{center}
\vspace*{\stretch{1}}
\dotfill
\dotfill {\huge {\bf Word count}: \Sexpr{count}} \dotfill
\dotfill
\vspace*{\stretch{1}}
\end{center}

\clearpage

I borrowed this from Omar Wasow. Thanks!