我想将所有全大写单词、缩写词等转换为小写,最好是自动实时转换,而不更改源文件。理想的解决方案还允许我使用黑名单和/或单独标记来阻止其中一些替换。
据我所知,这无法仅靠 LaTeX 来实现。
由于不了解 Lua,我尝试采用以下解决方案:宏:替换所有出现的单词类似于http://lua-users.org/wiki/FrontierPattern我可以匹配正确的单词。但由于替换适用于整个 tex 文件,包括关键字和命令,因此排版被破坏了。
有没有办法将 Lua 的字符串替换仅应用于输入 -文本仅此而已?
作为替代方案,我查看了xesearch
-package,它仅处理输入的正确部分。但据我所知,它的“(非常直白的)正则表达式”似乎不允许搜索全大写的单词。
答案1
这只使用 pdflatex。
答案已被编辑,以筛选出(catcode 12)“标点符号”来辨别一个单词是否是“全大写”。
已重新编辑以接受多段参数。已编辑以在段落在环境中结束时正常工作\]
。已编辑以增强范围遏制功能,如下所示:
使用
{...}
将提供范围限制,但组中的任何 CAPS 单词将不会改为小写字母;使用
\bgroup...\egroup{}
将提供范围限制,以及组中的任何大写单词将要改为小写字母。
对范围限制的修复{...}
是通过在每个单词的开头添加\junkchar
,然后在排版之前将其删除来实现的。
当前解决方案的优点在于宏和内联数学不会干扰算法。
但是,仍然(至少)存在一个问题:与所有宏一样,\verb
不能成为参数的一部分。
\documentclass{article}
\usepackage{stringstrings,ifthen}
\makeatletter\let\Gobble\@gobble\makeatother
\def\junkchar{+}% MUST BE ANY catcode 11 OR 12 character
% TESTS IF WORD IS ALL CAPITAL LETTERS (catcode 12 PUNCTUATION IS SCREENED)
\def\testcaps#1{%
\def\capword{T}\testeleven#1\relax\relax%
\if T\capword\caseupper[q]{#1}%
\ifthenelse{\equal{#1}{\thestring}}{}{\def\capword{F}}%
\fi}
% TESTS IF ALL LETTERS OF WORD ARE \catcode 11 or \catcode 12, THEN \capword STAYS {T}
\def\testeleven#1#2\relax{%
\ifcat\noexpand#1A\ifx\relax#2\relax\else\testeleven#2\relax\fi\else%
\ifcat\noexpand#10\ifx\relax#2\relax\else\testeleven#2\relax\fi\else%
\def\capword{F}% \fi%
\fi%
\fi}
% CONVERTS CAP WORDS OF ARGUMENT INTO SMALL CAPS (\par ALLOWED)
\long\def\sccaps#1{\testcappars#1\par\relax\relax}
% PARSES \sccaps ARGUMENT INTO PARAGRAPHS AND INVOKES \testcapwords ON EACH PARAGRAPH
\long\def\testcappars#1\par#2\relax{%
\testcapwords\junkchar#1 \relax\relax%
\ifx\relax#2\else\unskip\par\testcappars#2\relax\fi}
% PARSES PARAGRAPH INTO WORDS; TESTS EACH WORD; IF ALL CAPS, MAKES IT SMALL CAPS
\def\testcapwords#1 #2\relax{%
\testcaps{#1}\if T\capword\makesc#1\relax\relax\else\Gobble#1\fi%
\ifx\relax#2\else\ \expandafter\testcapwords\junkchar#2\relax\fi}
% PRODUCES SMALL CAPS WORD FROM KNOWN UPPERCASE WORD.
\def\makesc#1#2#3\relax{\textsc{#2\caselower{#3}}}% #1 IS STRIPPED \junkchar
\begin{document}
\sccaps{% CAN'T START ARGUMENT WITH A SPACE
This is a test of the EMERGENCY BROADCAST SYSTEM.
This is ONLY a test. The EBS may be consulted for further \textbf{information}.
What about paragraphs that CONTAIN $Ax^2 + Bx + C$ math data?
\[
y = Ax^2 + Bx + C
\]
or EVEN displaystyle math?
And NOW for a second paragraph at 9:30 PM.
Testing for \bgroup\tiny group LIMITING behavior\egroup{}, but you
must use bgroup and egroup as the containment if you want CAPS in the group
to be made small caps.
Using braces, {\tiny group LIMITING behavior}, provides group
containment, but CAP words in the group, like LIMITING, are not small-capped.
}
\end{document}