Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I use beamer to make presentations for my lectures (I despise PowerPoint). Each presentation represents one chapter of the textbook. Therefore, I need the section numbers in the beamer presentation to match the section numbers in the textbook chapter. However, since we sometimes skip sections, I must use \setcounter{section}{...}.

In the code displayed below, the headlines of the second and third slides display the section numbers correctly as 1 and 9, respectively. However, the table of contents on the first slide displays the section numbers incorrectly as 1 and 2. The table of contents seems to ignore the \setcounter{section}{...} command.

Looking in the toc file, this is indeed the case

\beamer@endinputifotherversion {3.26pt}
\beamer@sectionintoc {1}{First}{2}{0}{1}
\beamer@sectionintoc {9}{Ninth}{3}{0}{2}

The third and fifth parameters of the \beamer@sectionintoc macro have not been changed to reflect the non-sequential value of the section number.

Is there something else that needs to be set in order for beamer to display the correct section number in the table of contents?

\documentclass{beamer}
% This gets rid of the font warnings
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\setbeamertemplate{section in toc}[sections numbered]

\setbeamercolor{headline colour}{fg=blue,bg=yellow}
\setbeamertemplate{headline}
{
   \leavevmode%
   \hbox{%
      \begin{beamercolorbox}[wd=\paperwidth,ht=4ex,dp=1.5ex]{headline colour}%
      \raggedright
      \hspace*{2em}%
      {\small%
         \ifx\insertsection\empty%
            \relax%
         \else
            Section \#\thesection\ \insertsectionhead%
         \fi
      }%
      \hspace*{2em}%
      \end{beamercolorbox}%
   }%
}

\begin{document}

\begin{frame}
   {Table of Contents}
   \tableofcontents
\end{frame}

\section{First}
\begin{frame}
   Something in the First section.
\end{frame}

\setcounter{section}{8}
\section{Ninth}
\begin{frame}
   Another thing in the Ninth section.
\end{frame}

\end{document}
share|improve this question
    
Welcome to TeX.sx! –  barbara beeton Mar 25 '13 at 14:31

1 Answer 1

up vote 6 down vote accepted

The beamer class uses an independent counter for the table of contents, so it's not sufficient to set the section counter, but also the TeX counter \beamer@tocsectionnumber (which must be operated on with primitive commands). I suggest using a command for setting directly the requested number:

\setnextsection{9}

which is easier than setting it one less than wanted.

\documentclass{beamer}
% This gets rid of the font warnings
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\makeatletter
\newcommand{\setnextsection}[1]{%
  \setcounter{section}{\numexpr#1-1\relax}%
  \beamer@tocsectionnumber=\numexpr#1-1\relax\space}
\makeatother

\setbeamertemplate{section in toc}[sections numbered]

\setbeamercolor{headline colour}{fg=blue,bg=yellow}
\setbeamertemplate{headline}
{
   \leavevmode%
   \hbox{%
      \begin{beamercolorbox}[wd=\paperwidth,ht=4ex,dp=1.5ex]{headline colour}%
      \raggedright
      \hspace*{2em}%
      {\small%
         \ifx\insertsection\empty%
            \relax%
         \else
            Section \#\thesection\ \insertsectionhead%
         \fi
      }%
      \hspace*{2em}%
      \end{beamercolorbox}%
   }%
}

\begin{document}

\begin{frame}
   {Table of Contents}
   \tableofcontents
\end{frame}

\section{First}
\begin{frame}
   Something in the First section.
\end{frame}

\setnextsection{9}
\section{Ninth}
\begin{frame}
   Another thing in the Ninth section.
\end{frame}

\end{document}

enter image description here

share|improve this answer
    
Thanks. And I completely agree that it makes more sense to set the section number to the value you want it to be rather than one less than what you want it to be –  Nicholas Hill Mar 25 '13 at 14:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.