1

I want to count the number of characters within a section and then display if that value is below, in, or above the limit. I have grabbed the code from Dynamically count and return number of words in a section. And I have the following (compile it with pdflatex --shell-escape mwe.tex).

\documentclass{article}

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

\usepackage{lipsum}

\begin{document}

\section{test1}
\wordcount
\ifnum\wordcount<1000 Below 
\ifnum\worcount>2000 Above
\else Inside

foo bar


\section{test2}
\wordcount

foo bar

foo bar

foo bar

\end{document}

The code above doesn't work because \wordcount is not really a value in LaTeX sense. Is there a way that I can translate to a real value in order for the code to work?

Other solution would be to use Environment that counts words inside, but on that case I would need to save the value of each counter and display it latter.

3
  • If you must count the words before typesetting, the answer you found along with the environ package BODY macro may help. Commented Apr 3, 2013 at 18:13
  • This is a very amorphous question. For example, does mathmode get counted? Is a word defined by glue, or by white-space? Examples (1 word or 2?): 30~GPa; \upshape next; minute\ldots\passed; end\par Beginning... Commented Apr 3, 2013 at 18:15
  • Well, the word count shall be performed in the same way you perform it in the final PDF. With this in mind, glue would represent a space (not a word), I think math shall be considered, and of course LaTeX macros shall not be counted, but the content inside macros.
    – cacamailg
    Commented Apr 3, 2013 at 18:33

1 Answer 1

1

The Perl script TeXcount (http://app.uio.no/ifi/texcount/) will produce a word count of your TeX document, with breakdowns by section. An example is shown below. You may also omit the .tex file and add a -help flag to see the myriad options.

$ perl texcount.pl paper.tex 

File: paper.tex
Encoding: ascii
Words in text: 5824
Words in headers: 22
Words outside text (captions, etc.): 1032
Number of headers: 8
Number of floats/tables/figures: 18
Number of math inlines: 48
Number of math displayed: 0
Subcounts:
  text+headers+captions (#headers/#floats/#inlines/#displayed)
  90+12+0 (1/0/6/0) _top_
  162+1+0 (1/0/1/0) Section: Abstract
  751+1+0 (1/0/1/0) Section: Introduction
  795+1+0 (1/0/6/0) Section: Methods
  3964+2+0 (1/0/20/0) Section: Results \& Discussion
  44+1+0 (1/0/0/0) Section: Acknowledgments
  0+2+320 (1/9/0/0) Section: Table Legends
  18+2+712 (1/9/14/0) Section: Figure Legends
2
  • 2
    Perhaps an example output would improve this answer, otherwise, it is a mere link-answer
    – user31729
    Commented Jul 17, 2014 at 21:02
  • @ChristianHupfer Thanks for the suggestion. I have added some example output. Commented Jul 18, 2014 at 18:04

You must log in to answer this question.

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