[Tex/LaTex] Automatical title section in beamer only on the first frame after \section

automationbeamerframe-titlesectioning

I'm trying to make that the title of the last section appears as frametitle only on the first frame after each \section{...} code.

Now, i'm doing it manually with this code :

\newcommand{\TS}[0]{
\mode<beamer>{    
\frametitle{\thesection \insertsection}
}}

And I put manualy on the first frame after \section{title of the last section} this code :

\section{title of the last section}

\begin{frame}
\TS
content of the frame with the last section in frametitle.
\end{frame}

\begin{frame}
This is the second frame without a title.
\end{frame}

So I want make this to made done automatically (and also put as frame subtitle the subsection if exists, and only put the subsection as frametitle if the current section as been displayed on a previous frame, …. I hope I'm being precise enough).

I've read this subject in order to try to customise it with part of the code, but without any success.

Best Answer

You need to find a frame-related hook. Below I've used \beamer@checkframetitle that inserts \insertsectiontitle only for the first frame after \section:

enter image description here

\documentclass{beamer}

\makeatletter
\newcommand<>{\insertsectiontitle}{\frametitle{\thesection~\insertsection}}
\let\oldbeamer@checkframetitle\beamer@checkframetitle% Store the \frametitle checking mechanism
\renewcommand<>{\section}{%
  \gdef\beamer@checkframetitle{% Update \frametitle checking to ...
    \insertsectiontitle% ...insert the section title and...
    \global\let\beamer@checkframetitle\oldbeamer@checkframetitle% ...revert to it's old definition
  }% Regular \section stuff follows
  \alt#1{\@ifnextchar[\beamer@section\beamer@@section}
    {\beamer@secgobble}}
\makeatother

\begin{document}

\section{title of the last section}

\begin{frame}
content of the frame with the last section in frametitle.
\end{frame}

\begin{frame}
This is the second frame without a title.
\end{frame}

\end{document}

An update to the above, which inserts the \subsection as \framesubtitle on the first slide after \section, and as \frametitle after that:

enter image description here

\documentclass{beamer}

\makeatletter
\newcommand<>{\insertsectiontitle}{\frametitle{\thesection~\insertsection}}
\newcommand<>{\insertsubsectiontitle}{\frametitle{\thesubsection~\insertsubsection}}
\newcommand<>{\insertsubsectionsubtitle}{\framesubtitle{\thesubsection~\insertsubsection}}
\let\oldbeamer@checkframetitle\beamer@checkframetitle% Store the \frametitle checking mechanism
\renewcommand<>{\section}{%
  \gdef\beamer@checkframetitle{% Update \frametitle checking to ...
    \insertsectiontitle% ...insert the section title and...
    \ifx\relax\insertsubsection\relax
      \global\let\beamer@checkframetitle\oldbeamer@checkframetitle% ...revert to it's old definition - no subsection
    \else
      \insertsubsectionsubtitle% ...insert the subsection title and...
      \global\let\beamer@checkframetitle\insertsubsectiontitle% ...always insert subsection
    \fi
  }% Regular \section stuff follows
  \alt#1{\@ifnextchar[\beamer@section\beamer@@section}
    {\beamer@secgobble}}
\makeatother

\begin{document}

\section{First section}
\subsection{First subsection}

\begin{frame}
Frame 1
\end{frame}

\begin{frame}
Frame 2
\end{frame}

\subsection{Second subsection}

\begin{frame}
Frame 3
\end{frame}

\begin{frame}
Frame 4
\end{frame}

\section{Second section}
\subsection{First subsection}

\begin{frame}
Frame 5
\end{frame}

\begin{frame}
Frame 6
\end{frame}

\end{document}