# Emphasizing all occurrences of given word in document

Please forgive my naivety, but I have a fairly large TeX document and I was hoping that there was an easy way to do the following. I can create code blocks in the text using the following:

\begin{lstlisting}
TEST
\end{lstlisting}


This will give all words within the lstlisting a different font to distinguish it as code from normal text.

I was hoping that there was a way in which I can search the whole document and add these tags to a word. For example every time the word "TEST" appears, it is encased in:

\begin{lstlisting}
TEST
\end{lstlisting}

• Welcome to TeX.SX! For me that seems like a job for your favourite editor's search and replace function (often, but no always, triggered by Ctrl+H). A TeX solution might be possible (there are some crazy things possible in TeX), but not really desirable, I would have thought. It might just be your example, but I think it can't hurt to note that there is also \lstinline{TEST} for single words in text flow. – moewe Aug 15 '15 at 11:22
• Yeah thought that was an option its just I have used the word over multiple tex documents and included the documents as chapters in my m=ain tex file, so it would be tedious opening up every document and using find and replace – TotalNewbie Aug 15 '15 at 11:32
• But it is much safer than making "TEST" active (not sure if that is possible) and let TeX do that itself. Notepad++ lets you do search-and-replace in entire directories of files, so that should not be the excuse. – moewe Aug 15 '15 at 11:34
• Have a look at the related Macro: Replace all occurrences of a word and to a lesser extent How to replace text. So it is not entirely impossible... – moewe Aug 15 '15 at 11:37
• I wouldn't use lstlisting for this, though. lstinline makes more sense, but beware! You may not like an automatic solution for stuff like this. It would be better and more maintainable to do a search/replace (perhaps with sed or the windows equivalent). – Sean Allred Aug 15 '15 at 17:15

Must be compiled with XeTeX.

% arara: xelatex
\documentclass{article}
\usepackage{xesearch}
\SearchList{make-blue}{\textcolor{blue}{#1}}{test}
\SearchList{make-red}{\textcolor{red}{#1}}{maybe,different}
\SearchList{code}{\lstinline{#1}}{code}

\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\usepackage{xcolor}

\begin{document}
Hello, this is a test, Test, TEST.

But maybe you want a different test.

Some code.
\end{document}


Note that you can make an entire list case-sensitive with \SearchList* or a particular word case-sensitive with \SearchList{...}{...}{...,*Word,...}.

• I've also managed to compile it with XeLaTex. I just would like to let others know about it. – Rubens Feb 7 '17 at 9:50
• @Rubens yes, and the XeLaTeX format uses the XeTeX engine :) You may be interested in this jargonfile. – Sean Allred Feb 7 '17 at 12:41
• I know you merely used the package and probably didn't write it, but what the hell is in the manual? I just opened it with texdoc xesearch and my computer went nuts. Eventually, the kernel killed Okular, freeing half of my 8G of RAM. I hate to think what using the package does, if merely opening the manual requires these kinds of resources! I only saw page 1 and it took most of 4G?! I was trying to figure out why this answer now gives an error, but I now have other problems. – cfr Feb 16 '18 at 3:47
• @cfr It's uh–definitely a little busy :-) – Sean Allred Feb 17 '18 at 1:15
• In case you're interested, the follow-up is at tex.stackexchange.com/questions/415559/…. I'm not risking trying to read the documentation again. – cfr Feb 17 '18 at 1:44

Here's a LuaLaTeX-based solution. Note that the search is case-sensitive: while "TEST" is highlighted, "test", "Test", and "TeSt" are not.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{xcolor,luacode,luatexbase}

%% Lua-side code
\begin{luacode}
function highlight_TEST ( line )
return string.gsub ( line, "TEST",
"\\textcolor{red}{\\texttt{TEST}}" )
end