1

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.

4
  • If the goal is to check each unescaped % manually afterwards and correct the ones that should be \% (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
    Commented Sep 13, 2023 at 11:03
  • @Marijn Yes, I have done that. The BASH script is an automated check, essentially does the same thing as an in-editor search. Works, but is not "the TeX way".
    – user287367
    Commented Sep 13, 2023 at 15:21
  • 1
    While most things can be done in (La)TeX one way or another (see the answer below), this kind of task often leads to difficult to understand code that breaks easily (see the answer below). So I would recommend to leave the TeX way and take the Bash way in this case...
    – Marijn
    Commented Sep 13, 2023 at 20:21
  • @Marijn Indeed. Also, there is a moderate chance that the bash code will be understood (or at least, can be run) on other machines, by those who have a minimal knowledge.
    – user287367
    Commented Sep 13, 2023 at 22:44

1 Answer 1

0

You don't need Lua for this, it can be done in plain LaTeX, but it isn't a nice, clean, easy-to-understand method. This is based on this answer, which is about taking the remainder of the line as a parameter. I do recommend reading that answer, as it explains what is going on much better than I can.

The next trick is to re-define '%' to do something, in this case, step the counter.

Note, though, that this entire method seems to fail with a strange error if you use \par to end the paragraph. If you just use a blank line to end the paragraph, it works fine.

\documentclass{article}
\begin{document}

\newcounter{pctcount}


\newcommand*{\newlinecommand}[2]{%
  \newcommand*{#1}{%
    \begingroup%
    \escapechar=`\\%
    \catcode\endlinechar=\active%
    \csname\string#1\endcsname%
  }%
  \begingroup%
  \escapechar=`\\%
  \lccode`\~=\endlinechar%
  \lowercase{%
    \expandafter\endgroup
    \expandafter\def\csname\string#1\endcsname##1~%
  }{\endgroup#2\space}%
}

%%% USAGE:
\newlinecommand{\emphline}{\emph{#1}}

\newlinecommand{\incrementpcount}{\stepcounter{pctcount}}

First words \emphline rest of line
some more text

% this would fail if it was after 'catcode' \par

\catcode`\%=13
\def%{\incrementpcount}


This line % has an unescaped percent.

And also % here.

It is the percent sign which is a problem, though\par
This is a new paragraph

I want a literal percent here: \% yay, percent sign

pctcount=\arabic{pctcount}

\end{document}
2
  • I will have a look at that. Will respond again, later today.
    – user287367
    Commented Sep 13, 2023 at 15:22
  • Yes, that get me going in the right direction. As you noted, problem if \par. I also looked at the linked explanation. Yikes!
    – user287367
    Commented Sep 13, 2023 at 20:08

You must log in to answer this question.