6

I search for a package or a script, that can count the number of characters including spaces in my document. I have tried to search for it, and I have found many solutions, that should do this, but I can't get it to work. I'm using Miktex and Texmaker.

\documentclass{report}
\begin{document}
\input{A_File} % This is a file that can contain everything, eq. figures, equations, minpages etc. To make it simple , let 's say that it contains the following text and equation
This is an example
\begin{equation}
2+2=4 \text{A text}
\end{equation}

\end{document}

So the file A_File contains 'This is an example' and 'A text' which should be counted. I now want to count the the number of characters with spaces in my document. As it can be seen there are 4 characters in 'This', 2 in 'is', 2 in 'an', 7 in 'example' and 3 spaces. Furthermore there are 5 characters in ' A text' with 1 space. This gives 24 number of characters including spaces.

The script should then display:

Total number of characters including spaces = 24.

I'm not so good at latex, so I just want a script I can copy and insert, and then it works.

Morten
  • 309
  • 3
  • 10
  • Welcome to TeX.SX! There's texcount, an external (perl?) script. It's easy to run it under Linux/MacOS, but windows? –  Dec 28 '15 at 18:57
  • I use windows. How does this 'perl' works. Is it something in Texmaker ? Can you perhaps give an minimal example of the use ? – Morten Dec 28 '15 at 19:02
  • perl is a powerful scripting language and the perl binaries must be installed of course to run other perl scripts. I don't know whether texcount is shipped with MikTeX at all, I don't use that stuff –  Dec 28 '15 at 19:06
  • 1
  • Maybe related: http://tex.stackexchange.com/questions/534/is-there-any-way-to-do-a-correct-word-count-of-a-latex-document – Rico Dec 28 '15 at 19:34
  • 1
    TeXcount may be asked to count the number of word or characters, but not including spaces. Count spaces in LaTeX source is rather problematic. How many spaces are in "x<space><space><space><space><space>x" ? One or five? And how many spaces are "x\hspace{5em}x"? And how to now if the space in "this<space>space" will be lost in the pdf, due to a linebreak? – Fran Dec 28 '15 at 21:05
  • @Fran IMHO a space is (a space) inter-words even if more then (well one space). here (x x) one space. – touhami Dec 28 '15 at 21:26
  • @touhami In word processors you type 5 spaces and you get 5 (or more) spaces (equivalent to \space\space\space\space\space). In a LaTeX with the same 5 spaces you get 1 \space (or more). If the final space printed is what matter, how we should consider superfluous spaces, \quad or other spacing commands? How count the extra space added/ removed to justify the margins and break lines in LateX, Word or whatever? ... If that do not matter, why on earth do the counting? – Fran Dec 29 '15 at 01:21
  • How should This is a unique function $f$ such that $blah math$ holds be counted? Also, do you want a nondestructive way to do this? As in, should it also produce a pdf that contains the normal output from compiling the file? Or are you okay with it only producing a word count? – Hood Chatham Dec 29 '15 at 03:46
  • @Fran IMHO a space is blanck betwen two words (if it's one or five or \quad or \hfill, it's one) my idea is if in a paragraph we count 100 words there is 99 space. but may be it's not so easy. – touhami Dec 29 '15 at 06:49
  • #Alenanno #Rico

    Yes, I have seen all those solutions, but I can't get them to work. I espacially like this perl thing, but it doesn't work for me. Can you perhaps give a more detail explanation how to get the perl script working ?

    – Morten Dec 29 '15 at 11:46
  • @touhami To make a pure TeX solution to this, it would be necessary to make the catcode of space into "other", and then the easiest thing to do would be to count all spaces. To count only relevant spaces, I would set up a two-state state machine that shifts to state "space" when it sees a space, and in state space doesn't count further space characters. – Hood Chatham Dec 29 '15 at 15:47
  • @Morten: You don't need Perl to run TexCount, you can use the webinterface: http://app.uio.no/ifi/texcount/online.php Simply paste your tex code or upload the entire *.tex file. Again, it does not count spaces, unfortunately. – NCH32 Sep 12 '16 at 13:15

1 Answers1

2

Without counting spaces, probably TeXcount is the best solution. Otherwise, assuming that additional normal spaces characters in the source code can be safely ignored (but not space commands as \quad, including even \\ and \par ...) one solution that could work reasonably for simple documents (not for child documents and text in math mode, sorry) is convert from .tex to .lyx using:

  • LyX menu : File > ImportFile > LaTeX (plain) ...

  • tex2lyx directly (e.g, at the system prompt type tex2lyx -c report test.tex test.lyx, and next lyx test.lyx).

Then, simply ask for the statistics in LyX Tools > Statitistics...

Example:

Source code test.tex:

\documentclass{report}
\begin{document}
This is an example.
This is a file that can contain everything, eq. figures, equations, minpages etc. To make it simple , let 's say that it contains the following text and equation
\begin{equation}
2+2=4 \text{A text}
\end{equation}
\end{document}

Result:

mwe

Fran
  • 80,769