![使用 LuaLaTeX 给文本上色不起作用](https://linux22.com/image/398733/%E4%BD%BF%E7%94%A8%20LuaLaTeX%20%E7%BB%99%E6%96%87%E6%9C%AC%E4%B8%8A%E8%89%B2%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我需要为某些符号/单词的所有出现着色,因此我遵循了以下步骤这个答案这是我在 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}