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?


4 Answers 4


Compile this with XeLaTeX



This is hello and this is hellow.

enter image description here

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.

enter image description here

\setmonofont{Courier New}[BoldFont="Courier New Bold"]
function hello ( s ) 
   return (string.gsub(s , "(hello)(%L)", "{\\ttfamily\\bfseries\\color{cyan}%1}%2"))
  luatexbase.add_to_callback("process_input_buffer", hello, "hello")}}
hello hellow Hello hello.

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

\definehighlight[SPECIAL][color=cyan, style=\ttbf]


When you greet people, you say hello. 

which gives:

enter image description here


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:

enter image description here

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


\clist_new:N \l_bold_word_clist
\tl_new:N \l_BODY_tl
   \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


  \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

  • such poetry! bravo!
    – thymaro
    Nov 13, 2018 at 10:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .