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?

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

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.