21

I would like to specify a background color in the alert environment. I can, for example, use

\setbeamercolor{alerted text}{fg=blue} 

to change the colour of the alterted text to blue, but this

\setbeamercolor{alerted text}{bg=blue} 

does not work. Is there a way to get around this?

I find that the font colours that the alert environment makes are too subtle and I'd like to add a coloured highlight box around my text in addition to changing the fg colour.

lockstep
  • 250,273

3 Answers3

20

Since (La)TeX doesn't have a “background color” for text, you would have to put your text inside a box which you can then color. Consider the following example using \colorbox.

\documentclass{beamer}

\setbeamercolor{alerted text}{fg=white,bg=red}
\newcommand{\boxalert}[1]{{%
  \usebeamercolor{alerted text}\colorbox{bg}{\alert{#1}}%
}}

\begin{document}
\begin{frame}
Hello \boxalert{World}!
\end{frame}
\end{document}

Which renders

Hello World

Edit: I'm no expert in beamer internals, so the following is nothing more than a hack. However, if you would like \boxalert to work with overlays, something like the following “seems” to work:

\newenvironment{boxalertenv}{\begin{altenv}%
      {\usebeamertemplate{alerted text begin}\usebeamercolor[fg]{alerted text}\usebeamerfont{alerted text}\colorbox{bg}}
      {\usebeamertemplate{alerted text end}}{\color{.}}{}}{\end{altenv}}

\newcommand<>{\boxalert}[1]{{%
  \begin{boxalertenv}#2{#1}\end{boxalertenv}%
}}

...

Hello \boxalert<2>{World}!
Juan A. Navarro
  • 62,139
  • 32
  • 140
  • 169
  • 1
    This breaks if you use overlays with alert, like so: \alert<+->{Text} – Seamus Feb 17 '11 at 17:55
  • Thanks for pointing that out! I changed the answer defining a new command instead of trying to redefine \alert, I've also provided some sort of hack to get the new command working with overlays. – Juan A. Navarro Feb 18 '11 at 10:46
  • 1
    Navarro's solution changes the text position. There must be a way to tell latex to draw a white background when non-alerted... – dividebyzero Aug 02 '12 at 21:14
  • The current solution works most of the time but if \boxalert falls inside a minipage environment it breaks it down... – Sergio Parreiras Sep 16 '13 at 16:26
3

You can also create a new macro and use \alt in order to achieve to redefine an alert and use a colorbox. I highly recommend the tutorial "A beamer tutorial in Beamer". It contains many useful commands.

\documentclass{beamer}
\usepackage{color}
\def \boxalert#1#2{\alt<#1>{\colorbox{red}{#2}}{#2}} 

\begin{document}
     \begin{frame}
        \onslide<3->{\boxalert{3}{3 hello out of the itemize!}} 
        \begin{itemize}
          \item<1-> 1
          \item<2-> 2 
          \item<3->{\boxalert{3}{3 hello!}} 
          \item<4-> 4
          \item<5-> 5
       \end{itemize}
    \end{frame}

    \begin{frame}
       \begin{itemize}
         \item<1-> 1
         \item<2-> 2
         \item{\boxalert{3}{3 hello!}} 
         \item<4-> 4
         \item<5-> 5
       \end{itemize}
    \end{frame}
\end{document}
orestis
  • 143
  • 5
2

Here is a suggestion which redefines to use \alert in order to use \highLight of lua-ul (it will work only with LuaLaTeX).

The blue background won't change the position of the text.

\documentclass{beamer}
\usepackage{xcolor}
\usepackage{luacolor,lua-ul}

\setbeamercolor{alerted text}{fg=yellow,bg=cyan}

\makeatletter \renewenvironment<>{alertenv} {\only#1{@highLight[alerted text.bg]\color{alerted text.fg}}} \makeatother

\begin{document}

\begin{frame} \alert{highlighted text}

\alert<2>{Some text} highlighted in the second overlay. \end{frame}

\end{document}

Output of the above code

F. Pantigny
  • 40,250