5

How to automate numbering sections and subsections, so when their order changes, numbering is adjusted automatically using the following code

    \documentclass{beamer}
\usepackage{verbatim}
\setbeamertemplate{navigation symbols}{}

\setbeamercolor{author}{fg=blue}
\setbeamerfont{title}{size=\Large, series=\bfseries}
\setbeamerfont{subtitle}{size=\Large, series=\bfseries}
\setbeamerfont{author}{size=\footnotesize}
\setbeamerfont{date}{size=\small}

\setbeamertemplate{section page}
{
\begingroup
\begin{beamercolorbox}[sep=10pt,center,rounded=true,shadow=true]{section title}
    \usebeamerfont{section title}\insertsection\par
\end{beamercolorbox}
\endgroup
}

\setbeamertemplate{subsection page}
{
\begingroup
\begin{beamercolorbox}[sep=2pt,center,rounded=true,shadow=true]{section title}
    \usebeamerfont{section title}\insertsection\par
\end{beamercolorbox}
\vspace*{-1.pt}
\begin{beamercolorbox}[sep=6pt,center,rounded=true,shadow=true]{subsection title}
    \usebeamerfont{subsection title}\insertsubsection\par
\end{beamercolorbox}
\endgroup
}

    \setbeamerfont{section title}{size=\Large,series=\bfseries}
    \setbeamerfont{subsection title}{size=\large,series=\bfseries}

\begin{document}


\begin{frame}
\titlepage
\end{frame} 

\begin{frame}[t]
\frametitle{Table of Contents}
\tableofcontents
\end{frame}


\setbeamercolor{section title}{bg=blue!40}
\setbeamercolor{subsection title}{bg=blue!20}

\section{Immortal Games}
\frame{\sectionpage}

\subsection{Anderssen-Kieseritzky 1851}
\frame{\subsectionpage}

\subsection{Sliwa-Bronstein 1957}
\frame{\subsectionpage}

\subsection{Opera Game}
\frame{\subsectionpage}

\subsection{Alekhine`s Gun Game}
\frame{\subsectionpage}


\section{Famous Games}
\frame{\sectionpage}

\subsection{Byrne-Fischer}
\frame{\subsectionpage}

\subsection{Evergreen Game}
\frame{\subsectionpage}

\subsection{Deep Blue vs Kasparov}
\frame{\subsectionpage}

\subsection{Caruana vs Wojtkiewicz}
\frame{\subsectionpage}

\end{document}
0

2 Answers 2

8

Numbering of sections and subsection is automatic in latex - so I guess your question might be how to display these numbers?

  1. To display them in the toc:

    \setbeamertemplate{section in toc}[sections numbered]
    \setbeamertemplate{subsection in toc}[subsections numbered]
    
  2. on your section and subsection pages: you can add the numbers to your definition, e.g. \thesection.\thesubsection~\insertsubsection


\documentclass{beamer}

\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{section in toc}[sections numbered]
\setbeamertemplate{subsection in toc}[subsections numbered]

\setbeamercolor{author}{fg=blue}
\setbeamercolor{section title}{bg=blue!40}
\setbeamercolor{subsection title}{bg=blue!20}

\setbeamerfont{title}{size=\Large, series=\bfseries}
\setbeamerfont{subtitle}{size=\Large, series=\bfseries}
\setbeamerfont{author}{size=\footnotesize}
\setbeamerfont{date}{size=\small}
\setbeamerfont{section title}{size=\Large,series=\bfseries}
\setbeamerfont{subsection title}{size=\large,series=\bfseries}

\setbeamertemplate{section page}{%
    \begingroup
        \begin{beamercolorbox}[sep=10pt,center,rounded=true,shadow=true]{section title}
        \usebeamerfont{section title}\thesection~\insertsection\par
        \end{beamercolorbox}
    \endgroup
}

\setbeamertemplate{subsection page}{%
    \begingroup
        \begin{beamercolorbox}[sep=2pt,center,rounded=true,shadow=true]{section title}
        \usebeamerfont{section title}\thesection~\insertsection\par
        \end{beamercolorbox}
        \vspace*{-1.pt}
        \begin{beamercolorbox}[sep=6pt,center,rounded=true,shadow=true]{subsection title}
        \usebeamerfont{subsection title}\thesection.\thesubsection~\insertsubsection\par
        \end{beamercolorbox}
    \endgroup
}

\AtBeginSection[]{%
    \begin{frame}
        \sectionpage
    \end{frame}
}

\AtBeginSubsection[]{%
    \begin{frame}
        \subsectionpage
    \end{frame}
}

\begin{document}

\begin{frame}[t]
    \frametitle{Table of Contents}
    \tableofcontents
\end{frame}

\section{Immortal Games}
\subsection{Anderssen-Kieseritzky 1851}
\subsection{Sliwa-Bronstein 1957}
\subsection{Opera Game}
\subsection{Alekhine`s Gun Game}
\section{Famous Games}
\subsection{Byrne-Fischer}
\subsection{Evergreen Game}
\subsection{Deep Blue vs Kasparov}
\subsection{Caruana vs Wojtkiewicz}

\end{document}
4
  • Thank you. Can the subsection numbers be indented in TOC?
    – Hany
    Commented Nov 13, 2017 at 8:39
  • @Hany yes they can, but this is a new question Commented Nov 13, 2017 at 9:35
  • @samcarter_is_at_topanswers.xyz I have set the section counter to start numbering sections at 0 (via \setcounter{section}{-1} ). Using your method to display the section numbers in the toc still starts at 1. I have compiled the document multiple times and tried switching up the order of the code in the preamble but this didn't fix it. Do you have an idea what I'm missing? Commented Apr 1 at 9:32
  • @LinusDieLinse If you have a new question, please use the "Ask Question" button on the top right to ask a new question Commented Apr 1 at 9:59
1

(I didn't really look at your code)

You can use the following macros to access the section and subsection number:

\the\beamer@tocsectionnumber% section number
\thesubsection% subsection number

To detect unnumbered sections and subsections I use the following code:

\documentclass[]{beamer}


\makeatletter
\newif\ifStarredSubsection@
\newif\ifStarredSection@

\AtBeginSection[%>>>
  \global\StarredSection@true%
]{%
  \global\StarredSection@false%
}%<<<
\AtBeginSubsection[%>>>
  \global\StarredSubsection@true%
]{%
  \global\StarredSubsection@false%
}%<<<

\newcommand*{\mycursec}{%
    \ifStarredSection@%
    \else%
      \makebox[24pt][l]{\the\beamer@tocsectionnumber.}%
    \fi%
  \insertsection}
\newcommand*{\mycurssec}{%
    \ifStarredSubsection@%
    \else%
      \ifnum\c@subsection>0\relax%
        \makebox[24pt][l]{\the\beamer@tocsectionnumber.\thesubsection}%
      \fi%
    \fi%
  \insertsubsection}
\makeatother

\begin{document}

\section{foo}
\subsection{bar}

\begin{frame}{\mycursec}{\mycurssec}
  Frame with section and subsection as title and subtitle
\end{frame}


\end{document}
2
  • Your code did not work with mine. It displayed a new empty page, and a line in the next page with section/subsection numbers. Please consider involving your solution with my code.
    – Hany
    Commented Nov 13, 2017 at 8:29
  • @Hany why if the other code works for you?
    – Skillmon
    Commented Nov 13, 2017 at 14:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .