[Tex/LaTex] Section numbering in Beamer (‘CambridgeUS’ theme)

beamernumberingsectioning

Does anybody know how to make latex put the number for a section presented in header?

\documentclass{beamer}
%\usetheme{Warsaw}
\usetheme{CambridgeUS}
\begin{document}

\section{Section a}
\begin{frame}
Test frame
\end{frame}
\end{document} 

Thank you in advance.

Best Answer

For those themes using the infolines outer theme, such as CambridgeUS, you can add the following lines to the preamble:

\setbeamertemplate{headline}
{
  \leavevmode%
  \hbox{%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,right]{section in head/foot}%
    \usebeamerfont{section in head/foot}\thesection\ \insertsectionhead\hspace*{2ex}
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,left]{subsection in head/foot}%
    \usebeamerfont{subsection in head/foot}\hspace*{2ex}\insertsubsectionhead
  \end{beamercolorbox}}%
  \vskip0pt%
}

EDIT: In the following example I defined a \Switch command that allows to turn on/off the numbering of the sections in the headline; initially, it is set to 0 (turn off numbering), but redefining it to be 1 will turn on the numbering; the command can be redefined as many times as required:

\documentclass{beamer}
\usepackage{ifthen}
\usetheme{CambridgeUS}

\newcommand\Switch{0}
\newcommand\SecInHead{%
  \ifthenelse{\equal{\Switch}{1}}
    {\thesection.}{}}

\setbeamertemplate{headline}
{
  \leavevmode%
  \hbox{%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,right]{section in head/foot}%
    \usebeamerfont{section in head/foot}\SecInHead\insertsectionhead\hspace*{2ex}
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,left]{subsection in head/foot}%
    \usebeamerfont{subsection in head/foot}\hspace*{2ex}\insertsubsectionhead
  \end{beamercolorbox}}%
  \vskip0pt%
}
\begin{document}

\begin{frame}
Test frame with un-numbered section
\end{frame}

\renewcommand\Switch{1}
\section{Section a}
\begin{frame}
Test frame with numbered section
\end{frame}

\renewcommand\Switch{0}
\begin{frame}
Test frame with un-numbered section
\end{frame}

\end{document}