josephwright / beamer

A LaTeX class for producing presentations and slides
Other
1.35k stars 139 forks source link

Enable transparent background of beamercolorbox #884

Open chrjabs opened 5 months ago

chrjabs commented 5 months ago

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 ```latex \documentclass[xcolor=usenames,dvipsnames]{beamer} \colorlet{color1}{PineGreen} \mode { \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:

samcarter commented 5 months ago

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:

Screenshot 2024-02-08 at 13 59 08

chrjabs commented 5 months ago

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

samcarter commented 4 months ago

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.

chrjabs commented 4 months ago

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?

samcarter commented 4 months ago

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?

chrjabs commented 4 months ago

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.