4

Problem

I want to include a word count in my document, using texcount. But I also want to include a comma as a thousand separator. This is the code I came up with:

\makeatletter\@@input|"echo `texcount -1 -sum=1,1,1,1,1,1,1 -merge -incbib -utf8 \jobname.tex`| sed `s/^\(.\{1\}\)/\1,/`| cut -c1-5"\makeatother

However, there are special characters in the sed command: backslash and braces. LaTeX is thus unable to run this line of code.

Attempted Solutions

I tried escaping the backslashes and braces with \textbackslash, \{, and \}, but it doesn't work. I also tried substituting \makeatletter\@@input by \immediate\write18, but it doesn't work either.

cgnieder
  • 66,645

2 Answers2

5

You can use \@backslashchar to access \ as a normal punctuation character.

David Carlisle
  • 757,742
  • Do you know why I need to escape the backslashes but not the braces? Just curious. – Lucas De Abreu Maia Jan 24 '20 at 16:49
  • 1
    @LucasDeAbreuMaia as long as they are balanced braces are not a problem, same as in \typeout{aa {bbb} ccc} which will show aaa {bbb} ccc you only need quote { if you need an unmatched one. – David Carlisle Jan 24 '20 at 16:55
2

You could use a more advanced method:

\documentclass{article}
\usepackage{xparse,siunitx}

\ExplSyntaxOn
\NewDocumentCommand{\countwords}{o}
 {
  \IfNoValueTF{#1}
   {\abreu_countwords:n {\c_sys_jobname_str.tex}}
   {\abreu_countwords:n {#1}}
 }

\cs_new_protected:Nn \abreu_countwords:n
 {
  \sys_get_shell:xnN
   {
    texcount ~ -q ~ -1 ~ -sum=1,1,1,1,1,1,1 ~ -merge ~ -incbib ~ -utf8 ~ #1 ~ 2>/dev/null
   }{}
   \l_tmpa_tl
  \num[group-four-digits,group-separator={,}]{\tl_use:N \l_tmpa_tl}
 }
\cs_generate_variant:Nn \sys_get_shell:nnN { x }
\ExplSyntaxOff

\begin{document}

\countwords

abc def ghi klm nop abc def ghi klm nop [...long nonsense omitted...]

\end{document}

You can also do \countwords[somefile.tex] in order to get the count for another file.

enter image description here

egreg
  • 1,121,712