I often transfer files between myself and someone with little knowledge of LaTeX, for editing purposes. So far, I have managed to get most TeX-specific oddities under control (thanks to utf-8 encoding). But I am stymied by the percent symbol %.
What I wish to do is detect whenever an un-escaped % is used in the document body (OK in Preamble). If it is used, then its presence is counted. At the end of the document, I can print a message in the log file, if the count exceeds zero.
This MWE code does the counting part (I omitted the message):
\documentclass{article} % Compile using lualatex.
\newcounter{pctcount}
\begin{document}
\begingroup
\lccode`\~="0025
\lowercase{\endgroup\gdef~{\stepcounter{pctcount}\%}}
\catcode`\%=13
This line % has an unescaped percent.\par
And also % here.\par
pctcount=\arabic{pctcount}\par
\end{document}
Essentially, it changes % from a comment character, to a macro. My problem: I would also like it to retain its capability as a comment character, so that it both increments the counter, and works as a comment.
I have tried various magic tricks such as using \catcode to temporarily
assign comment code 14 to an otherwise-unused character (such as the generic currency symbol). No joy, except distant demonic laughter if I randomly type too much code.
Is this possible in lualatex? Lua code acceptable.
What I am now doing, is scanning the *tex files via BASH script.
\%(while leaving all real comments unchanged) then maybe an in-editor solution would be better, where you search within the editor interface (with regular expressions) for all % not preceded by . – Marijn Sep 13 '23 at 11:03