I am struggling with adding parsing rules to TexCount in an external options file.

I am running TexCount as follows:

texcount -sub -html -stat -inc -v3 -opt=tcrules master.tex > count.html

The file tcrules is as follows:


# None for now


%macro \url [ignore]

%macro \Cref [ignore]
%macro \cref [ignore]

%macro \DTLloaddb [ignore,ignore]

%envir longtable [ignore] ignore

%macro \cite [ignore]
%macro \Cite [ignore]
%macro \parencite [ignore]
%macro \Parencite [ignore]

All of the macro rules work fine, however the %envir longtable [ignore] ignore rule is causing an issue:

$ texcount -sub -html -stat -inc -v3 -opt=tcrules master.tex > count.html
Invalid TC option: envir longtable [ignore] ignore

However, if I put the same rule into the LaTeX document as:

%TC:envir longtable [ignore] ignore

Then TeXCount works and produces the desired result.

Any help with why the envir is not working in the external options file would be much appreciated.

I am using TexCount version 3.0, 2013 Jul 29.


This is a bug!

The problem seems to be that when TeXcount options are provided in an options file, the third argument, e.g. the final ignore option in

%envir longtable [ignore] ignore

is ignored. The reason is a RegEx in TeXcount which requires the third argument to be numeric (as in older versions of TeXcount).

Fortunately, there's a quick fix (which I'll ensure gets into the next update of TeXcount). Just find the subroutine definition indicated by sub __optionfile_tc and change the line

} elsif ($arg=~/^(\w+)\s+([\]*\w+)\s+([^\s\n]+)(\s+([0-9]+))?/i) {


} elsif ($arg=~/^(\w+)\s+([\]*\w+)\s+([^\s\n]+)(\s+([0-9\w]+))?/i) {

by adding \w to allow words as the third argument.

As Harry has already pointed out, one may use the integer codes instead of names, although the names are more intuitive. However, the negative integer codes also don't work, which may be fixed by changing the line to

} elsif ($arg=~/^(\w+)\s+([\]*\w+)\s+([^\s\n]+)(\s+(-?[0-9]+|\w+))?/i) {

and then hope for the best.

I somehow suspect that a million monkeys are more likely to come up with a really neat Perl script than with Shakespeare. At least that's what I feel like when I'm working with Perl.

  • Thank you for your response! I had been looking through the code but didn't at all notice that it was a regex problem (Perl is all a bit much for me :P). I literally just now posted an answer that works, I guess because the regex doesn't mind the integer codes. It's great to hear that it'll be fixed in the next update. Thank you for your software, it's incredibly useful for my thesis! When I have 15 rep I will vote this up :)!
    – Harry
    Jun 23 '15 at 12:59
  • @Harry: Glad to hear! And it's nice to see that the more advanced features are being used. Thanks for detecting the bug! Jun 23 '15 at 13:09

I found a fix, though I am not familiar enough with the TeXCount Perl code to explain why the following is the case.

What seems to be happening is that for envir TeXCount commands in an external options file (but not inside LaTeX files as %TC:envir commands), the (what seems to be relatively new) keyword options for how to treat the main text in the environment are not dealt with correctly.

However, the integer indexes for each keyword work, e.g.:

%envir longtable [ignore] 0


%envir longtable [0] 0

work, whereas:

%envir longtable [ignore] ignore


The codes from the documentation are as follows (in bold):

5.1 Parameter and content handling rules

There are a set of alternative rules that may be used for parsing macro parameters and environment contents.

These rules, or parser states, are identified by keywords:

Text: (key: text, word, wd, w formerly 1) Count as text (i.e. count words).

Header text: (key: headertext, headerword, hword, hwd, hw formerly 2) Count as header text.

Other text: (key: otherword, other, oword, owd, ow formerly 3) Count as float/caption text.

Displaymath: (key: displaymath, dsmath, dmath, ds formerly 7) Count as displayed math formulae.

Inline math: (key: inlinemath, inline, imath, eq formerly 6) Count as inlined math formulae.

To header: (key: header, heading, head formerly 4) Count header, then count text as headertext (transition state).

To float: (key: float, table, figure formerly 5) Count float, then parse contents as isfloat (transition state).

Preamble: (key: formerly -9) Parse as preamble, i.e. ignore text but look for preambleinclude macros.

Ignore: (key: ignore formerly 0) Ignore text, i.e. do not count, but will still parse the code.

Float: (key: isfloat formerly -1) Float contents, ignore text but look for floatinclude macros.

Strong exclude: (key: xx formerly -2) Strong ignore which ignore environments, e.g. to use in macro definitions where \begin–\end need not be balanced.

Stronger exclude: (key: xxx formerly -3) Stronger ignore, handles macros as isolated tokens without handling their parameters, to use with macro definitions like \newcommand and \def.

Exclude all: (key: xall formerly -4) Ignore all, including unbalanced braces (e.g. used by %TC:ignore and the verbatim environment). This rule may be used for environment contents, but not for macro or environment parameters or options since the exclusion causes { and [ to be ignored.

So using the integer codes worked even though the keyword versions inexplicably didn't. I couldn't get some of the negative integer codes to work, but for the purposes of what I am doing I didn't need them; therefore, this is a solution that works, even if it was not obvious to arrive at!

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.