6

Hi is there something like texcount to count all the words of a latex file including the ones linked by input and include?

Solution for linux as well as for windows are fine for me.

2 Answers 2

10

TeXcount can include subfiles included by \input, \include or the subfiles package. For this, you need to use the option -inc which will automatically parse the subfiles, or -merge which will merge the included subfiles into the main document.

3
  • 4
    Perfect. Thank you: texcount inhalt.tex -inc -total was exactly what I was looking for.
    – magu_
    Commented Jun 4, 2014 at 11:37
  • I've tried texcount main.tex with no result: no words counted. Is there a simpler and complete way to execute this command?
    – nilon
    Commented Jan 10, 2017 at 11:49
  • Does it fail to execute, or does it execute but return a zero count? Commented Jan 10, 2017 at 13:20
3

Save this script as buildFile.pl and run it with Perl or execute it: buildFile.pl < main.tex > mainTotal.tex. Then you have one TeX document which can use texcount or something else. However, sometimes it is easier to create a pdf and count the words with other tools, like pdftotext <file>.pdf - | wc -w

#!/usr/bin/perl -w
##################################################
# build one main file
# (C) 2005 Herbert Voss
##################################################
#
#use strict;
# 
# call with
# 
# ./buildFile.pl main.tex > mainTotal.tex
# 
# in the mainTotal.tex will be comments all starting
# with %%% which will not be of interest
# 

sub p_inc {
  $fileName = shift;
    if ( open (my $file, "$fileName.tex") ) {
      print "%%%---------- open: ", $fileName, "\n";
      while (<$file>) {
        if ((/^\s*\\include{\s+(\S+)/i) or (/^\s*\\input{\s+(\S+)/i)) {
          my $include = $1;
          chomp($include);chop($include);
          print "%%%%%%%%% jump to ", $include, "\n";
          p_inc($include);
        } else { print unless /^\s*(#|$)/; }
      }
      print "%%%---------- close: ", $fileName, "\n";
      close $file;
    } else { print "%%%<===== file does'n exist\n"; }
}
#
@lines = (<>);
for $line (@lines) {
  next if $line =~ /^\s*(%)/;
  if (($line =~ /^\s*\\include{\s*(\S+)/i) or ($line =~ /^\s*\\input{\s*(\S+)/i)) {
    my $include = $1;
    chomp($include);
    chop($include);
    print "%%%%%%%%%%% jump to ", $include, "\n";
    p_inc($include);
  } else { print $line; }
} 
3
  • 1
    I didn't know about pdftotext. This works like a charm. It does however also count stuff like toc,tof,...
    – magu_
    Commented Jun 4, 2014 at 9:24
  • 1
    @magu_ There are options -f -l for pdftotext, you can give the page range and you can exclude toc, tof etc. by this way.
    – wipet
    Commented Jun 4, 2014 at 9:52
  • Herbert. This works. It is a very nice way to characterize also non latex files. However I feel a bit more comfortable with the texcount answer, since it does this natively. But thank you anyway since you made me learn something new.
    – magu_
    Commented Jun 4, 2014 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .