Debian Codegraph @

texmf-dist/source/lualatex/countwords/countwords.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2026 by Hugo Heagren <[email protected]>
4 % ---------------------------------------------------------------------------
5 % This work may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either version 1.3c
7 % of this license or (at your option) any later version.
8 % The latest version of this license is in
9 % http://www.latex-project.org/lppl.txt
10 % and version 1.3c or later is part of all distributions of LaTeX
11 % version 2008-05-04 or later.
12 %
13 % This work has the LPPL maintenance status `maintained'.
14 %
15 % The Current Maintainer of this work is Hugo Heagren.
16 %
17 % This work consists of the files countwords.dtx and countwords.ins
18 % and the derived filebase countwords.sty.
19 %
20 % \fi
21 %
22 % \iffalse
23 %<*driver>
24 \ProvidesFile{countwords.dtx}
25 %</driver>
26 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
27 %<package>\ProvidesPackage{countwords}
28 %<*package>
29 [2026/04/14 1.0.1 Exact word counts]
30 %</package>
31 %
32 %<*driver>
33 \documentclass{ltxdoc}
34
35 \usepackage[T1]{fontenc}
36 \usepackage{listings}
37 \usepackage{xcolor}
38
39 % ^^A https://tex.stackexchange.com/a/57172/238079
40 \usepackage{accsupp}
41 \newcommand{\emptyaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
42
43 \lstset{
44 basicstyle=\MacroFont,
45 columns=fullflexible,
46 keepspaces=true,
47 numbers=left,
48 xleftmargin=5.0ex,
49 numberstyle=\scriptsize\emptyaccsupp,
50 language={[LaTeX]TeX},
51 keywordstyle=\color{blue},
52 commentstyle=\color{gray},
53 morekeywords={
54 maketitle,
55 countwordsstart,
56 countwordsstop
57 },
58 }
59
60 \usepackage{hyperref}
61
62 \usepackage[date=year]{biblatex}
63 \addbibresource{./references.bib}
64
65 \EnableCrossrefs
66 \CodelineNumbered
67 \RecordChanges
68 \begin{document}
69 \DocInput{countwords.dtx}
70 \PrintChanges
71 \PrintIndex
72 \end{document}
73 %</driver>
74 % \fi
75 %
76 % \CheckSum{17}
77 %
78 % \CharacterTable
79 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
80 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
81 % Digits \0\1\2\3\4\5\6\7\8\9
82 % Exclamation \! Double quote \" Hash (number) \#
83 % Dollar \$ Percent \% Ampersand \&
84 % Acute accent \' Left paren \( Right paren \)
85 % Asterisk \* Plus \+ Comma \,
86 % Minus \- Point \. Solidus \/
87 % Colon \: Semicolon \; Less than \<
88 % Equals \= Greater than \> Question mark \?
89 % Commercial at \@ Left bracket \[ Backslash \\
90 % Right bracket \] Circumflex \^ Underscore \_
91 % Grave accent \` Left brace \{ Vertical bar \|
92 % Right brace \} Tilde \~}
93 %
94 %
95 % \changes{<+version+>}{<+date+>}{Converted to DTX file}
96 %
97 % \DoNotIndex{\newcommand,\newenvironment}
98 %
99 % \GetFileInfo{countwords.dtx}
100 % \title{The \textsf{countwords} package}
101
102 % \author{Hugo Heagren\thanks{The bulk of the actual code is in lua
103 % and was written by Philipp Gesang \cite{gesang12_wordcount}.} \\
104 % \texttt{\href{mailto:[email protected]}{[email protected]}}}
105
106 % \date{\fileversion~from \filedate}
107 %
108 % \maketitle
109 %
110 % \section{Introduction}
111 %
112 % This is a simple package for generating \emph{exact} wordcounts in
113 % \LaTeX{} projects. It counts words, storing the value in a standard
114 % \LaTeX{} counter, which can then be printed in the normal way. Word
115 % counting can be paused and resumed, so you can skip certain
116 % environments like mathematics, and multiple word counts can happen
117 % in parallel (you could keep separate global and per-chapter counts,
118 % for example).
119 %
120 % A note on \textsf{TeXcount} \cite{rødland_texcount}.
121 % \textsf{TeXcount} is a popular way of counting words in \LaTeX{}
122 % projects. It is an external script, which is run \emph{on} the
123 % \LaTeX{} file, and produces an estimate of the number of words in
124 % the final document. You can't use \textsf{TeXcount} to print a
125 % wordcount for a document within that very document.\footnote{Well
126 % you \emph{can} with some jiggerypokery, but it isn't pretty, and
127 % this isn't what \textsf{TeXcount} was designed to do.}
128 % \textsf{countwords} is a \LaTeX{} package which works \emph{within}
129 % the document. \textsf{countwords} also prints \emph{exact} word
130 % counts, because it counts the number of printed characters in the
131 % final document as they are rendered; \textsf{TeXcount} prints a
132 % reasonable estimate.
133 %
134 % \section{Usage}
135 %
136 % \textsf{countwords} requires Lua\LaTeX. Load as usual:
137 %
138 % \lstinputlisting{./countwords-load-example.tex}
139 %
140 % The package has one option, \texttt{threshold}, set with
141 % \texttt{threshold=\(\langle{}number\rangle\)} This is the minimum number of
142 % characters that a string must contain to count as a single word. So
143 % setting it to 1 means that `I' and `a' count as words (as well as
144 % anything longer); setting it to 3 means that `and' counts as a word,
145 % but not `I', `a' or `as'. If unspecified it defaults to 1 (which is
146 % usually the most sensible for prose writing).
147 %
148 % \textsf{countwords} stores all wordcounts in standard \LaTeX
149 % counters, and does not create or manage these itself. So you must
150 % create a counter to store the wordcount in:
151 %
152 % \lstinputlisting[firstnumber=last]{./countwords-counter-example.tex}
153 %
154 % \DescribeMacro{\countwordsstart\{\(\langle{}counter\rangle{}\)\}}
155 % Issue this command to start counting words into
156 % \(\langle{}counter\rangle{}\). After this, whenever \LaTeX{} encounters a string
157 % of consecutive characters of longer than or equal to the threshold,
158 % the \(\langle{}counter\rangle{}\) will increment by one.
159 %
160 % \DescribeMacro{\countwordsstop\{\(\langle{}counter\rangle{}\)\}}
161 % Issue this command to stop counting words in
162 % \(\langle{}counter\rangle{}\). After this, \textsf{countwords} will not alter the
163 % value of \(\langle{}counter\rangle{}\).
164 %
165 % \section{Examples}
166 % \subsection{Simple example}
167 % \lstinputlisting{./countwords-simple-example.tex}
168 %
169 % \subsection{Printing count at the beginning}
170 %
171 % The \textsf{totcount} package \cite{koutavas_totcount} can be used
172 % to print the eventual total of a counter before that total is
173 % reached.
174 %
175 % \lstinputlisting{./countwords-totcount-example.tex}
176 %
177 % \subsection{Skipping maths}
178 %
179 % By starting and stopping the wordcount, things like display
180 % mathematics can be skipped. Similar techniques could automatically
181 % skip footnotes or citations.
182 % \lstinputlisting{./countwords-maths-example.tex}
183 %
184 % \StopEventually{}
185 %
186 % \section{Implementation}
187 % \iffalse
188 %<*package>
189 % \fi
190 %
191 % \begin{macrocode}
192 \RequirePackage{luatexbase}
193 \RequirePackage{luapackageloader}
194 % \end{macrocode}
195 % \subsection{Lua}
196 % \begin{macrocode}
197 \directlua{countwords = require("countwords")}
198 % \end{macrocode}
199 % \subsection{Options}
200 %
201 % \begin{macrocode}
202 \DeclareKeys
203 {
204 threshold.code = \directlua{ countwords.set_threshold(\number#1) },
205 }
206 \SetKeys{threshold=1}
207 \DeclareUnknownKeyHandler{\PackageWarning{countwords}{Unknown option `#1'}}
208 \ProcessKeyOptions
209 % \end{macrocode}
210 % \subsection{Add callback}
211 % \begin{macrocode}
212 \directlua{
213 luatexbase.add_to_callback(
214 "pre_linebreak_filter",
215 countwords.callback,
216 "wordcount"
217 )
218 }%
219 % \end{macrocode}
220 % \subsection{Document commands}
221 %
222 %
223 % \begin{macro}{\countwordsstart}
224 % \begin{macrocode}
225 \def\countwordsstart#1{%
226 \directlua{ countwords.enable_counter("#1") }
227 }
228 % \end{macrocode}
229 % \end{macro}
230 %
231 %
232 % \begin{macro}{\countwordsstop}
233 % \begin{macrocode}
234 \def\countwordsstop#1{%
235 \directlua{ countwords.disable_counter("#1") }
236 }
237 % \end{macrocode}
238 % \end{macro}
239 %
240
241 %
242 % \printbibliography
243 %
244 % \iffalse
245 %</package>
246 % \fi
247 %
248 % \Finale
249 \endinput
250
251 % \endinput
252 % Local Variables:
253 % mode: docTeX
254 % TeX-master: t
255 % End: