This can be done using TeXcount, which is a Perl script. You probably have this available as part of the TeX installation, or it can be found on the web size together with a online web application you can try out.
TeXcount sets breakpoints for which subcounts are generated.
Breakpoints are by default set at all chapter/section headers, which you would have to turn off: use command line option -sub=none
for the script or choose Subcounts: None in the web interface.
Breakpoints can be inserted in the document using TeX comment lines on the form
%TC:break breakpoint-title
where you enter you own breakpoint title (spaces allowed), which will then initiate a new subcount.
An alternative is to define a macro, eg \CountSection
, which takes a single argument (which will be the breakpoint title), and specify that TeXcount should insert a breakpoint there:
%TC:breakmacro \CountSection countsection
where countsection
is any non-empty string (no spaces) which will prefix just the title to indicate what kind of count it is. This still requires the -sub=none
option.
You can try this out on the web application first. You don't have to enter a complete document, just a fragment of code will do.
Let me elaborate and respond to questions in the comments: first, an example on how to get the subcounts working with output to the console; second, how to get the counts included in the document.
TeXcount uses breakpoints to split the counts into subcounts. There are no "end breakpoints": a subcount continues until the next breakpoint.
On the other hand, if what you really need is to ignore parts of the document in the counts, not break it into multiple segments, it is possible to mark segments to be ignored using
%TC:ignore
...
%TC:endignore
There are two ways of inserting breakpoints to split the document into subcount regions: either by explicitly setting a breakpoint in the document using the %TC:breakpoint
instruction, or by specifying macros that automatically set a breakpoint. By default (-sub=section
), TeXcount adds breakpoints at \chapter
, \section
, etc.; using the option -sub=none
avoids this. You can then use the %TC:breakmacro
instruction to specify other macros that should set a breakpoint.
Here's a short example, which is not a complete document, just a fragment of code:
%% Use \CountSection to set subcount breakpoints
\newcommand\CountSection[1]{} % No output in document
% Set macro handling rule: ignore text in argument
%TC:macro \CountSection [ignore]
% Specify that \CountSection sets a new breakpoint
%TC:breakmacro \CountSection Subcount
Any text occurring before a breakpoint will be part of the initial subcount
named \textit{top}.
\CountSection{First count section}
This text will be counted as part of the subcount labeled \textit{First count
section}, which will continue until the next breakpoint.
\CountSection{Second count section}
This will be counted as another subcount.
%TC:break Subcount text at the end
This will be the final subcount induced by an explicit breakpoint.
You can try running it through TeXcount on your own computer, or in the online web application, and then try things out to see how it works. If you add \section
macros to the code, these will add additional breakpoints unless you specify -sub=none
. Btw, -sub
is just short for -subcount
, and is only used to choose between a few alternatives of intial breakpoint macros, not to specify your own.
TeXcount by default outputs the counts (and other output) to the console. You can instead write the output to file, which is often useful either to import the output into the document or when writing the output as HTML.
There are a few tricks to importing the count back into the document, all of which require that TeXcount write the output to file and this file then be imported into the document. There is some info on that in the TeXcount FAQ, and an example on StackExchange. It is possible to customise the format of the output using templates (StackExchange example), but that's a bit more advanced and you should have the counting work properly before you start on that.