2

I'm quite new to verbatim and quite confused too.

I wanted to include a 'simple' wordcount at the end of my essay (as it has been demanded) and didn't want to run an external pdf-wordcount software every time I changed something. So, I found the first function titled \wordcount and using verbatim in another post and adapted it, so it fits its purpose better. The text.tex document holds in general my essay except appendix and excludes input files etc., but I noticed wordcount also counts my footnotes (it really shouldn't).

Therefor my question: Is there any plausibly easy way to either exclude footnotes from being counted or do I have to display the extended wordcount (\wordcountt) everytime and substract those 'Words outside text' myself?

Thanks a lot for any help!

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{verbatim}
\usepackage{moreverb} % for verbatim ouput

\newcommand{\wordcount}[1]{
    \immediate\write18{texcount -nc -nobib -1 -sum text.tex > #1-words.sum}
    \begin{flushright}\footnotesize \textsc{Wordcount:} \input{#1-words.sum}words.\end{flushright}
    }
\newcommand{\wordcountt}[1]{
    \immediate\write18{texcount -nobib -sum text.tex > #1-words.sum}
    \input{#1-words.sum}
    }

\begin{document}

\input{text}

\wordcount{text.tex}
\wordcountt{text.tex}

\end{document}

1 Answer 1

4

There are two ways to ignore the contents of specific commands such as \footnote{} for the word count in TeXcount.

The first way is to add a 'magic command' to the file being counted. This is described on page 18 of the TeXcount manual. The examples there, while mentioning ignore, may not be very clear, a simpler example is provided in Using Texcount but how do I ignore macro contents?.

The syntax of the comment is %TC:macro \macroname [options for each argument], so in this case it becomes %TC:macro \footnote [ignore]. This comment needs to be placed in all files that you count through the \wordcount{} macro, so in this case you should put the comment at the start of text.tex.

If you have multiple files then an easier alternative is the second way to set options for texcount, which is to provide an option file. This is a text file that contains all the options that you want to set. The syntax is slightly different, instead of %TC:macro this file should contain the syntax %macro. The file can be loaded with the command line flag -optionfile=filename. When you load this option file you don't need any comments in the .tex files anymore.

So for example with the following file called texcount.cfg:

%macro \footnote [ignore]

and the following macro call in your main document:

\immediate\write18{texcount -optionfile=texcount.cfg -nc -nobib -1 -sum text.tex > #1-words.sum}

the footnotes are ignored.

Note that it is not necessary to have the filename text.tex hardcoded into the \write18 call. You can use #1 to denote the first argument to the macro (which is the filename), that way you can give the file any name you want. Furthermore, the code in the question actually does not use \verbatiminput for the summary file. I corrected that in the MWE below, and removed also \usepackage{moreverb} because it is not necessary for verbatim input/output (that is handled by the verbatim package).

MWE, first version (with comment):

tcfootnotes.tex

%TC:macro \footnote [ignore]
This is a text \footnote{with footnotes}

Main file:

\documentclass{article}
\usepackage[utf8]{inputenc}
% the following \usepackage statement
% is only to make the page smaller for the screenshot,
% it can be removed for the actual document
\usepackage[totalheight=8cm]{geometry}

\usepackage{verbatim}

\newcommand{\wordcount}[1]{
    \immediate\write18{texcount -nc -nobib -1 -sum #1 > #1-words.sum}
    \begin{flushright}\footnotesize \textsc{Wordcount:} \input{#1-words.sum}words.\end{flushright}
    }
\newcommand{\wordcountt}[1]{
    \immediate\write18{texcount -nobib -sum #1 > #1-words.sum}
    \verbatiminput{#1-words.sum}
    }

\begin{document}

\input{tcfootnotes}

\wordcount{tcfootnotes.tex}
\wordcountt{tcfootnotes.tex}

\end{document}

MWE, second version (with option file):

tcfootnotes.tex

This is a text \footnote{with footnotes}

texcount.cfg:

%macro \footnote [ignore]

Main document:

\documentclass{article}
\usepackage[utf8]{inputenc}
% the following \usepackage statement
% is only to make the page smaller for the screenshot,
% it can be removed for the actual document
\usepackage[totalheight=8cm]{geometry}

\usepackage{verbatim}

% note the addition of -optionfile=texcount.cfg in the command below
\newcommand{\wordcount}[1]{
    \immediate\write18{texcount -optionfile=texcount.cfg -nc -nobib -1 -sum #1 > #1-words.sum}
    \begin{flushright}\footnotesize \textsc{Wordcount:} \input{#1-words.sum}words.\end{flushright}
    }
\newcommand{\wordcountt}[1]{
    \immediate\write18{texcount -nobib -sum #1 > #1-words.sum}
    \verbatiminput{#1-words.sum}
    }

\begin{document}

\input{tcfootnotes}

\wordcount{tcfootnotes.tex}
\wordcountt{tcfootnotes.tex}

\end{document}

Result (same for both versions):

enter image description here

1
  • Thanks, you are a legend to me!
    – Aureius
    Sep 16, 2021 at 15:11

You must log in to answer this question.

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