I'd like to turn all all-uppercase words, acronyms etc. into small caps, preferably automatically on the fly and without changing the source files. The ideal solution would also allow me to block some of these substitutions out with a blacklist and/or individual mark-up.
In my understanding this cannot be done with LaTeX alone.
Not knowing Lua, I tried to adapt the solution in Macro: Replace all occurrences of a word with something along the lines of http://lua-users.org/wiki/FrontierPattern which I can get to match the right words. But as the replacement applies to the whole tex-file, including keywords and commands, the typesetting is broken.
Is there a way to apply Lua's string-replacement only to the input-text and nothing else?
As an alternative I had a look at the xesearch
-package, which processes the right part of the input only. But its "(very blunt form of) regular expressions" doesn't seem to allow to search for all-caps words, as far as I can see.
Best Answer
This uses just pdflatex.
Answer has been EDITED to screen out (catcode 12) "punctuation" in discerning if a word is "all caps".
REEDITED to take multi-paragraph arguments. EDITED to work properly when paragraph ends on a
\]
environment. EDITED to enhance scope containment features as follows:Use of
{...}
will provide scope containment, but any CAPS word in the group will not be made into small caps;Use of
\bgroup...\egroup{}
will provide scope containment, and any CAPS word in the group will be made into small caps.This fix to scope containment for
{...}
was accomplished by adding a\junkchar
at the beginning of every word, and then stripping it out prior to typesetting.What is good about the current solution is that macros and inline math do not disturb the algorithm.
However, there is still (at least) one issue: as with all macros,
\verb
cannot be part of the argument.