TeX - LaTeX Asked on March 26, 2021
Is there a way to pass a regexp to an engine (pdflatex
, xelatex
… I don’t know what could be the right choice) to produce a highlighting of the matches in the pdf output? I need the .tex
file not be modified.
Let me explain. If I have the file:
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}
I’d like to have something like (this is a conceptual example):
pdflatex --highlight "(foo|bar)" file.tex
and have:
Please note that I’m familiar with emacs regexp only so, please, specify what kind of regexp you are using.
Another usefull thing should be if I could store the regexps in a file to be passed to the engine.
Lua provides various pattern-matching capabilities. It's not "full" regexp, but it's quite similar. LuaTeX lets you assign functions to the process_input_buffer
callback, where they can act as preprocessors, "on the fly", on the input stream, before TeX commences its usual processing.
I suggest you create a separate tex file called, say, highlight.tex
, as follows:
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" )
}}
Then the only modification you'd have to apply to the "main" tex file is to insert the instruction input highlight
in the preamble.
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}
Correct answer by Mico on March 26, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP