# Format words starting with specific letter sequence

Is it possible to automatically format words starting from specific letter sequence? For instance, in the text "HalloWorld, HalloPeople, there is a nice day!" I want the whole words starting from "Hallo" (case-sensitive) to be red ("HalloWorld" and "HalloPeople" in this case). Thanks!

• Should the "World" and "People" substrings also be marked in red? – Mico Aug 23 '15 at 9:57
• yes, whole words should be formatted (the question has been clarified). – Oleg Aug 23 '15 at 9:58
• – moewe Aug 23 '15 at 9:59
• Thanks! I will study this solutions (I'm quite new in LaTeX). – Oleg Aug 23 '15 at 10:09
• I'd use external tools (Perl, sed in Unix/Linux, Python) for this. But then again, I'm a complete rookie programming in (La)TeX... – vonbrand Aug 24 '15 at 1:10

The following solution, which uses LuaLaTeX, is a simple extension of the answer given at https://tex.stackexchange.com/a/165224/5001.

The solution assumes that the string "Hallo", without any subsequent letters, should be highlighted as well. If that's not the case, just change "(Hallo%a*)" to "(Hallo%a+)" in the second argument of the string.gsub function.

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

\usepackage{luacode,luatexbase}
\begin{luacode}
function Hallo_Highlight ( line )
return string.gsub(line, "(Hallo%a*)" , "\\textcolor{red}{%0}")
end
\end{luacode}

\begin{document}
Hallo xyz HalloWorld, HalloPeople! abc
\end{document}


Addendum to address the OP's follow-up question: To switch off the highlighting of the "Hallo..." words whenever a lstlisting environment is active, it's useful to (a) set up explicit LaTeX macros that turn highlighting on and off (called \HighlightOn and \HighlightOff, respectively, and (b) use the etoolbox package to patch the lstlisting environment so that highlighting is switched off at the start of a lstlisting environment and switched back on at the end of the lstlisting environment. The macros \HighlightOn and \HighlightOff can also be used in other parts of the document; do make sure, though, to alternate the use of the macros. I.e., don't use \HighlightOn twice (or more times) in a row; ditto for \HighlightOff.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor,luacode,luatexbase}
\begin{luacode}
function Highlight ( line )
line = string.gsub (line, "(Hallo%a*)" , "\\textcolor{red}{%0}")
return ( line )
end
\end{luacode}
\newcommand\HighlightOn{\luaexec{%
Highlight, "Highlight" )}}
\newcommand\HighlightOff{\luaexec{%
luatexbase.remove_from_callback('process_input_buffer',
"Highlight" )}}
\HighlightOn  % switch on highlighting of "Hallo..." strings

\usepackage{listings,etoolbox}
\AtBeginEnvironment{lstlisting}{\HighlightOff}
\AtEndEnvironment{lstlisting}{\HighlightOn}

\begin{document}
Hallo xyz HalloWorld, HalloPeople! abc

\begin{lstlisting}
Write('HalloWorld');
\end{lstlisting}

Hallo xyz HalloWorld, HalloPeople! abc
\end{document}

• Thanks! Works like a charm ) Lua seems to be very powerful tool. – Oleg Aug 23 '15 at 10:45
• Is it possible to check in Hallo_Highlight function, which environment the line comes from? In particular, I need to ignore formatting if the matched words are in lstlisting environment. – Oleg Aug 23 '15 at 19:50
• @Oleg - Please see the addendum to my answer. – Mico Aug 23 '15 at 20:35