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!
- 13
-
Should the "World" and "People" substrings also be marked in red? – Mico Aug 23 '15 at 09:57
-
yes, whole words should be formatted (the question has been clarified). – Oleg Aug 23 '15 at 09:58
-
2Welcome to TeX.SX! There are How to style all instances of a certain word?, How to automatically color a string that starts by a specific character (@)?, Macro: Replace all occurrences of a word. – moewe Aug 23 '15 at 09: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 01:10
1 Answers
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
luatexbase.add_to_callback( "process_input_buffer", Hallo_Highlight, "Hallo")
\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{%
luatexbase.add_to_callback( "process_input_buffer",
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}
-
-
Is it possible to check in
Hallo_Highlightfunction, which environment thelinecomes from? In particular, I need to ignore formatting if the matched words are inlstlistingenvironment. – Oleg Aug 23 '15 at 19:50 -

