# Coloured math in beamer

I have a question: Is there any way to have math (everything between $s) in different colour than text (for example, green) in beamer? • Please have a look at How do I get different colors for math and for text. I suggest closing this question as a duplicate, at least if the beamer class would not make a difference. Dec 8, 2011 at 9:52 • beamer has its own internal way of changing the colour for mathematics which is detailed in Section 17.3 of the manual. If this worked for what you have in mind then this would be an easier method than that given in the question that Stefan links to. Dec 8, 2011 at 9:59 • Michal: Did Andrew's comment help answer your question? Dec 10, 2011 at 20:42 • @AndrewStacey: That looks like an answer to me... Dec 22, 2011 at 3:40 ## 2 Answers Beamer has this functionality already built in. The beamer colours math text and its children math text inlined, math text displayed, and normal text in math text control the colour of mathematics in a beamer document. Section 17.3 of the beamer manual (version 3.12) has more details, note that they come with two warnings: one a "consider not doing it" warning and the other a "the implementation might break" warning. An alternative method which is more robust but interacts less well with beamer's internal colour stuff is to use the unicode-math package (needs lualatex or xelatex) to load a font for mathematics with a specified colour. See Will Robertson's answer to my question Can I change the font and colour of a letter permanently? for details. Where the weakened interaction shows is with things like partially visible text in beamer: this is done by mixing colours at the time that the text is parsed, but with unicode-math then the colour is set once-for-all at the start and so can't be faded later on. (This is the solution that I use for this, by the way. To get round this interaction issue - which I don't encounter very often - I fade things by overlaying a partially transparent block instead of by mixing colours.) This is a snippet in LuaTeX using regular expressions. We activate and deactivate searching manually by two TeX commands and in that selected part we search for $...$, but neither for $$...$$ nor \$. It seems that using this method we could modify almost any part of the document on-the-fly.

If you don't use LuaTeX, we could extract those selected parts, modify them and return them to the main TeX engine on-the-fly or after the run of TeX (the same method use external tools like MakeIndex, Xindy, ...).

I enclose the code (we run lualatex mal-inline-math.tex) and its output.

% lualatex mal-inline-math.tex
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{luacode}
\usepackage{xcolor}

\begin{document}
\begin{luacode*}
function colorit() -- the core of the algorithm
math=tex.toks[0] -- take data from TeX to Lua
math=unicode.utf8.gsub(math, "([^%$\\])(%$[^%$]+[^%$\\]%$)", "%1{\\color{blue}%2}") -- regular expression, a try to find$...$without $$...$$ and withtou a dollar sign print(math) -- print the result to the terminal tex.print(math) -- typeset the result in TeX end -- function, colorit \end{luacode*} % pass an argument to Lua \def\colorit#1\endcolorit{\toks0{#1}\directlua{colorit()}} Untouched part:$k+l+m$.\par \colorit % color inline mathematics, if found Some text.$a+b+c$. \$ and some mathematics.
$$d+e+f,$$ $x+y+z$.
Next part of the document.
\endcolorit % stop searching for inline mathematics
\end{document}