beamercustom-theme

Add gradient between custom lines of header


I am working on setting up a beamer theme, which is based on the JuanLesPins theme. This specific theme has a touch that I really like, but I am unable to reproduce it: in the header, the lines that indicate the title, current section and current subsections have a gradient between them (see images: 1st is mine, 2nd is the original). Don't worry about having different colors, it's a different part of my customization and it's irrelevant for this question.

My version, without gradient The JuanLesPins smooth version

I would like to know how to do a similar smooth gradient effect. For what it's worth, I use pdflatex, but if necessary, I could switch to a different compiler.

One of the reasons why I am customizing the theme is that I want the lines to appear only when their content is non empty (that is, display the section and/or subsection line only if they respectively exist). To do this, here is the code I came up with thus far:

\usetheme{JuanLesPins}
\makeatletter
\setbeamertemplate{headline}{%
  \begin{beamercolorbox}[wd=\paperwidth,ht=2ex,dp=1ex]{title in head/foot}
    \hspace*{2ex}\inserttitle\hspace*{2ex}
  \end{beamercolorbox}
  
  % Section appears only if it exists
  \ifx\insertsectionhead\@empty
  % No section, do nothing
  \else
    \begin{beamercolorbox}[wd=\paperwidth,ht=2ex,dp=1ex]{section in head/foot}
      \hspace*{4ex}\insertsectionhead\hspace*{2ex}
    \end{beamercolorbox}
    
    % Subsection appears on a new line only if it exists
    \ifx\insertsubsectionhead\@empty
    % No subsection, do nothing
    \else
      \begin{beamercolorbox}[wd=\paperwidth,ht=2ex,dp=1ex]{subsection in head/foot}
        \hspace*{6ex}\insertsubsectionhead\hspace*{2ex}
      \end{beamercolorbox}
    \fi
  \fi
}
\makeatother

In the beamercolorbox documentation I found, I don't see any option that I could tweak to make a gradient, so maybe this is not the right object to do that.

Here is a "minimal" reproducible example:

\documentclass{beamer}

\usetheme{JuanLesPins}

\makeatletter
\setbeamertemplate{headline}{%
  \begin{beamercolorbox}[wd=\paperwidth,ht=2ex,dp=1ex]{title in head/foot}
    \hspace*{2ex}\inserttitle\hspace*{2ex}
  \end{beamercolorbox}
  
  % Section appears only if it exists
  \ifx\insertsectionhead\@empty
  % No section, do nothing
  \else
    \begin{beamercolorbox}[wd=\paperwidth,ht=2ex,dp=1ex]{section in head/foot}
      \hspace*{4ex}\insertsectionhead\hspace*{2ex}
    \end{beamercolorbox}
    
    % Subsection appears on a new line only if it exists
    \ifx\insertsubsectionhead\@empty
    % No subsection, do nothing
    \else
      \begin{beamercolorbox}[wd=\paperwidth,ht=2ex,dp=1ex]{subsection in head/foot}
        \hspace*{6ex}\insertsubsectionhead\hspace*{2ex}
      \end{beamercolorbox}
    \fi
  \fi
}
\makeatother

\title{My Custom Beamer Theme}
\subtitle{Subtitle}

\begin{document}

\begin{frame}
    \titlepage
\end{frame}

\section{First section}
\begin{frame}
    This frame is the only one of the section.
\end{frame}

\section{Second section}
\subsection{First subsection} 
\begin{frame}
    Now look, the subsection line was added in the header.
\end{frame}

\subsection{Second subsection} 
\begin{frame}
    It's still here, nice.
\end{frame}

\end{document}

Solution

  • Beamer uses vertical shadings in the background of the JuanLesPins theme. You can define a whole bunch of these shadings and use them conditionally in your headline:

    \documentclass{beamer}
    
    \usetheme{JuanLesPins}
    
    \makeatletter
    \AfterBeginDocument{
      {
      
        \usebeamerfont*{headline}
      
        \usebeamercolor{normal text}
        \usebeamercolor{title in head/foot}
        \usebeamercolor{subsection in head/foot}
        \usebeamercolor{section in head/foot}
        \usebeamercolor{frametitle}
      
        \pgfdeclareverticalshading{beamer@treeshade@a}{\the\paperwidth}{%
          color(0ex)=(pgftransparent!0);%
          color(2.25ex)=(pgftransparent!0);%
          color(3.25ex)=(pgftransparent!0);%
          color(5.5ex)=(pgftransparent!0);%
          color(6.5ex)=(title in head/foot.bg);%
          color(9.25ex)=(title in head/foot.bg)%
        }
        
        \pgfdeclareverticalshading{beamer@treeshade@b}{\the\paperwidth}{%
          color(0ex)=(pgftransparent!0);%
          color(2.25ex)=(pgftransparent!0);%
          color(3.25ex)=(section in head/foot.bg);%
          color(5.5ex)=(section in head/foot.bg);%
          color(6.5ex)=(title in head/foot.bg);%
          color(9.25ex)=(title in head/foot.bg)%
        } 
        
      }
    }
    
    \setbeamertemplate{headline}{%
       \ifx\insertsectionhead\@empty
         \pgfuseshading{beamer@treeshade@a}%
       \else
         \ifx\insertsubsectionhead\@empty
           \pgfuseshading{beamer@treeshade@b}%
         \else
           \pgfuseshading{beamer@treeshade}%
         \fi
      \fi 
      \vskip-0.05ex%
      \vskip1ex%
      \vskip-9.15ex%
      \begin{beamercolorbox}[wd=\paperwidth,ht=1.125ex,dp=1.125ex,ignorebg,%
        leftskip=.3cm,rightskip=.3cm plus1fil]{title in head/foot}
        \usebeamerfont{title in head/foot}\insertshorttitle
      \end{beamercolorbox}
      \begin{beamercolorbox}[wd=\paperwidth,ht=2.125ex,dp=1.125ex,ignorebg,%
        leftskip=.3cm,rightskip=.3cm plus1fil]{section in head/foot}
        \usebeamerfont{section in head/foot}%
        \hskip6pt\usebeamertemplate{section in head/foot}
      \end{beamercolorbox}
      \begin{beamercolorbox}[wd=\paperwidth,ht=2.125ex,dp=1.125ex,ignorebg,%
          leftskip=.3cm,rightskip=.3cm plus1fil]{subsection in head/foot}
        \usebeamerfont{subsection in head/foot}%
        \hskip12pt\usebeamertemplate{subsection in head/foot}
      \end{beamercolorbox}
      \vskip-0.4ex%
    }
    \makeatother 
    
    \title{My Custom Beamer Theme}
    \subtitle{Subtitle}
    
    \begin{document}
    
    \begin{frame}
        \titlepage
    \end{frame}
    
    \section{First section}
    \begin{frame}
        This frame is the only one of the section.
    \end{frame}
    
    \section{Second section}
    \subsection{First subsection} 
    \begin{frame}
        Now look, the subsection line was added in the header.
    \end{frame}
    
    \subsection{Second subsection} 
    \begin{frame}
        It's still here, nice.
    \end{frame}
    
    \end{document}
    

    enter image description here