Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
3  
Can you not just estimate the number of words, add some noise to the made up number and enter that? I think if the person responsible for processing your proposal is going to count the words, he/she needs to rethink his or her priorities. :) –  Roman Luštrik Feb 15 '12 at 10:06

4 Answers 4

up vote 25 down vote accepted

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).

\documentclass{article}
\newcommand\wordcount{
    \immediate\write18{texcount -sub=section \jobname.tex  | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'count.txt'}
(\input{count.txt}words)}

\begin{document}
\section{Introduction}
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.

\wordcount
\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.

 \wordcount
\section{Conclusion}
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.

\wordcount
\end{document}

share|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. –  Marc van Dongen 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
    
@Jake Thanks for that. –  Marc van Dongen Feb 15 '12 at 11:55
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

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.

share|improve this answer
    
Does it expand macros and count the resulting words? –  Marc van Dongen 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. –  Harish Kumar Feb 15 '12 at 11:02

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.

\startluacode
    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
                    context(w.categories[listname].total)
                else
                    context(w.total)
                end
                context.par()
            end
        end
    end
\stopluacode

\def\wordcount{%
    \dosingleempty\dowordcount}

\def\dowordcount[#1]{%
    \ctxlua{userdata.wordcount("#1")}}

\starttext

% Set up the word count
\ctxlua{languages.words.threshold=2}
\setupspellchecking [state=start, method=2]

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

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

\setupspellchecking [list=stop]

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

\stoptext

The result:

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).

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.