Dynamically count words in chapter and insert word count at start of chapter

Assuming the file count.tex is not created, it may be that the command in \write18 is not run, so you might want to ensure that you are actually running TeXcount.

First, for \write18 to execute, LaTeX must be run with a command line option: --enable-write18 or --shell-escapee as explained in the TeXcount FAQ.

Next, you can try running TeXcount without the pipes, and use the option -out=filename to write the output from TeXcount directly to file: eg texcount -out=\jobname.out \jobname.tex to give a minimal example. If this fails, TeXcount is probably not run at all.

Maybe you need to provide the full path to texcount, although I think that would be unlikely to be a problem on Linux.

Is there any information in the LaTeX log? I think it should log that \write18 is being run or not, and perhaps provide some error message if something has gone seriously wrong.


Once you have TeXcount running using \write18, you need to run TeXcount with options that make TeXcount process the included files, and produce statistics either per file or per section depending on you desire.

By default, TeXcount only parses the main file, not files included through \input or \include. To make TeXcount process these, you need to use one of two options: either -inc which makes TeXcount parse each file separately, or -merge which makes it merge the files together and process it as if it was one big file.

What would be closest to the original example would be

texcount -merge -sub=section \jobname.tex

which would merge the files and produce per section summary counts. I think the grep and sed commands might work as in the example with this command: otherwise, it should work with some adjustments.

I recommend running TeXcount from the command line first to see the full output, and then adding the greps and seds to check if they work as desired.

Since your files lie in subfolders, you might want to verify what the value of \thesection is at each relevant point.

A slightly different approach could be to use per-file statistics from TeXcount by running something like

texcount -inc -brief \jobname.tex

which should return one line per file plus one for the total. Potential problems with this approach would be that you'd need the file name (or path) to extract the correct line from the TeXcount output, and that the section headers in your example would be counted as part of the main file rather than the corresponding file.


As a side-note, there are other ways to provide per section counts. One is to run TeXcount on each included file rather than one the whole document and grep out the relevant section. Another is to use a template to customise the output from TeXcount in such a way that it produces LaTeX code: I'll see if I can find or come up with an example of how to do that.

Alternative solution giving counts per file

You can define an alternative file input command which does the counting per file:

\newcommand\countinput[1]{
\input{#1}
\immediate\write18{texcount "#1.tex" -1 -sum > count.txt}
\footnote{FILE #1 CONTAINS \oldinput{count.txt} WORDS}
}

You may experiment with TeXcount options -1, -sum, -brief to find combinations that give you what you want. There is also the -template option for additional customisation, but that might get a bit more tricky.

You can even redefine the existing \input along these lines:

\let\oldinput=\input
\newcommand\countinput[1]{
\oldinput{#1}
\immediate\write18{texcount "#1.tex" -1 -sum > count.txt}
\footnote{FILE #1 CONTAINS \oldinput{count.txt} WORDS}
}
\let\input=\countinput

Do note that you now have to use \oldinput instead of \input to include the counts file.

For experimenting, it might be easier to use \verbatiminput from the verbatim package to include the counts file since the counts file tends to contain characters that TeX treats as special characters: eg "#". That way, you can use the full default output from TeXcount with per section counts should you wish.

Do note that the per file counts will not include the chapter headers as those are part of the main file rather than included in the subfiles.