I would like to count the "a" word in a text and put output to the PDF file, eg.

This is a document about processing text in TeX.
The document has a new macro but $a = 10$ is not counted,
nor \lstinline{a = 2}
nor \begin{lstlisting}a = 1;\end{lstlisting}

Code should output 2, as "a" is only two times mentioned. I do not want to process math, verbatim or lstlisting.

I have seen the answer for a question Environment that counts words inside and I can count only 'b' letters (not words)

\def\endassignment{\marginpar{\arabic{word} words}}

Here are some bad words.


A problem here is that, when I would like to write word bad in bold using \textbf{bad}, the source code is not compiling.

This question is only half-way. I would like to automatically modify every "a word" to "a~word".

  • %s/\<\(\w\) \(\w\{3,}\)/\1\~\2/g?
    – morbusg
    Commented Feb 17, 2014 at 22:39
  • Yes @morbusg , but that will also change single "a" in listings, eg. printf("Hello, a very nice example"); should stay untouched.
    – faramir
    Commented Feb 18, 2014 at 1:20

1 Answer 1


One solution is to use TeXcount which is made for counting the words in TeX/LaTeX documents. This is a Perl script that you can download and run, or you could use the online web service.

If you run this with the option -freq it will not only give a summary of the total word count, as is the main purpose, but a count of all the different words: a slight overkill since you're only interested in the particular word "a", but should do the job.

If you run this on the web service, you'll have to choose all as the Word freq option.

TeXcount will try to determine which words are part of the text, and which are not: e.g. maths and verbatim should not be counted as text. For this, it uses macro and environment parsing rules which tells it how to interpret different macros and environments. You may have to add rules if TeXcount lacks these. E.g. to add rules for \lstinline and lstlisting environment, you'll have to add these:

%TC:macro \lstinline [xx]
%TC:envir lstlisting [] xall
This is a document about processing text in TeX.
The document has a new macro but $a = 10$ is not counted,
nor \lstinline{a = 2}
nor \begin{lstlisting}a = 1;\end{lstlisting}

Lines beginning with %TC: are TeX comments used to give instructions to TeXcount, e.g. for adding macro handling rules as here. The [xx] and [] indicate how to process the arguments, where xx indicates a strong form of ignore. The xall indicates that the within the environment, everything should be ignored, even macros and special characters.

  • What about new listing made of lstlisting by using \lstnewenvironment command? I found that I need to specify every new lst environment.
    – faramir
    Commented Feb 18, 2014 at 18:13
  • @faramir: That's right. TeXcount cannot generate macro/environment handling rules defined by commands like \lstnewenvironment, same as with \newcommand, etc. So you'll have to specify the rule using %TC: instructions like the %TC:envir in the example. Commented Feb 18, 2014 at 18:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .