0

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
mriisa
  • 540
  • @cabohah I have now written it as a shell script and updated it in the original post. But it still poses a problem with latex. Would it be possible to just call a .sh file instead of inserting the text manually? – mriisa Jun 02 '23 at 13:06
  • I believe this should work out of the box, but escaping the % character may be a bit difficult, so just put it in a separate .sh file and use ShellEscape which should work -- if it doesn't then you need to make a MWE somehow. Try also 2> error.txt then \VerbatimInput{error.txt} (or just download error.txt within 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
  • 1
    @cabohah there is nothing overleaf specfic about this question – David Carlisle Jun 02 '23 at 20:45
  • you have defined \quickcharcount to have 1 argument you don't use??? – David Carlisle Jun 02 '23 at 20:51
  • @cabohah Unless the question e.g. shows only an Overleaf link and not a MWE inside the question itself, it should be suitable for the site, just like how questions about TeXStudio or any other editor is. – user202729 Jun 03 '23 at 02:40
  • @cabohah The OP provided a bash version with the same (no line ends) issue. The original version with just bat/cmd could be said to be linux specfic, but even that wasn't overleaf specific, you would have the same error in any linux tex. I tested my answer on a local installation (on this windows laptop) not overleaf – David Carlisle Jun 03 '23 at 10:13
  • @DavidCarlisle So because you think, that trying to use a bat with overlead is not overleaf specific, I've removed my corresponding comments. But IMHO in this case, the overleaf tag should also be removed. And IMHO in this case, the question is a duplicate of https://tex.stackexchange.com/questions/17462/write18-pass-through-to-shell and still should be closed. – cabohah Jun 03 '23 at 10:22
  • % 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

1 Answers1

1

enter image description here

You have defined \quickcharcount to have 1 argument you don't use so I defined it with no arguments.

A newline in the source is a space so all your script is on one line. You could use ^^J to write a newline but here I just use the bash command separator ; You also need \@percentchar as % is a comment.

\documentclass{article}

\makeatletter \newcommand{\quickcharcount}{%no argument \immediate\write18{ char=$(texcount -1 -sum -merge -char -q main.tex); space=$(texcount -1 -sum -merge -q main.tex); nChar=${char@percentchar(}; nSpace=${space@percentchar(}; sum=$((nChar + nSpace)); echo $sum > count.sum }% \input{count.sum}% } \makeatother

\begin{document} abc xyz

\quickcharcount \end{document}

David Carlisle
  • 757,742
  • It works! But just to clarify, the reason there was a unused argument was that it was used to input the name of the "main" tex file. So it would be "#1.tex". I had just removed it during troubleshooting. – mriisa Jun 03 '23 at 10:50