I am trying to write a script that using texcount will output only the number of characters including spaces. I made a batch script which works, no problems there.
The problem is trying to run it in latex shell escape in overleaf using the \immediate\write18 environment. The problems seems mostly to be with how latex handles the special characters used in the batchscript. The script/latex code is as follows:
\newcommand{\quickcharcount}[1]{%
\immediate\write18{
texcount -1 -sum -merge -char -q main.tex > chars.txt
texcount -1 -sum -merge -q main.tex > spaces.txt
for /f "skip=1" %%G IN (spaces.txt) DO if not defined line set "nSpace=%%G"
for /f "skip=1" %%G IN (chars.txt) DO if not defined line set "nChar=%%G"
for /f "tokens=1*delims=:" %%G in ('findstr /n "^" spaces.txt') do if %%G equ 2 set "nSpace=%%H"
for /f "tokens=1*delims=:" %%G in ('findstr /n "^" chars.txt') do if %%G equ 2 set "nChar=%%H"
set /a "sum=nChar+nSpace"
echo %sum% > count.sum
}%
\input{count.sum}
}
Does anyone have experience with this, and know how to solve the problem?
EDIT:
I was told it was not possible with a bat script, so I have now written it as a shell script. But I am still struggling with getting it to work inside latex. You said it was somewhat restrictive with overleaf, so could this also be the reason here?
#!/bin/bash
char=$(texcount -1 -sum -merge -char -q main.tex)
space=$(texcount -1 -sum -merge -q main.tex)
nChar=${char%(*}
nSpace=${space%(*}
sum=$((nChar + nSpace))
echo $sum > count.sum

%character may be a bit difficult, so just put it in a separate.shfile and useShellEscapewhich should work -- if it doesn't then you need to make a MWE somehow. Try also2> error.txtthen\VerbatimInput{error.txt}(or just downloaderror.txtwithin the generated files) to see if it prints any error message. // @cabohah as far as I can see https://tex.stackexchange.com/questions/534049/pythontex-not-working-on-current-overleaf-compiler is on-topic here so this question should also be. – user202729 Jun 02 '23 at 13:34\quickcharcountto have 1 argument you don't use??? – David Carlisle Jun 02 '23 at 20:51%is a duplicate but that doesn't cover line ends being written as spaces (but that must be a duplicate of something:-) – David Carlisle Jun 03 '23 at 10:37