0

I'm trying to do a wordcount of my thesis project in LaTeX. I'm using VIM with VIMLaTeX on an ubuntu machine.

I have the following code in my main.tex file

\newcounter{Hoofdstuk}
\newcommand{\file}{empty}
\setcounter{Hoofdstuk}{1}
\forloop{Hoofdstuk}{1}{\value{Hoofdstuk}<20}
{
  \renewcommand{\file}{Hoofdstuk\arabic{Hoofdstuk}/\arabic{Hoofdstuk}}
  \IfFileExists{\file}{
    \input{Hoofdstuk\arabic{Hoofdstuk}/\arabic{Hoofdstuk}}
  }{}
}

To input al my chapters into my main file. Now i want to count all the words in my thesis with the following code:

\usepackage{moreverb}
\immediate\write18{texcount -inc -incbib -sum main.tex > /tmp/wordcount.tex}
\newcommand\wordcount{
\verbatiminput{/tmp/wordcount.tex}}

And in essence this code works, for files I input with just the filename. But for files inputted in the forloop it errors and says it can't find the file: './Hoofdstuk\arabic{' and a second error that is encountered and '}' without a corresponding '{' which seems logic knowing the first error.

Does anybody know how to fix this? I also get the same errors when running texcount -inc -incbib main.tex in terminal

  • Not an exact solution, but if all your files are named Hoofdstuk<something>, is there a reason why you cannot just generate several lines of code with \input{Hoofdstuk<something>/Hoofdstuk<something>} using vim? See for example vim.fandom.com/wiki/Making_a_list_of_numbers – LokiRagnarok Oct 15 '19 at 11:27
  • Also, if \file already contians your path, why don't you use \input{\file}? – LokiRagnarok Oct 15 '19 at 11:32
  • @LokiRagnarok the \file was inserting when i was trying to debug, so yes I should and could use it in the \input ofcourse. For your other suggestion of inserting a list with VIM. Yes that would be a tactical solution, but I liked it more to do it this way with latex because I use this template for a lot of reports and don't know how many chapters those reports are going to have beforehand, and I didn"t want to edit it manually each time :) – Blub21 Oct 16 '19 at 12:46
0

There are two things that fail, causing different issues.

First, you need to be aware that TeXcount only parses the TeX code, using heuristic rules to try to distinguish text from macros. However, it does not actually run TeX, so it will not actually run the \forloop and expand the code it produces. It therefore does not run \input on the files Hoofdstuk1.tex, Hoofdstuk2.tex, etc. This is why it will not process these files.

Next, what it does do with the \input statement is try to parse the line

    \input{Hoofdstuk\arabic{Hoofdstuk}/\arabic{Hoofdstuk}}

purely as it stands, and can't make any sense of it since it is not a file name. In fact, I suspect there is a minor bug here as it seems it fails to match up the curly braces correctly, and ends up with a left over }, which causes the error message.

The reason the \input statement within \forloop is parsed by TeXcount is that TeXcount does not come with a macro handling rule for \forloop, so the parameters to \forloop are parsed as text. This can be "fixed" by adding a TeXcount instruction providing a macro handling rule:

%TC:macro \forloop [ignore,ignore,ignore,xx]

Inserting this line at any place prior to the first use of \forloop will tell TeXcount that it takes four arguments, all of which should be ignored: the xx rule is just a stronger form of ignore which does a little less in terms of trying to process the contents (not sure if it is really needed or if ignore would be sufficient in general). All this does is make TeXcount ignore \forloop and its parameters, and thus prevent the error message.

To actually solve the problem of actually including the Hoofdstuk-files, I think the simplest is to change the texcount instruction so it includes them. I think this might do the trick if these are the only files on the Hoofdstuk*.tex form:

\immediate\write18{texcount -inc -incbib -sum main.tex Hoofdstuk*.tex > /tmp/wordcount.tex}

This just tells TeXcount explicitly to include all these files as well.

NB: I haven't actually tested this, and it might need a little tweaking to get it to work the way you want. But I think this is the most viable approach.

If you need to be more specific about the selection of files to include, you could accumulate the list of files you have included in a macro variable, say \filelist, and then run TeXcount with a command like

\immediate\write18{texcount -inc -incbib -sum main.tex \filelist > /tmp/wordcount.tex}

which is basically the same, except you can be more secure that the files counted are the same as those included in the document.

| improve this answer | |
  • Thanks! I took your first suggestion with the Hoofdstuk*.tex and it works beautifull! – Blub21 Oct 16 '19 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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