As daleif has pointed out in a comment, TeXcount does not actually expand LaTeX commands, and so it does not expand the path in \chaptersPath
when trying to locate the included files.
There is a trick that might work. TeXcount can take instructions embedded in the LaTeX document as comments on the form %TC:...
. One such instruction is %TC:subst
which does a simple substitution of a macro with an arbitrary text string. Eg, you could try including somewhere at the top of the document (eg right after the definition of \chaptersPath
) the command
%TC:subst \chaptersPath /my/chapters/path
and see if that does the job. This basically does a simple search and replace. You may have to fiddle around a bit to get the path right (absolute path or relative path?). I'm not entirely sure how it will handle tex files included inside included tex files, but I guess you'll find out if that works out.
Update on bug in TeXcount that causes problems
The TeXcount solution, as well as other uses file inclusion using \include
, may fail due to a bug in TeXcount.
When using \include
to include a TeX file, the .tex
file type is not required. TeXcount was supposed to add .tex
to the file name if not already there, but this seems to have failed and was the root cause of the difficulty of getting the solution to work.
This bug can be bypassed by ensuring all file inclusion using \include
contain the .tex
file type.
An alternative is to fix the code of TeXcount which is quite simple. Just find the function definition sub _parse_include_file
and change the line
if (!$file=~/\.tex$/i) {$file.='.tex';}
to
if ($file!~/\.tex$/i) {$file.='.tex';}
and TeXcount should append .tex
to the file name when required.
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.
Best Answer
It should work to include the line
which tells TeXcount the
\markdownInput
should be processed the same way as\input
. There are other file inclusion rules thaninput
which may be appropriate if the file is not found, but that would have been reported and thus does not seem to be the issue in your case.You will need to run TeXcount with either the
-inc
or-merge
option in order to process included files: these will either process included files separately, or merged into the main document.If you still have problems with this, an alternative to manually replacing the macro with
\input
when running TeXcount is to have TeXcount do the replacement by inserting the TeXcount instructionwhich does a simple text replacement, including in the output.
However, I would not be too confident about having TeXcount processing verbatim or markdown code. Special characters may cause problems, so make sure you inspect the verbose output to verify how the text is being parsed.