# Strange behaviour with alert in mathmode

I got this unexpected behaviour when I change colour to alerted text in mathmode. In particular, alerted text.fg seems to change only when \alert is called again. Changing with \colorlet affect only the colour inside mathmode.

Here an example

\documentclass{beamer}
\begin{document}
\begin{frame}{Title}
$\begingroup\setbeamercolor{alerted text}{fg=blue}\alert{blue}\endgroup+\textcolor{alerted text.fg}{red}$  These should be \textcolor{alerted text.fg}{red}, \alert{red} and \textcolor{alerted text.fg}{red}

$\begingroup\setbeamercolor{alerted text}{fg=blue}\alert{blue}\endgroup\setbeamercolor{alerted text}{fg=red}+\textcolor{alerted text.fg}{red}$  These should be \textcolor{alerted text.fg}{red}, \alert{red} and \textcolor{alerted text.fg}{red}

$\begingroup\setbeamercolor{alerted text}{fg=blue}\alert{blue}\endgroup\setbeamercolor{alerted text}{fg=red}\colorlet{alerted text.fg}{red}+\textcolor{alerted text.fg}{red}$  These should be \textcolor{alerted text.fg}{red}, \alert{red} and \textcolor{alerted text.fg}{red}

$\begingroup\setbeamercolor{alerted text}{fg=blue}\alert{blue}\endgroup\setbeamercolor{alerted text}{fg=red}\alert{}+\textcolor{alerted text.fg}{red}$  These should be \textcolor{alerted text.fg}{red}, \alert{red} and \textcolor{alerted text.fg}{red}
\end{frame}

\end{document}

• Do you really need to change back and forth the colors? Wouldn't it be simpler to apply just the color you want without \alert? May 7 at 15:13

\setbeamercolor{}{} itself does not yet create the .fg and .bg colours, they are only created when the beamer colour is used.

But instead of changing the alert colour back and forth, you could create more alert-like macros with whatever colour you like -- or simply use \structure, that's blue by default.

\documentclass{beamer}

\setbeamercolor{foo}{fg=green}

\newenvironment<>{fooenv}{\begin{altenv}#1%
{\usebeamercolor[fg]{foo}\usebeamerfont{foo}\usebeamertemplate{foo text begin}}
{\usebeamertemplate{foo text end}}{\color{.}}{}\ignorespaces}{\ifhmode\unskip\fi\end{altenv}}

\newcommand<>{\foo}[1]{\begin{fooenv}#2\relax#1\end{fooenv}}

\begin{document}
\begin{frame}{Title}
$\structure{blue}+\textcolor{alerted text.fg}{red}$  These should be \textcolor{alerted text.fg}{red}, \alert{red} and \textcolor{alerted text.fg}{red}

$\foo{green}+\textcolor{alerted text.fg}{red}$  These should be \textcolor{alerted text.fg}{red}, \alert{red} and \textcolor{alerted text.fg}{red}

\end{frame}

\end{document}