2

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}
\usetheme{Madrid}

\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{\EqColorOn}{\directlua{luatexbase.add_to_callback("process_input_buffer", color_a , "color_a" )}}
\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?

1 Answers1

4

You need the fragile option at the frame:

\documentclass[11pt]{beamer}
\usetheme{Madrid}

\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{\EqColorOn}{\directlua{luatexbase.add_to_callback("process_input_buffer", color_a , "color_a" )}}
\newcommand{\EqColorOff}{\directlua{luatexbase.remove_from_callback("process_input_buffer", "color_a" )}}


\begin{document}
    \begin{frame}[fragile]
        \EqColorOn
        asdasdasdasdasdasdasd
        \EqColorOff
    \end{frame}
\end{document}

enter image description here

Ulrike Fischer
  • 327,261