1

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!

3

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.

enter image description here

% !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.

enter image description here

% !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}
  • 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

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.