LaTeX Word Counter: TeXcount, Overleaf and detex
November 11, 2026 · 10 min read
LaTeX Word Counter: Accurate Counts with TeXcount, Overleaf and detex
A LaTeX word counter has to skip commands, comments and math that a plain counter treats as words. The standard tool is TeXcount: Overleaf runs it behind File > Word Count, and TeX Live ships it as texcount. Below, one small file gets counted several ways, so you can see exactly what each number includes.
Why a Plain Word Counter Overcounts LaTeX Source
Here's a short .tex file with the usual suspects. It has a heading, bold text, a citation, inline and displayed math, a figure caption and a comment.
\documentclass{article}
\begin{document}
\section{Results}
% TODO: tighten this paragraph before submission
Our model is \textbf{faster} than the baseline \cite{smith2020}.
The error falls as $n^{-1/2}$ when the sample grows.
\begin{equation}
E = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2
\end{equation}
\begin{figure}[h]
\centering
\caption{Error against sample size}
\end{figure}
We test this claim on three datasets.
\end{document}
A reader sees three sentences of body text, 22 words in all. Here's the same file counted four ways:
| Method | Count | What ends up in the number |
|---|---|---|
wc -w sample.tex | 51 | Every space-separated token: the preamble, \begin lines, the comment, the cite key and each piece of the equation |
pdftotext sample.pdf -, then wc -w | 43 | The compiled page: the heading, the [?] citation marker, split-up math, (1), Figure 1: and the page number |
| TeXcount, text words | 22 | Body text only; the heading, caption and formulae go on separate lines |
TeXcount with -sum | 29 | 22 text words, 1 header word, 4 caption words, plus 1 per formula |
We ran wc and pdftotext on this exact file, and got the TeXcount figures by pasting it into the TeXcount web service described below.
The gaps have clear causes. Markup inflates the wc count: the comment alone adds 7 words, and \documentclass{article} counts as one. The PDF count drops the markup but picks up text the typesetter adds. So a generic word counter treats LaTeX source as prose, and counting the compiled page isn't clean either.
How Do You Count Words in Overleaf?
Open your project and choose File > Word Count. Overleaf's documentation says the count runs TeXcount, skips LaTeX commands, and by default leaves out references, headers, captions, floats and displayed math. What you get is close to the "text words" row in the table above.
Two setup details trip people up. First, the main document must sit at the project's top level, not inside a folder. Second, TeXcount follows \input and \include but not \import from the import package. For that one, Overleaf's docs suggest adding this line to the preamble and recompiling:
%TC:fileinclude \import dir,file
The %TC: comments you add here work the same way on your own machine, which the next sections cover.
Running TeXcount from the Command Line
TeXcount is a Perl script, and CTAN lists it as part of both TeX Live and MiKTeX. If you have a full TeX distribution, you probably have it already.
texcount thesis.tex # counts this one file
texcount -inc thesis.tex # also counts \input and \include files, each separately
texcount -merge thesis.tex # merges included files in place, one combined count
texcount -sum thesis.tex # adds a total: all words plus formulae
texcount -1 -sum thesis.tex # prints only that total, as a single number
texcount -inc -html -v -sum thesis.tex > report.html
The last command writes a colour-coded HTML report showing which words landed in which counter. It's the quickest way to audit a surprising number. Other useful flags are -brief (one summary line per file), -total (no per-file sums), -char (letters instead of words) and -freq (word frequencies).
TeXcount reports seven separate counts. Here's how our sample splits across them:
- Words in text: paragraph prose, including the argument of
\textbf. In the sample, the three body sentences. - Words in headers:
\sectionand similar titles. Here, the word Results. - Words outside text: captions and footnotes. Here, Error against sample size.
- Number of headers: one, for the
\section. - Number of floats: one, for the figure.
- Math inlines: one, the
n^{-1/2}term. - Math displayed: one, the
equationenvironment.
By default, -sum adds the three word counts and the two formula counts, which is why the sample's sum is 29. You can pass weights instead, such as -sum=1,0,0,0,0,0,0 for text words only.
One limitation to know: TeXcount reads your source with built-in rules and never compiles it. A macro you defined yourself, or one from a less common package, can be counted wrongly until you add a rule. Numbers in running text also count as words.
Including and Excluding Text with %TC Directives
TeXcount reads instructions from LaTeX comments that start with %TC:. LaTeX ignores them, so they're safe to leave in a submitted file.
To leave a block out, such as a title page or an appendix, wrap it:
%TC:ignore
\maketitle
\begin{abstract}
This abstract is not counted.
\end{abstract}
%TC:endignore
Everything between the two markers disappears from all seven counters. By default, the abstract environment counts as body text, so this matters when a limit excludes it.
Citations are skipped by default, apart from any optional text like [p. 4]. To count each \cite as exactly one word, add a macro count rule to the preamble:
%TC:macrocount \cite 1
Overleaf's docs show a different approach, %TC:macro \cite [option:text,text], which parses the citation key as text, so a single-key citation counts as one word.
Table contents are ignored by default. Overleaf's docs suggest these two lines to count them:
%TC:group table 0 1
%TC:group tabular 1 1
group is an older alias for envir. The last number tells TeXcount to parse the environment's contents as counted text.
For the bibliography, run texcount -incbib or add %TC:incbib to the file. It reads a thebibliography environment or the BibTeX .bbl file. On Overleaf, its docs say you also need a latexmkrc file in the project for this to work. Per the same page, TeXcount doesn't currently support BibLaTeX, so a biblatex reference list won't be counted this way.
How Do You Count Words in LaTeX Without Installing Anything?
Use the TeXcount web service. You paste LaTeX code or upload a .tex file. Then pick a sum mode (text words, all words, or words plus math) and decide whether to include the bibliography. It runs TeXcount itself, not an approximation of it. The one catch: it can't follow \input or \include files, so paste each chapter separately or merge them first.
If you're on Linux or macOS and have detex from opendetex, a pipe works too:
detex thesis.tex | wc -w
detex -n thesis.tex | wc -w # don't follow \input or \include
Per its man page, detex removes comments, control sequences and all math. It also ignores the equation, tabular and verbatim environments, among others, and -e replaces that list. Because it keeps heading and caption words, its total usually lands above TeXcount's text-word count.
You can count the compiled PDF with pdftotext thesis.pdf - | wc -w, but treat it as a rough ceiling. As the sample showed, page numbers, figure labels and equation numbers sneak in. Two desktop editors also have menus for this: Kile under File > Statistics, and Texmaker when you right-click inside its PDF viewer. Our guide to LaTeX editors compares Texmaker with the other desktop options.
Can LaTeX Print the Word Count Inside the Document?
Yes, if you let LaTeX run TeXcount during compilation. The TeXcount FAQ suggests writing the count to a file and reading it back:
\immediate\write18{texcount -1 -sum \jobname.tex -out=\jobname.sum}
\newcommand\wordcount{\input{\jobname.sum}}
Put both lines in the preamble, then write \wordcount wherever the number should appear. -1 -sum makes the file hold a single number. Change it to -1 -sum=1,0,0,0,0,0,0 if you want body text only.
\write18 runs a shell command. TeX Live's default restricted mode doesn't allow texcount, so compile with pdflatex -shell-escape thesis.tex. Shell escape lets a document run any command on your machine, so only enable it for files you wrote yourself. Overleaf's documentation shows a similar pattern with a \quickwordcount command.
Which Parts Should a LaTeX Word Counter Include?
The tools can't answer this one for you. Your journal, department or examiner decides whether the abstract, captions, footnotes, headings, equations and references count. The Oxford Mathematical Institute's word count FAQ says the same: only the people assessing the work can formally say how it's counted.
A common reading is body text only, which matches TeXcount's default and Overleaf's figure. A broader rule might include captions and footnotes, which TeXcount reports as words outside text. Word processors differ on footnotes too, as our guides to the Microsoft Word counter and the Google Docs word counter show.
We prefer reporting TeXcount's text-word count and attaching the -v -html report when the rule is ambiguous. The reviewer then sees exactly which words were counted, and nobody argues over a number.
Counting Words When You Draft in Markdown First
Many people draft in Markdown with $...$ math and convert to LaTeX later. Our editor's status bar shows live word, character and line counts, but it counts the raw text. That means a $\frac{a}{b}$ formula and any LaTeX command count as words.
The sentence counter has an Ignore markdown syntax setting that strips Markdown symbols, not LaTeX commands. Use it for the prose, then treat the math separately.
For a limit that matters, convert first. The Markdown to LaTeX converter runs in your browser and passes $...$ math through unchanged, so you can run TeXcount on the resulting .tex. Our post on converting Markdown to LaTeX with Pandoc covers the command-line route. To check a single formula before it goes in, try the LaTeX equation editor.
A LaTeX word counter is only as accurate as its rules, and TeXcount lets you adjust almost all of them. Use Overleaf's menu or texcount for the body-text number, add %TC: comments to match your word limit, and keep the HTML report as evidence. If you draft in Markdown first, write in the editor, then convert and count the .tex file.