13

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?

13

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.

5

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

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}
  • such poetry! bravo! – thymaro Nov 13 '18 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.