Asger Sommer PROJECTS BLOG ARCHIVE

Counting Units Directly in LaTeX

Ever wondered how to deal with requests like these, when writing your assignments in LaTeX?

You have a maximum of two pages to do it. One page equals 2400 units (incl. letters, digits and spaces).

Like many others, I’ve been in that situation, and as far as I know there doesn’t exist a LaTeX package for inserting the unit count directly in the document. TeXcount does exist, but it will require you to manually determine and write the character count, and doesn’t count spaces as a part of the character count. So what to do?

Got a computer running macOS or Linux with a LaTeX installation? Say no more, here’s what to do:

  1. Make sure TeXcount works (i.e. does texcount work in the command line?). If not, install it.
  2. Insert the following code in your preamble

    % Run texcount to get word and character count
    \immediate\write18{texcount text1.tex text2.tex -total -template="{1}" -out=words.sum}
    \immediate\write18{texcount text1.tex text2.tex -total -template="{1}" -char -out=chars.sum}
    % Run bash commands to get the sum (i.e. unit count)
    \immediate\write18{\unexpanded{paste -d+ words.sum chars.sum | bc > sum.sum}}
    % Define macro \unitcount for including the unit count
    \newcommand\unitcount{\input{sum.sum}}
    
  3. Change text1.tex, text2.tex etc. to the relevant .tex file(s) you want to count units in.
  4. Use \unitcount wherever you want it in your LaTeX document to get the unit count.

So, here’s the caveat: it might slightly overestimate the character count. While TeXcount counts characters (abc etc.) and punctuation (,.), it doesn’t count spaces. So here I’m using the amount of words as a proxy for spaces. Also, it will require your LaTeX-editor to allow shell escaping.

I’d like to one day make this work independently of the shell commands (which is why it doesn’t just run as one combined shell command), but alas – if it works, it works.

Liked this post?

If you liked this post, you can share it with your followers or follow me on Twitter!