The following is a proof-of-concept to count conditionally on the number of words in a macro argument, in this case \caption
. It requires modifying the source of texcount
, which is written in Perl.
There is a subroutine inc_count
(around line 2800 in texcount
3.1.1) that counts words one by one. The line that actually increases the count is
${$count->{'counts'}}[$cnt]+=$value;
with the variable $cnt
representing the type of count. Captions are type 3.
You can add some checks to this function to count caption words only if the total number in that caption reaches a predefined threshold. To do this you need to use a variable to keep track of the current total of words in a caption, and another variable to keep track of what the type of the previous word was. The possible situations that the check needs to account for are:
- The type is not a caption. Then, reset the caption word count and count the word normally for its type.
- Optionally, check if the previous word was in a caption, if yes, then that caption has now ended, and the number of words can be printed.
- The type is a caption. Then, check if the previous word was also a caption.
- If not, then a new caption has started. Set temporary count to one, do not store the count yet.
- If the previous word was a caption, then check if the threshold has been reached.
- If not, then increase the count, but don't store the count yet.
- If the count is at the threshold, then store the current count.
- If the count is over the threshold, then store an additional count of one.
MWE:
my $prev_type;
my $capt_count;
# Increment TeX count for a given count type
sub inc_count {
my ($tex,$cnt,$value)=@_;
my $count=$tex->{'subcount'};
if (!defined $value) {$value=1;}
if (!defined $prev_type) {$prev_type = -1;}
if (!defined $capt_count) {$capt_count = 0;}
my $CAPT_THRESHOLD = 3;
if ($cnt != 3){ # not a caption, reset and count as normal
if($prev_type == 3){ # previous word in caption, which has now ended
print "Caption: $capt_count words\n";
}
$capt_count = 0; # reset
$prev_type = $cnt;
${$count->{'counts'}}[$cnt]+=$value; # count as normal
}else{ # in a caption
if ($prev_type == 3) { # check if previous word was also in a caption
$capt_count++;
if ($capt_count == $CAPT_THRESHOLD) { # at threshold
${$count->{'counts'}}[$cnt]+=$capt_count; # count previously ignored words
}
if ($capt_count > $CAPT_THRESHOLD) { # over threshold
${$count->{'counts'}}[$cnt]+=$value; # count one for current word
}
} else { # previous was not a caption, so start of new caption
$prev_type = 3;
$capt_count = 1;
}
}
}
LaTeX document:
\documentclass{article}
\begin{document}
Some normal text
\begin{figure}
text in figure
\caption{short caption}
\end{figure}
\begin{figure}
text in figure
\caption{one two three four five}
\end{figure}
more text
\end{document}
texcount
output (threshold = 3, so the first caption is not counted but the second caption is counted):
Caption: 2 words
Caption: 5 words
File: captioncount.tex
Encoding: ascii
Words in text: 5
Words in headers: 0
Words outside text (captions, etc.): 5
Number of headers: 0
Number of floats/tables/figures: 2
Number of math inlines: 0
Number of math displayed: 0
Note that the added Perl code is not very clean or conforming to best practise, and may produce incorrect results - it is a proof of concept only.