# LaTeX: Printing word count of an imaginary section

I would like to print the word count in a section of a document. However section doesn't mean \section{...}. It just means an arbitrary collection of paragraphs. The word count should exclude captions and footnotes.

\begin{document}

stuff

% start word count

paragraph #1 \footnotes{...}

\begin{figures}...\end{figures}

paragraph #2

% end word count

\wordcount
% or some other command that prints the word count

other stuff

\end{document}


Do I use a container of some sort? LaTeX equivalent of div tags? Or do I still use \section{...} but suppress the section heading?

A solution will be greatly appreciated!

• Welcome to TeX.SX! Maybe making the “section” in a separate document that can be compiled as standalone. – egreg Feb 12 '18 at 16:13
• I can clearly do that. But I hope to have a dynamic, automated solution within the original document. As I work in the "section," the word count updates itself. :) – Angle Qian Feb 12 '18 at 16:17
• @Angle Quian tex.stackexchange.com/questions/44618/… – sergiokapone Feb 12 '18 at 16:17
• @sergiokapone That relies on \section{...}. Then, how do I suppress the section heading? – Angle Qian Feb 12 '18 at 16:25

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.

• Could you elaborate a little more on your solution? Perhaps in conjunction with an implementation? How do I set breaks (beginning and end)? How do I print the word count on the document? Thanks so much! – Angle Qian Feb 12 '18 at 17:18
• If I'm defining a new command, how do I obtain the word count for a section defined by a breakpoint? -sub[breakpoint-title] returns an error – Angle Qian Feb 12 '18 at 17:32
• @AngleQian: I've added some additional info trying to answer you questions. Upon rereading your original question, I realise an alternative to subcounting could be to mark parts of the document to be ignored by TeXcount, and have added some info on that as well. Hope that helps. – Einar Rødland Feb 13 '18 at 1:43