[Tex/LaTex] How to add the section in the headline of the default beamer template

beamer

The default beamer template shows only the subsection in the headline. I would like it to show the section as well as the subsection. It can be done by changing the theme to infolines etc, but I prefer to use the default theme and just add the section name in the headline.

Here is how the original frame looks:
Original frame

Here is how I want it:
Desired frame

MWE:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[T1]{fontenc}
\usepackage{graphicx}

%Modify theme
\setbeamertemplate{navigation symbols}{} %get rid of navigation symbols
\setbeamertemplate{sections/subsections in toc}[ball unnumbered] %bullets in table of contents should be balls
\setbeamertemplate{itemize items}[ball] %bullets in frames should be balls

\title{My title}
\author{My name}
\institute{My institute}
\date{\today}

\begin{document}

\maketitle

\begin{frame}{Outline}
\tableofcontents
\end{frame}

\section{Section 1}

\begin{frame}{Section 1}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\end{itemize}
\end{frame}

\section{Section 2}
\subsection{Subsection 2.1}
\begin{frame}{Subsection 2.1}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\end{itemize}
\end{frame}

\subsection{Subsection 2.2}
\begin{frame}{Subsection 2.2}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\end{itemize}
\end{frame}

\end{document}

Best Answer

One possibility is to (re)define an appropriate template (the sensible candidates are headline or frame title) to use \insertsectionhead and \insertsubsectionhead:

enter image description here

The code:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[T1]{fontenc}
\usepackage{graphicx}

%Modify theme
\setbeamertemplate{navigation symbols}{} %get rid of navigation symbols
\setbeamertemplate{sections/subsections in toc}[ball unnumbered] %bullets in table of contents should be balls
\setbeamertemplate{itemize items}[ball] %bullets in frames should be balls

\setbeamertemplate{headline}{%
  \leavevmode%
  \begin{beamercolorbox}[sep=0.3cm,wd=\paperwidth]{section in head/foot}%
  \usebeamercolor[fg]{structure}\Large\insertsectionhead\hfill\insertsubsectionhead%  
  \end{beamercolorbox}%
}

\title{My title}
\author{My name}
\institute{My institute}
\date{\today}

\begin{document}

\maketitle

\begin{frame}{Outline}
\tableofcontents
\end{frame}

\section{Section 1}

\begin{frame}
\frametitle{Title for the frame}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\end{itemize}
\end{frame}

\section{Section 2}
\subsection{Subsection 2.1}
\begin{frame}
\frametitle{Title for the frame}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\end{itemize}
\end{frame}

\subsection{Subsection 2.2}
\begin{frame}
\frametitle{Title for the frame}
\begin{itemize}
\item Item 1
\item Item 2
\item Item 3
\end{itemize}
\end{frame}

\end{document}