5

I am trying to count words in a document that contains various C types such as \texttt{uintptr\_t}, \texttt{size\_t}, etc. However, when I use texcount it breaks at the underscore and these as two words.

Is there a way to tell texcount to treat words with underscores as being only one word?

If I run texcount on this file it reports 5 words:

\documentclass{article}
\begin{document}
Word with underscore: \texttt{uintptr\_t}.
\end{document}

How can I work around this and count uintptr\_t as one word?

edit: I've tried the following which mostly works:

\documentclass{article}
%TC:newcounter icode Number of words in code
%TC:newcounter icodeuses Number of code uses
%TC:macrocount \code [icodeuses]
%TC:macro \code [icode]
\newcommand*{\code}[1]{\texttt{#1}}
\begin{document}
Word with underscore: \code{uintptr\_t}.
\end{document}

This now reports the following for texcount -sum:

Sum count: 6
Words in text: 3
Words in headers: 0
Words outside text (captions, etc.): 0
Number of headers: 0
Number of floats/tables/figures: 0
Number of math inlines: 0
Number of math displayed: 0
Number of words in code: 2
Number of code uses: 1

Can I exclude certain counters from the total sum?

3
  • Welcome to TeX.se, and thanks for providing a minimal example. I don't think there's a way to do this with a command line option passed to texcount but you could modify the script itself to do this. – Alan Munn Sep 22 '19 at 15:00
  • I had a look at the source but unfortunately I don't speak enough perl to fix this. However, I think I found a reasonable workaround using the -template= flag and bash: echo "Total words: $(($(texcount '-template={w} + {icodeuses}' foo.tex)))" prints Total words: 4 – ar31 Sep 22 '19 at 18:14
  • In Linux you can also first remove the \_ from your file with sed and then count: sed "s/\\\_//g" yourfile.tex|texcount -. – Marijn Sep 22 '19 at 18:39
5

There's no simple way to do this with a command line option, but it's easy to add \_ to the list of character modifiers.

Make a copy of texcount.pl and place it somewhere where it will be found (your local ~/bin folder is an obvious choice). It's probably best to give it a different name so as not to override the standard one. (This assumes Mac/Linux; I don't have any idea about how to do this in Windows.)

On line 494 you should find the following line:

my $modifiedchars='\\\\[\'\"\`\~\^\=](@|\{@\})';

Change this to:

my $modifiedchars='\\\\[\'\"\`\~\^\=\_](@|\{@\})';

This will now include \_ as character modifier.

Now running this modified file with your example yields:

$ ./texcount-new.pl  -sum word-count-underscore.tex
File: word-count-underscore.tex
Encoding: ascii
Sum count: 5
Words in text: 3
Words in headers: 0
Words outside text (captions, etc.): 0
Number of headers: 0
Number of floats/tables/figures: 0
Number of math inlines: 0
Number of math displayed: 0
Number of words in code: 1
Number of code uses: 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.