[Tex/LaTex] Beamer customizing headline

beamer

I'm trying to create my own Beamer theme and I have a problem in handling the headline. I would like to create a headline with a coloured line for the frame title, while for other frames I want a classical headline with sections and possibly subsections. Here is the part of the code I use:

\defbeamertemplate*{headline}
{  \ifnum\c@framenumber>1
    \leavevmode%
    \hbox{%
    \begin{beamercolorbox}[wd=\paperwidth,ht=\headerheight,dp=0pt]{}%
       \ifx\insertsection\@empty
       \else
       {
       {\usebeamerfont{headsection}\hspace{1em} \color{LightBlue}\insertsection}
          \ifx\insertsubsection\@empty
          \else
          {\hspace{0.5em}$\vert$ \hspace{0.5em}{\usebeamerfont{headsubsection}\color{DarkGray}\insertsubsection}}%
        \fi
       }
       \fi
    \end{beamercolorbox}%
    } %
  \vskip0pt%
  \else
    \hbox{%
    \begin{beamercolorbox}[wd=\paperwidth,ht=20pt,dp=0pt]{linemid}
    \end{beamercolorbox}%
    }
  \fi
}

In such a way I obtain a result which is not satisfactory because the coloured line in title frame is OK, but sections and subsection of the following frames (and so the title and the body of the frame) are shifted down. It seems that beamer keeps the space reserved for the first headline, but I don't want that. Is there a way to fix the problem?

Best Answer

That's right, the beamer package only calculates the height of the headline once, at the beginning of the document. The macro responsible for this task is called \beamer@calculateheadfoot, and is defined in beamerbaseframecomponents.sty (ll. 161-182). If the head of your headline changes in the course of the presentation (which doesn't sound like a terribly good idea to my mind, BTW), you'll have to issue this command manually.

This can be done for example by adding it to the internal macro \beamer@@@@frame: Like this, the head of head- and footlines etc. are recalculated each time a frame is being built:

\usepackage{etoolbox}
\makeatletter
\pretocmd{\beamer@@@@frame}{%
  \addtocounter{framenumber}{1}%
  \beamer@calculateheadfoot%
  \addtocounter{framenumber}{-1}}{}{}
\makeatother

Add this code snippet to your document or your theme file. (The manual change of framenumber is necessary to get the correct headline as beamer hasn't stepped the counter at this point.)

Minimal example:

\documentclass{beamer}

% To make a compilable example, these definitions are necessary - you can replace them by your own
\colorlet{LightBlue}{blue}
\colorlet{DarkGray}{gray}
\setbeamercolor{linemid}{bg=green}
\newlength{\headerheight}
\setlength{\headerheight}{10pt}

\makeatletter
\setbeamertemplate{headline}
{  \ifnum\c@framenumber>1
    \leavevmode%
    \hbox{%
    \begin{beamercolorbox}[wd=\paperwidth,ht=\headerheight,dp=0pt]{}%
       \ifx\insertsection\@empty
       \else
       {
       {\usebeamerfont{headsection}\hspace{1em} \color{LightBlue}\insertsection}
          \ifx\insertsubsection\@empty
          \else
          {\hspace{0.5em}$\vert$ \hspace{0.5em}{\usebeamerfont{headsubsection}\color{DarkGray}\insertsubsection}}%
        \fi
       }
       \fi
    \end{beamercolorbox}%
    } %
  \vskip0pt%
  \else
    \hbox{%
    \begin{beamercolorbox}[wd=\paperwidth,ht=20pt,dp=0pt]{linemid}
    \end{beamercolorbox}%
    }
  \fi
}

% Recalculate the head of head- and footlines for each frame
% (http://tex.stackexchange.com/a/61788/3323)
\usepackage{etoolbox}
\makeatletter
\pretocmd{\beamer@@@@frame}{%
  \addtocounter{framenumber}{1}%
  \beamer@calculateheadfoot%
  \addtocounter{framenumber}{-1}}{}{}
\makeatother

\begin{document}

\section{section}
\subsection{subsection}
\frame{Title page with special headline}
\frame{Normal frame}
\frame{Normal frame 2}
\end{document}
Related Question