beamer icon indicating copy to clipboard operation
beamer copied to clipboard

Enable transparent background of beamercolorbox

Open chrjabs opened this issue 1 year ago • 6 comments

I am working on a beamer theme right now and found myself wanting colorboxes with truly transparent backgrounds. I found a stackexchange post addressing this topic and then noticed that implementing this as a proper parameter bgopacity for beamercolorbox and beamerboxesrounded does not seem too hard.

The result is cleanest for non-rounded boxes, as they are drawn as one shape. For rounded boxes, especially with a heading, the box can sometimes get tiny gaps, as the shapes can't overlap with transparent background colours. I made sure that the shapes still overlap when no transparency is used, so that the gaps don't appear in this case.

Here is an example of what I get with my code.

An example of  colorboxes with transparent background

The LaTeX code to generate the example above
\documentclass[xcolor=usenames,dvipsnames]{beamer}

\colorlet{color1}{PineGreen}

 \mode<presentation>
{
\usetheme{default}
\usecolortheme{lily}
\usefonttheme{professionalfonts}
\setbeamercolor{alert}{fg=red!80!black}
\setbeamercolor{text1}{fg=color1!80!black}
\setbeamercolor{text2}{fg=black}
\setbeamercolor{structure}{fg=color1!80!black}
\setbeamercolor{frametitle}{fg=red!80!black}
\setbeamercolor{box1}{fg=black,bg=Dandelion}
\setbeamercolor{box2}{fg=black,bg=SeaGreen}
\setbeamercolor{box3upper}{fg=black,bg=blue}
\setbeamercolor{box3lower}{fg=black,bg=orange}

\setbeamerfont{frametitle}{series=\bfseries}
\setbeamertemplate{frametitle}
{
  \begin{flushright}
    \insertframetitle\par
  \end{flushright}
}
\usebackgroundtemplate{\includegraphics[width=\paperwidth,height=\paperheight]{lion.png}}
\setbeamercovered{transparent}
\setbeamerfont{title}{size=\Large,series=\bfseries,shape=\itshape,family=  \rmfamily}
\setbeamercolor{title}{fg=red!80!black}
}

\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}

\begin{document}

\begin{frame}
\frametitle{slide title}
\begin{columns}
\column{0.5\textwidth}
  \begin{beamercolorbox}[shadow=true,rounded=true,bgopacity=.35]{box1}
    {\usebeamercolor[fg]{text2} \bf T1}
    \begin{itemize}
    \item element
    \item element
    \item element
    \item element
    \item element
    \end{itemize}
  \end{beamercolorbox}
\column{0.5\textwidth}
  \begin{beamercolorbox}[shadow=true,rounded=false,bgopacity=.35]{box2}
    {\usebeamercolor[fg]{text2} \bf T2}
    \begin{itemize}
    \item element
    \item element
    \item element
    \item element
    \end{itemize}
  \end{beamercolorbox}
\end{columns}
\begin{beamerboxesrounded}[bgopacity=.35,upper=box3upper,lower=box3lower]{Testtitle}
  \begin{itemize}
    \item element
    \item element
  \end{itemize}
\end{beamerboxesrounded}
\end{frame}

\end{document}

The implementation is based on this stackexchange post and I made the following additional modifications:

  • implement transparent background for rounded=false: the stackexchange post only made modifications to beamerboxesrounded, not to non-rounded boxes that are drawn directly in beamercolorbox
  • make sure boxes are not affected with bgopacity=1: in order to make sure I'm not messing anything up, pretty much all changes are guarded by if bgtransparency != 1 (looking through the theme examples in the userguide, they all still look fine to me)
  • more exact clipping of shadows when transparent background is used

chrjabs avatar Feb 08 '24 08:02 chrjabs

Thanks for your pull request! I had a quick look at the code, but did not have time yet to test. Just some things I'm wondering about:

  • shouldn't the option be documented for both kinds of colourboxes?

  • on the left hand side, the shadow is visible behind the semi-transparent box:

Screenshot 2024-02-08 at 13 59 08

samcarter avatar Feb 08 '24 13:02 samcarter

Good points, I entirely missed that beamerboxesrounded is documented. I added the missing documentation and fixed the shadow clipping.

chrjabs avatar Feb 08 '24 14:02 chrjabs

One point to think about: with the default value of 1, the opacity will no longer adopt to the surrounding opacity. This means existing code like https://tex.stackexchange.com/a/656921/36296 or https://tex.stackexchange.com/a/331416/36296 will no longer give the desired result.

samcarter avatar Feb 20 '24 10:02 samcarter

Right. I guess one could leave \bmb@bgopacity undefined by default and only call \pgfsetfillopacity if it is defined. Or did you have another idea for how to deal with this?

chrjabs avatar Feb 20 '24 10:02 chrjabs

Right. I guess one could leave \bmb@bgopacity undefined by default and only call \pgfsetfillopacity if it is defined. Or did you have another idea for how to deal with this?

Yes, this could work.

Or maybe avoid all potential backwards compatibility problems by creating a new transparent box?

samcarter avatar Feb 20 '24 10:02 samcarter

Got around to implementing this with not defining \bmb@bgopacity by default. The example from https://tex.stackexchange.com/a/656921/36296 seems to build without an issue. I am not entirely sure what would happen now if you do something like this:

\begin{beamercolorbox}[bgopacity=.5]
\begin{beamercolorbox}
\end{beamercolorbox}
\end{beamercolorbox}

I suppose the inner box would still be drawn with 0.5 opacity, but I think this is a pretty niche case and can be easily fixed by setting bgopacity=1 on the inner box.

chrjabs avatar Feb 27 '24 09:02 chrjabs