我需要为某些符号/单词的所有出现着色,因此我遵循了以下步骤这个答案这是我在 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}
我期望所有出现的“as”都会变成蓝色,但实际上什么都没有发生,字符串“asdasdasdasdasdasd”以黑色打印。有人能帮帮我吗?
答案1
您需要fragile
框架中的选项:
\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}