2

I found the following line of code which adds a word count to my latex document but can anyone explain how it works? It gives a different count to the word counter in overleaf.

\newcommand\wordcount{
\immediate\write18{texcount -inc -sum -0 -template={SUM} main.tex > count.txt}
\input{count.txt}words}

enter image description here

1

1 Answer 1

5

When you have \wordcount in your document and you run the LaTeX engine with the unrestricted shell escape enabled, a process running texcount will be spawned.

The output will be written out in the count.txt file which is then input.

You can improve the working by directly piping the output into the LaTeX engine:

\documentclass{article}

\newcommand\wordcount{\input{|"texcount -inc -sum -0 -template={SUM} \jobname.tex"}}

\begin{document}

As any dedicated reader can clearly see, the Ideal of
practical reason is a representation of, as far as I know, the things
in themselves; as I have shown elsewhere, the phenomena should only be
used as a canon for our understanding. The paralogisms of practical
reason are what first give rise to the architectonic of practical
reason. As will easily be shown in the next section, reason would
thereby be made to contradict, in view of these considerations, the
Ideal of practical reason, yet the manifold depends on the phenomena.
Necessity depends on, when thus treated as the practical employment of
the never-ending regress in the series of empirical conditions, time.
Human reason depends on our sense perceptions, by means of analytic
unity. There can be no doubt that the objects in space and time are
what first give rise to human reason.

Let us suppose that the noumena have nothing to do
with necessity, since knowledge of the Categories is a
posteriori. Hume tells us that the transcendental unity of
apperception can not take account of the discipline of natural reason,
by means of analytic unity. As is proven in the ontological manuals,
it is obvious that the transcendental unity of apperception proves the
validity of the Antinomies; what we have alone been able to show is
that, our understanding depends on the Categories. It remains a
mystery why the Ideal stands in need of reason. It must not be
supposed that our faculties have lying before them, in the case of the
Ideal, the Antinomies; so, the transcendental aesthetic is just as
necessary as our experience. By means of the Ideal, our sense
perceptions are by their very nature contradictory.

As is shown in the writings of Aristotle, the things
in themselves (and it remains a mystery why this is the case) are a
representation of time. Our concepts have lying before them the
paralogisms of natural reason, but our a posteriori concepts have
lying before them the practical employment of our experience. Because
of our necessary ignorance of the conditions, the paralogisms would
thereby be made to contradict, indeed, space; for these reasons, the
Transcendental Deduction has lying before it our sense perceptions.
(Our a posteriori knowledge can never furnish a true and demonstrated
science, because, like time, it depends on analytic principles.) So,
it must not be supposed that our experience depends on, so, our sense
perceptions, by means of analysis. Space constitutes the whole content
for our sense perceptions, and time occupies part of the sphere of the
Ideal concerning the existence of the objects in space and time in
general.

%TC:ignore
This document has \wordcount words.
%TC:endignore

\end{document}

Using \jobname makes the command portable to other documents with no need to adapt it to the actual file name of the main file.

The %TC tags are used for avoiding texcount counting the last sentence.

What are the used options to texcount?

  1. -inc will count also files that are compiled with \input or \include
  2. -sum only produces the grand total of words
  3. -0 doesn't add a newline at the end of the output
  4. -template={SUM} specifies that the output will consist of just the grand total

enter image description here

4
  • Thank you for that @egreg, a comprehensive explanation. I find inserting the line Word count: \wordcount words The total number of words is displayed on a new line. Is there a command to suppress a new line at the beginning of input?
    – phald
    Jan 30, 2020 at 16:42
  • @phald I don't get line breaks. See the picture I added.
    – egreg
    Jan 30, 2020 at 16:45
  • Odd. I do (see picture) but no reason why that should happen
    – phald
    Jan 30, 2020 at 16:50
  • If i use \mbox{Word count: \wordcount words} then I can force the text onto one line.
    – phald
    Jan 30, 2020 at 17:00

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.