# Colorize text using LuaLaTeX doesn't work

I need to colorize all occurences of certain symbols/words and so I followed the steps of this answer. This is a test code I have written in TeXstudio:

% !TeX program = lualatex

\documentclass[11pt]{beamer}

\usepackage[italian]{babel}
\usepackage{xcolor}
\usepackage{luacode}

\begin{luacode}
function color_a ( s )
s = string.gsub ( s , "a%w" , "\\textcolor{blue}{%0}" )
return s
end
\end{luacode}

\newcommand{\EqColorOff}{\directlua{luatexbase.remove_from_callback("process_input_buffer", "color_a" )}}

\begin{document}
\begin{frame}
\EqColorOn
asdasdasdasdasdasdasd
\EqColorOff
\end{frame}
\end{document}


What I expect is that all occurences of "as" would become blue but instead nothing happens and the string "asdasdasdasdasdasdasd" is printed in black. Can anyone help me?

You need the fragile option at the frame:

\documentclass[11pt]{beamer}

\usepackage[italian]{babel}
\usepackage{xcolor}
\usepackage{luacode}

\begin{luacode}
function color_a ( s )
s = string.gsub ( s , "a%w" , "\\textcolor{blue}{%0}" )
return s
end
\end{luacode}