# automatically apply special formatting to selected words in text

Is it possible and furthermore constructive to make LaTeX automatically format specific words in specific way? Let's say, for example, I'd like to have every occurrence of the word "hello" in a bold cyan monospaced font, whereas the rest of the document remains normal, sans serif and black.

How can I achieve that without having to do a search&replace to place "hello" into a custom command every time before I compile the .tex file?

• You can, for example, run sed -i … by \write18 and modify text file before compiling it by latex. – Eddy_Em Apr 5 '13 at 12:17
• Wouldn't it be easier, if possible, to do that type of formatting in advance in the input file itself, by defining a custom \hello macro? – jub0bs Apr 5 '13 at 12:25
• – Steven B. Segletes Nov 13 '18 at 1:01

Compile this with XeLaTeX

\documentclass{article}
\usepackage{color,xesearch}

\SearchList*{redwords}{\textcolor{red}{#1}}{hello}

\begin{document}
This is hello and this is hellow.
\end{document}


But don't use it. If you want to specially mark a word, use a macro.

Why? Relying on automatic procedures is always risky: if I have a choice, I prefer explicit mark up. Another reason is that xesearch is resource hungry and currently unmaintained.

There's nothing similar for pdflatex; something can possibly be done with LuaLaTeX. I tried chickenize but it doesn't seem to offer the feature.

A little bit late to the party, but here's a LuaLaTeX-based solution. It sets up a function that scans the input before TeX does any of its usual processing; any instances string hello are replaced "on the fly" with {\ttfamily\bfseries\color{cyan}hello}, to be processed by LaTeX.

\documentclass{article}
\usepackage{fontspec}
\setmonofont{Courier New}[BoldFont="Courier New Bold"]
\usepackage{luacode,xcolor}
\begin{luacode}
function hello ( s )
return (string.gsub(s , "(hello)(%L)", "{\\ttfamily\\bfseries\\color{cyan}%1}%2"))
end
\end{luacode}
\AtBeginDocument{\directlua{
\begin{document}
hello hellow Hello hello.
\end{document}


In ConTeXt, you can use the translate module to do such compile time search and replace:

\usemodule[translate]
\definehighlight[SPECIAL][color=cyan, style=\ttbf]
\def\HELLO{\SPECIAL{hello}}
\translateinput[hello][\HELLO{}]
\enableinputtranslation

\setupbodyfont[ss,12pt]

\starttext
When you greet people, you say hello.
\stoptext


which gives:

For my amusement in using LaTeX3, the code below defines an environment boldwords that accepts a comma separated list of words to be typeset in bold inside the environment. Running the MWE produces:

Note, in particular, that the log in catalogue is not made bold because it is not a complete word.

\documentclass{article}
\usepackage{environ}
\usepackage{expl3}

\ExplSyntaxOn
\clist_new:N \l_bold_word_clist
\tl_new:N \l_BODY_tl
\NewEnviron{boldwords}[1]{
\clist_set:Nn \l_bold_word_clist {#1}
\tl_set:No \l_BODY_tl {\BODY}
\clist_map_inline:Nn \l_bold_word_clist {
\regex_replace_all:nnN {\b##1\b} { \c{textbf}\cB\{##1\cE\} } \l_BODY_tl
}
\l_BODY_tl
}
\ExplSyntaxOff

\begin{document}

\begin{boldwords}{dog, log}
a dog, a cat, a frog a log and a catalogue
a dog, a cat, a frog a log and a catalogue
a dog, a cat, a frog a log and a catalogue

a dog, a cat, a frog a log and a catalogue
a dog, a cat, a frog a log and a catalogue
\end{boldwords}

\end{document}

• such poetry! bravo! – thymaro Nov 13 '18 at 10:21