[Tex/LaTex] Beamer: TOC only once before sections with subsections

beamertable of contents

My presentation consist of multiple sections. Some sections consist of subsection and some not. I want beamer to display the TOC before every subsection and before every section which consists of no other subsection.

Currently, i write in the preamble:

\AtBeginSection[] {
    \begin{frame}<beamer>
    \frametitle{Inhalt} %
    \tableofcontents[currentsection]  
    \end{frame}
}

\AtBeginSubsection[] {
    \begin{frame}<beamer>
    \frametitle{Inhalt} %
    \tableofcontents[currentsubsection]  
    \end{frame}
}

As a result, if a section consist of subsecitons beamer displays the TOC twice before the first subsection in a section, one time for the section and one time for the subsection. That is ugly in my opinion. What can i do?

Best Answer

You can very simply use TeX conditionals. The only drawback (or feature) is that for section with subsections, in the first ToC does not "shade" the latter subsections. Basically, we tell LaTeX: At section, make ToC and remember false. At subsection, if you see true make ToC, and remember true.

\documentclass{beamer}

\newif\iflattersubsect

\AtBeginSection[] {
    \begin{frame}<beamer>
    \frametitle{Inhalt} %
    \tableofcontents[currentsection]  
    \end{frame}
    \lattersubsectfalse
}

\AtBeginSubsection[] {
    \iflattersubsect
    \begin{frame}<beamer>
    \frametitle{Inhalt} %
    \tableofcontents[currentsubsection]  
    \end{frame}
    \fi
    \lattersubsecttrue
}

\title{TITLE}
\author{AUTHOR}

\begin{document}

\begin{frame}\maketitle\end{frame}

\section{SECT I}
\subsection{I.1}
\begin{frame}I.1\end{frame}

\subsection{I.2}
\begin{frame}I.2\end{frame}

\section{SECT II}
\begin{frame}II frame 1\end{frame}
\begin{frame}II frame 2\end{frame}

\section{SECT III}
\subsection{III.1}
\begin{frame}III.1\end{frame}

\subsection{III.2}
\begin{frame}III.2\end{frame}

\end{document}