Automatical title section in beamer only on the first frame after \section

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.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – yo' Jan 29 '15 at 23:51
• Instead of using \frametitle, have you tried using the tree or smoothtree outer themes? Try this, for example, \documentclass{beamer} \useoutertheme{smoothtree} \begin{document} \section{Test section one} \begin{frame} test\end{frame} \subsection{Test subsection one one} \begin{frame} test\end{frame} \subsection{Test subsection one two} \begin{frame} test\end{frame} \section{Test section two} \begin{frame} test\end{frame} \subsection{Test subsection two one} \begin{frame} test\end{frame} \end{document} – Gonzalo Medina Jan 30 '15 at 0:02
• It does not do what I want : It displays alway the section instead of displaying it only on the first frame after the \section{...}. But thanks for trying. – math45 Jan 30 '15 at 0:27

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

\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:

\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}

• Thank you mery much !!!! I'll try to make a mix between your first code and your second, to display only the section or subection once time. Once again thank you !!! – math45 Jan 30 '15 at 1:01
• I tried to do mix the two codes, but I failed (I'm kind of a noob in LaTeX : I use it since only 2 years). Could you please : -display the section as frametitle even if there is no subsection. -display (only) the subsection as frametitle (alone) only if there is no section just above, and if there is a section above then do exactly what your second code does. -not display several times the subsection : if the subsection is displayed one time (as frametitle or as framesubtitle, i.e displayed with a section or without) then not display it again. Thank for your help. – math45 Jan 30 '15 at 1:40
• @math45: The "second code" does both already. Your other request requires more work in terms of patches to \subsection. – Werner Jan 30 '15 at 7:23