Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
Perfect. Thank you: texcount inhalt.tex -inc -total was exactly what I was looking for. –  magu_ Jun 4 at 11:37

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; }
} 
share|improve this answer
    
I didn't know about pdftotext. This works like a charm. It does however also count stuff like toc,tof,... –  magu_ Jun 4 at 9:24
    
@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 Jun 4 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_ Jun 4 at 14:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.