根据字典自动突出显示文本

根据字典自动突出显示文本

我想根据单词是否在预定义词典中来自动突出显示正在运行的文本的某些部分。类似于...

\documentclass{article}
\mydictionary{apple,banana}%A comma-separated list of unicode characters. For simplicity the dictionary contains words, and not compound expressions, such as "yellow lemon". In other words, no spaces are allowed.
\def\autohighlightStyle1{ textcolor=red }{ dictionary=\mydictionary }%I need the infrastructure to define my own formatting. For simplicity, in this example it is enough to change the textcolor. I will add my own formatting options later by myself.
\begin{document}
\autohighlightStyle1{Monkeys like banana, but they don't like apple.}
\end{document}

结果将是一个文本,其中来自字典\mydictionary(香蕉和苹果)的单词根据某些说明进行格式化,这里为简单起见,它们被涂成红色。

这样的事有可能实现吗?本质上,我需要通过空格对文本进行标记,然后逐字将其与字典的内容进行比较,然后应用样式,然后将其打印出来。

我正在使用 xelatex 和 unicode 文本(但同样,在这个简单的示例中,空格起到分隔符的作用)。

理想情况下,我希望将此包装器应用于整个文档,紧接着应用\begin{document}并在紧接着应用之前关闭\end{document},因此...它应该是...强大的...但是...为了简单起见,也许我们可以假设它接收普通文本作为参数。另一个想法是,也许我必须使用样式文件来做这样的事情?

答案1

||使用宏定义分隔的字典单词\setsepchar。然后,在 之后\begin{document},发行\def\currentword{}\tokencyclexpress 和 在 之前\end{document},发行\endtokencyclexpress。这将设置一个环境,其中首先筛选文档的所有标记以搜索字典单词。如果找到,则在执行标记列表之前将突出显示添加到输入标记流中。

请注意,如果单词完全匹配,则它会采用一种突出显示样式,如果它仅包含字典单词(例如复数),则会采用不同的突出显示样式。

精确匹配的突出显示格式是用 设置的\newcommand\autohighlightStyleA{\textcolor{red}}

“包含”匹配项的突出显示格式是用 设置的\newcommand\autohighlightStyleB{\textcolor{blue}}

如果字典中的单词跨越了组边界,例如ap\textbf{ple},它将不会被识别为突出显示。

我应该补充一点,该方法可以找到嵌入在诸如 之类的组中的字典单词\textit{banana},如果没有 ,这实际上是一件很难实现的事情tokcycle

\documentclass{article}
\usepackage{tokcycle,listofitems,xcolor}
\setsepchar{apple||banana}
\newcommand\testdict{%
  \if\relax\detokenize\expandafter{\currentword}\relax\else
    {\ignoreemptyitems
      \greadlist\dictcompA{\currentword}}%
    \readlist\dictcompB{\currentword}%
    \ifnum\listlen\dictcompA[]=0\relax
      \addcytoks[1]{\autohighlightStyleA}%
      \addcytoks[1]{\expandafter{\currentword}}
    \else
      \ifnum\listlen\dictcompB[]>1\relax
        \addcytoks[1]{\autohighlightStyleB}%
        \addcytoks[1]{\expandafter{\currentword}}
      \else
        \addcytoks[1]{\currentword}%
      \fi
    \fi
  \fi
  \gdef\currentword{}%
}
\makeatletter
\Characterdirective{\tctestifcatnx A#1{\g@addto@macro\currentword{#1}}
  {\testdict\addcytoks{#1}}}
\stripgroupingtrue
\Groupdirective{\testdict\groupedcytoks{\processtoks{#1}\testdict}}
\Macrodirective{\g@addto@macro\currentword{#1}}
\Spacedirective{\testdict\addcytoks{#1}}
\makeatother
\newcommand\autohighlightStyleA{\textcolor{red}}
\newcommand\autohighlightStyleB{\textcolor{blue}}
\begin{document}
\def\currentword{}\tokencyclexpress
\tableofcontents

\listoffigures

\section{My Section about a banana and two apples}

Monkeys like banana, but they don't like apple.

Monkeys like \textit{banana}, but \textit{they} don't like ap\textbf{ple}.

Monkeys \textit{like bananananas,} but \textit{they} don't like \textbf{apple}.
\begin{equation}
  \textrm{banana} = 2^\mathrm{apples}
\end{equation}
\subsection{Relevant figures about a banana}
\begin{figure}[ht]
\centering
\fbox{apple and banana}
\caption{A boxed version of ``apple and banana''}
\end{figure}
\endtokencyclexpress
\end{document}

在此处输入图片描述

ps 这适用于 pdflatex、xelatex 和 lualatex。

相关内容