I am writing a proposal for which it is required to give the number of words of the main section in the proposal itself. I found this answer on how I can get a word count on a LaTeX document:

Is there any way to do a correct word count of a LaTeX document?

But this is only for entire documents. I was wondering if it is possible to get a word count in a LaTeX document of a section/part of text and actually return this in the text as well.

I am open to suggestions with Sweave. One thing I could think of is to write the entire section in a Sweave block in an R character string. Then simply get the word count from that and return the string. This would however include all LaTeX codes used and not only intended words. Another solution I thought of was to extend this R code to wrap the string in an empty document, compile that and use tools available to get a wordcount from that, then return it. This seems like it could work but I am hoping there is a simpler solution.


You can use texcount to count the words. It automatically produces subcounts for the sections.

Here's a new macro that calls texcount, extracts the subcount for the current section, and then inserts the word count into the document. It requires write18 to be enabled, and texcount must be in your path (or you have to include the full path to the executable in the macro).

    \immediate\write18{texcount -sub=section \jobname.tex  | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'count.txt'}

In publishing and graphic design, lorem ipsum is placeholder text (filler text) commonly used to demonstrate the graphics elements of a document or visual presentation, such as font, typography, and layout. The lorem ipsum text is typically a section of a Latin text by Cicero with words altered, added and removed that make it nonsensical in meaning and not proper Latin.

\section{Main Stuff}
Even though "lorem ipsum" may arouse curiosity because of its resemblance to classical Latin, it is not intended to have meaning. Where text is comprehensible in a document, people tend to focus on the textual content rather than upon overall presentation, so publishers use lorem ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text. In spite of its basis in Latin, use of lorem ipsum is often referred to as greeking, from the phrase "it's all Greek to me," which indicates that this is not meant to be readable text.

Today's popular version of lorem ipsum was first created for Aldus Corporation's first desktop publishing program Aldus PageMaker in the mid-1980s for the Apple Macintosh. Art director Laura Perry adapted older forms of the lorem text from typography samples — it was, for example, widely used in Letraset catalogs in the 1960s and 1970s (anecdotes suggest that the original use of the "Lorem ipsum" text was by Letraset, which was used for print layouts by advertising agencies as early as the 1970s.) The text was frequently used in PageMaker templates.


| improve this answer | |
  • I tried this but it didn't work when there was a subsection in the section. You probably need a grep '\\<section\>' or equivalent. – user10274 Feb 15 '12 at 10:46
  • Awesome, thanks! Only tried it on whole sections and that works just fine. – Sacha Epskamp Feb 15 '12 at 10:52
  • 2
    @MarcvanDongen: If you have subsections, you should add sub=section to the texcount command. – Jake Feb 15 '12 at 11:44
  • Could someone briefly describe how that line of TeX/Shell-fu works in plain english? Very interesting. Particularly curious why there's an '18' there. – MercurialMadnessMan Feb 15 '12 at 16:46
  • 2
    @ Jake, Sorry I was trying to do that in windows. May be this is applicable for Linux. – Mahzabin Sep 28 '12 at 1:09

You asked for a LaTeX solution, but for completion I'll provide a ConTeXt solution as well, it might be helpful for someone. It does not rely on external programs.

    userdata = userdata or { }

    function userdata.wordcount(listname)
        filename = file.addsuffix(tex.jobname,"words")
        if lfs.isfile(filename) then
            local w = dofile(filename)
            if w then
                if type(w.categories[listname]) == "table" then




% Set up the word count
\setupspellchecking [state=start, method=2]

\setupspellchecking [list=foo]
\startsection [title=Foo]
    Foo Bar

\setupspellchecking [list=lorem]
\startsection [title=Lorem]
    Lorem ipsum dolor sit

\setupspellchecking [list=stop]

\startsubject [title=Statistics]
    Words in Foo:   \wordcount [foo]
    Words in Lorem: \wordcount [lorem]


The result:


The lua function reads in the external file \jobname.words that is created by the \setupspellchecking command. It returns a a lua table with some statistical information and extracts the relevant data. \wordcount is just a nice wrapper to keep the interface “contextish”.

By default only words with at least four characters are being counted. In the example I set the threshold to two characters.

Notes: In the example the text in the section heading is counted as well. Unlike the usual ConTeXt behaviour, this word count implementation needs two context runs.

The code is a modified version of the one found in the ConTeXt distribution (s-lan-03).

| improve this answer | |

If you are on windows, you can visit this site and install winedt. It has a built in word count feature (Document->word count). To count words in a para or section, you have to select that section and perform the count.

| improve this answer | |
  • Does it expand macros and count the resulting words? – user10274 Feb 15 '12 at 10:25
  • Fortunately I am on Linux :) But I have a Windows machine available so I'll look into it. Thanks. – Sacha Epskamp Feb 15 '12 at 10:38
  • @Marc, I have not tested the macro thing but I think it is doing good at this. – user11232 Feb 15 '12 at 11:02

If you are using Emacs with Auctex, you can select the text of the current region and run Latex on that (usually via the C-c C-r keybinding, which maps to TeX-command-region). This will create a file _region_.tex that can be compiled or texcount-ed whose body contains just the part of the Latex document that was highlighted; likewise tools such as tex4ht or PDF tools that can word count the output can be used to generate a wide variety of estimates of the word count.

Several other Latex editors support similar "region compilation", e.g., Kile. See LaTeX Editors/IDEs

| improve this answer | |

Counting words from chapters and in windows enviroment (without e.g. Cygwin).

%\immediate\write18{texcount -sub=chapter \jobname.tex  | grep "Chapter" | sed -e 's/+.*//' | sed -n \thechapter p > count.txt}
\immediate\write18{powershell -command " & { texcount -sub=chapter \jobname.tex  | findstr "Chapter" | ForEach-Object {$_.substring(0,3)} | Select -Index (\thechapter - 1) }" > count.txt}
| improve this answer | |
  • I get the error Undefined control sequence. \wordcount. – ikreb May 3 at 18:18

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.