我想自动突出显示文档中的所有标点符号,最好使用不同的背景颜色,或者通过更改标点符号的颜色。
目的主要是让我检查句子的长度和总体风格,这样我就不会担心等式和/或标题中的点。
有没有一种简单的方法可以让我“重新定义”一个点为 \color{red}{.} 从而将所有点变成红色?
使用下面接受的答案的解决方案(因为我无法在评论中使代码工作)
\usepackage{xcolor}
\usepackage{stringstrings}
\let\svcomma,\let\svperiod.\let\svsemicolon;
\catcode`,=\active\catcode`.=\active \catcode`;=\active %
\newcommand\colorpuncton[1][red]{%
% \catcode`,=\active\def,{\colorbox{#1}{\svcomma}}%
\catcode`.=\active\def.{\colorbox{blue!30}{\svperiod}}%
\catcode`;=\active\def;{\colorbox{green!30}{\svsemicolon}}%
}
\def\colorpunctoff{%
\catcode`,=12\let ,\svcomma%
\catcode`.=12\let .\svperiod%
\catcode`;=12\let ;\svperiod%
}
\catcode`,=12 \catcode`.=12 \catcode`;=12 %
\parskip 1em
\let\oldfigure\figure% Store old figure environment start
\let\endoldfigure\endfigure% Store old figure environment end
\renewenvironment{figure}[1][htbp]% Redefine figure
{\colorpunctoff\oldfigure[#1]}
{\endoldfigure\colorpuncton}
\let\oldtable\table%
\let\endoldtable\endtable%
\renewenvironment{table}[1][htbp]%
{\colorpunctoff\oldtable[#1]}
{\endoldtable\colorpuncton}
\let\oldSI\SI
\renewcommand{\SI}[2]{\colorpunctoff\oldSI{#1}{#2}\colorpuncton}
答案1
根据我的回答更改大写字母的颜色。关闭此功能可能允许在需要时以正常方式使用句号之类的功能。
已编辑,允许简单地暂停/更新方法,如果必须调用其他需要标点符号的宏,例如\cite
。已重新编辑,允许在单个宏调用中暂停/更新,\Xcite{}
。
\documentclass{article}
\usepackage{xcolor}
\usepackage{stringstrings}
\let\svcomma,\let\svperiod.\let\svsemicolon;
\catcode`,=\active \catcode`.=\active \catcode`;=\active %
\newcommand\colorpunctinit[1][red]{\colorpuncton%
\def,{\colorbox{#1}{\svcomma}}%
\def.{\colorbox{#1}{\svperiod}}%
\def;{\colorbox{#1}{\svsemicolon}}%
}
\newcommand\colorpuncton{%
\catcode`,=\active %
\catcode`.=\active %
\catcode`;=\active %
}
\def\colorpunctoff{%
\catcode`,=12 %
\catcode`.=12 %
\catcode`;=12 %
}
\newcommand\coloron[2]{%
\if\svcomma#1\catcode`,=\active\def,{\colorbox{#2}{\svcomma}}\else%
\if\svperiod#1\catcode`.=\active\def.{\colorbox{#2}{\svperiod}}\else%
\if\svsemicolon#1\catcode`;=\active\def;{\colorbox{#2}{\svsemicolon}}\else%
\fi\fi\fi%
}
\newcommand\coloroff[1]{\catcode`#1=12%
\edef\tmp{\detokenize{#1}}%
\caselower[q]{\tmp}%
\expandafter\let\expandafter#1\csname sv\thestring\endcsname%
}
\catcode`,=12 \catcode`.=12 \catcode`;=12 %
\parskip 1em
\newcommand\Xcite{\colorpunctoff\citeX}
\newcommand\citeX[1]{\cite{#1}\colorpuncton}
\begin{document}
\colorpunctinit[cyan]
\verb|\colorpunctinit[color]| will change all punctuation to the specified color,\\
here cyan, and should be done to initialize this process.\\
(\verb|But not commas; for some reason, in \verbatim.|)
\colorpunctoff
\verb|\colorpunctoff| will, indeed, suspend the procedure.
\coloron{,}{red}\coloron{.}{blue!30}\coloron{;}{green!30}
\verb|\coloron{<punctuation>}{color}| will turn an inactive symbol active with the
specified color, done here with red, to ",", blue!30 to the ``.'' and green!30 to the
``;''.
\coloroff{,}
\verb|\coloroff{<punctuation>}|, done here to the comma,
will restore that letter to the original LaTeX
setting; the others remain active.
\coloron{,}{cyan!40}
To change the color an already active symbol, one must first turn the color off;
then set the color anew, as done here to the comma ``,''.
To use \verb|\cite|, one must employ \verb|\Xcite{A, B}|
as in \Xcite{A, B}. The punctuation, it is still colored; yes it is.
\end{document}
答案2
您可以激活句点并为其赋予定义。但您应该知道句点通常是语法(例如数字)的一部分,如果句点不符合语法要求,您可能会得到非常可怕的错误。因此在这种情况下,您必须再次更改 catcode。
\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\catcode`\.=13
\def.{\colorbox{red}{\string.}}
a horse. a horse. a horse.
\catcode`\.=12 %disable the colored period
\begin{tikzpicture}
\draw(0.5,0.5)--(1,1);
\end{tikzpicture}
\catcode`\.=13
a kingdom.
\end{document}