14

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?

Mico
  • 506,678
  • You can, for example, run sed -i … by \write18 and modify text file before compiling it by latex. – Eddy_Em Apr 05 '13 at 12:17
  • 4
    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 05 '13 at 12:25
  • Duplicate?? https://tex.stackexchange.com/questions/248632/highlight-every-occurrence-of-a-list-of-words, https://tex.stackexchange.com/questions/401678/change-color-of-all-occurences-of-particular-character-in-entire-document. Also related: https://tex.stackexchange.com/questions/241440/how-to-insert-a-symbol-to-the-beginning-of-a-line-for-which-a-word-appears – Steven B. Segletes Nov 13 '18 at 01:01

4 Answers4

14

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}

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.

egreg
  • 1,121,712
6

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

\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{
  luatexbase.add_to_callback("process_input_buffer", hello, "hello")}}
\begin{document}
hello hellow Hello hello.
\end{document}
Mico
  • 506,678
4

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:

enter image description here

Aditya
  • 62,301
2

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.

\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}