10

Is there a way to count characters in a document portion? I need to write a summary that is 3000 characters long at most, and every time I compile the document I copy/paste the resulting text to Word to do the count. I found this post but it seems that solution doesn't work. What I would like to have is something like:

\begin{characterCount}
  % write here all the text
\end{characterCount}

if characterCounter > threshold:
    {\color{red} Character count exceeded! }
aaragon
  • 3,041
  • You should try to see with your text editor. Which one do you use ? Or with terminal commands such as wc. – Jérôme Dequeker Nov 10 '15 at 08:03
  • 'doesn't work' is not a helpful statement. What does not work? The above fragment isn't helpful neither. Are you after pure character count, i.e. without using macros? –  Nov 10 '15 at 08:04
  • I'm looking for an automated way, as in the pseudo-code I give, it would be nice if the system alerts me when I'm above some threshold without me having to explicitly find it. – aaragon Nov 10 '15 at 08:04
  • I don't know of an automated way of doing this, but possibly building on the solution to the question here http://tex.stackexchange.com/questions/48498/how-to-count-all-characters-including-spaces you may build a script and have this automated after every recompile of the document? (on windows make a .bat and run) – Fredrik Johansson Nov 10 '15 at 08:43

1 Answers1

11

TeXcount is your friend. It is a perl script for counting words in LaTeX documents. Although the ouput is a separate plain text file, you can include easily verbatim in the own LaTeX document without modify the result, because you can count any arbitrary chunks of text and avoid some parts using the pseudoenvironment comments %TC:ignore ... %TC:endignore. Moreover, you can obtain subcounts at some section levels and separate counts of words for main text, headers, captions, inlined math and displayed math (and more, although this is out of the question. Run texdoc texcount for more information).

mwe

\documentclass{article}
\usepackage{moreverb,savetrees}
% Compile with  --enable-write18 or --shell-escape options   
\immediate\write18{texcount -tex -sum \jobname.tex > /tmp/wordcount.tex}
\begin{document}
\section[Count-me]{Count me}
This section is counted.  The quick brown fox jumps over the lazy dog. 
%TC:ignore  
\section{Ignore me}
A litle ignored text in the count.
%TC:endignore   
\subsection{This is a subsection}
And some text with inline math ($E=mc^2$).
\subsection{And this is another subsection}
And some more text with display math \[E=mc^2\]
\subsection{One more subsection with a float}
This part of the text include  one figure float. 
\begin{figure}[h!]
\centering\rule{1cm}{1cm}\caption{A nice black box.}
\end{figure}
%TC:ignore  
\subsubsection*{Counts of words} 
\verbatiminput{/tmp/wordcount}
%TC:endignore   
\end{document}

To count characters instead of words, add simply the -char option after texcount:

mw2

Fran
  • 80,769
  • 1
    I need to count characters, not words. – aaragon Nov 11 '15 at 13:46
  • @aaragon Simply add -char to the texcount options. – Fran Nov 11 '15 at 13:52
  • I'm using your method to count the characters, but I don't get the same result as Word (which gives me 2007 chars with spaces). Your approach gives me 1915. Any idea on where are the differences? – aaragon Nov 15 '15 at 11:25
  • 1
    @aaragon. TeXcount exclude the spaces. This is explained in the documentation. – Fran Nov 15 '15 at 15:43
  • @Fran I have been looking for a solution of similar problem. Limitation of texcount is that it not only excludes spaces, but also punctuations (e.g. ? , . - etc). Spaces can be counted as number of words. But how should I account for these punctuations? Any idea? – Ankush Sep 20 '16 at 16:49
  • @Ankush In simple documents, you can use detex + wc to count all characters. The difference with the number of characters in `texcount must be the number of punctuations. – Fran Sep 20 '16 at 23:33