将正则表达式传递给引擎以在 pdf 输出中产生突出显示

将正则表达式传递给引擎以在 pdf 输出中产生突出显示

有没有办法将正则表达式传递给引擎(pdflatexxelatex...我不知道什么是正确的选择)以在 pdf 输出中突出显示匹配项?我需要.tex不修改文件。

让我解释一下。如果我有这个文件:

\documentclass[11pt]{article}
\usepackage{amsmath}
\pagestyle{empty}
\begin{document}

The words foo and bar should be higlighted in the pdf output of this
document, but I need this file not being modified.

An example for a more complex string, e.g. ``highlight this string'',
would be helpfull.

\end{document}

我想要类似的东西(这是一个概念性的例子):

pdflatex --highlight "\(foo\|bar\)" file.tex

并且有:

在此处输入图片描述

请注意,我只熟悉 emacs 正则表达式,因此请指定您使用的正则表达式类型。

另一个有用的事情是如果我可以将正则表达式存储在要传递给引擎的文件中。

答案1

Lua 提供了各种模式匹配功能。它不是“完整”的正则表达式,但非常相似。LuaTeX 允许您将函数分配给process_input_buffer,它们可以在输入流上“动态”充当预处理器,TeX 开始其常规处理。

我建议您创建一个单独的 tex 文件,例如,highlight.tex如下所示:

\AtBeginDocument{%
\usepackage{xcolor}  % for '\textcolor' macro
\usepackage{luacode} % for '\luaexec' macro
\luaexec{
function colorize ( u )
   return ( "\\textcolor{red}{" .. u .. "}" )
end
function highlight ( s )
   s = s:gsub ( "foo" , colorize )
   s = s:gsub ( 'bar' , colorize )
   s = s:gsub ( "``highlight this string''" , colorize )
   return s
end
luatexbase.add_to_callback ( "process_input_buffer" , highlight , "highlight" ) 
}}

然后,您需要对“主”tex 文件进行的唯一修改是\input highlight在序言中插入指令。

\documentclass[11pt]{article}
\input highlight  % <-- new

\begin{document}
The words foo and bar should be highlighted in the pdf output of this
document, but I need this file not being modified.

An example for a more complex string, e.g., ``highlight this string'',
would be helpful.
\end{document}

在此处输入图片描述

相关内容