(Updated the answer after OP indicated that he/she uses the book
document class and may have instances of search strings in the arguments of \chapter
and \section
.)
Here's a LuaLaTeX-based solution. In addition to the Lua fuction, named colorize
, that does the colorizing work, the code also sets up two TeX-side macros, \colorizeOn
and \colorizeOff
. As their names suggest, the macros serve to switch the operation of the Lua function on and off.
Some comments on the TeX-side code:
If some of your search strings may occur in the arguments of \chapter
and \section
instructions, you'll need to provide all-uppercase aliases for the colors you'll be using. For instance, if the applicable colors are named red
, blue
, and orange
, use \colorlet
instructions to set up the aliased names RED
, BLUE
, and ORANGE
.
As far as I can tell, there is no good reason or valid excuse for letting search strings occur in the arguments of \label
and \ref
.
Some comments on the Lua-side code:
\
(a single backslash) is a special character in Lua. To generate a backslash in a luacode
environment, one must write \\
.
The search and replacement patterns in the gsub
function must be delimited by matching single ('
) or double ("
) quotes. If the search patterns contain quote characters of the same type as are used for the delimiters, the quotes inside the search pattern must be escaped by prefixing them with a \
. (This isn't the case in the code below: there are two instances of '
in the first search string, but they need not be escaped as "
is being used to delimit the search pattern.)
The characters .
and )
that occur in the first search string have a special, so-called "magical", meaning in Lua if they occur in a search string. To treat them as ordinary characters, they must be escaped with a %
character.
The following are Lua's "magical" characters: (
)
.
%
+
-
*
?
[
^
$
. E.g., to search for a %
symbol, write it as %%
in the search string.
No escaping of these characters is needed in the replacement string (the third argument of gsub
), as they have no "magical" quality in the replacement string.
The %0
term in the third argument of gsub
represents the entire string that was found by the pattern match.



% !TEX TS-program = lualatex
\documentclass{book}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman} % choose various fonts, as needed
\usepackage{xcolor} % for "\textcolor" and "\colorlet" macros
\colorlet{RED}{red} % alias names for colors: use all-uppercase
\colorlet{BLUE}{blue}
\colorlet{ORANGE}{orange}
\usepackage{lipsum} % filler text
\usepackage{luacode} % for "luacode" environment and "\luastring" macro
%% Lua-side code
\begin{luacode}
function colorize ( buff )
buff = string.gsub ( buff, "foo's foo doesn't foo%.%)", "\\textcolor{red}{%0}" )
buff = string.gsub ( buff, "The quick brown fox", "\\textcolor{blue}{%0}" )
buff = string.gsub ( buff, "jumps over the lazy dog", "\\textcolor{orange}{%0}" )
return buff
end
\end{luacode}
%% TeX-side code
\newcommand\colorizeOn{\directlua{luatexbase.add_to_callback
( "process_input_buffer" , colorize , "colorize" )}}
\newcommand\colorizeOff{\directlua{luatexbase.remove_from_callback
( "process_input_buffer" , "colorize" )}}
\AtBeginDocument{\colorizeOn} % turn Lua function on by default
\begin{document}
\chapter{The quick brown fox jumps}
\section{Zorro jumps over the lazy dog}
(Every day, foo's foo doesn't foo.)
The quick brown fox jumps over the lazy dog.
\colorizeOff % switch off the Lua function
The quick brown fox jumps over the lazy dog.
\colorizeOn % switch Lua function back on
The quick brown fox jumps over the lazy dog.
\lipsum[1-12] % filler text
\end{document}