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.

Does anybody know how to make latex put the number for a section presented in header?

\documentclass{beamer}
%\usetheme{Warsaw}
\usetheme{CambridgeUS}
\begin{document}

\section{Section a}
\begin{frame}
Test frame
\end{frame}
\end{document} 

Thank you in advance.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

For those themes using the infolines outer theme, such as CambridgeUS, you can add the following lines to the preamble:

\setbeamertemplate{headline}
{
  \leavevmode%
  \hbox{%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,right]{section in head/foot}%
    \usebeamerfont{section in head/foot}\thesection\ \insertsectionhead\hspace*{2ex}
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,left]{subsection in head/foot}%
    \usebeamerfont{subsection in head/foot}\hspace*{2ex}\insertsubsectionhead
  \end{beamercolorbox}}%
  \vskip0pt%
}

EDIT: In the following example I defined a \Switch command that allows to turn on/off the numbering of the sections in the headline; initially, it is set to 0 (turn off numbering), but redefining it to be 1 will turn on the numbering; the command can be redefined as many times as required:

\documentclass{beamer}
\usepackage{ifthen}
\usetheme{CambridgeUS}

\newcommand\Switch{0}
\newcommand\SecInHead{%
  \ifthenelse{\equal{\Switch}{1}}
    {\thesection.}{}}

\setbeamertemplate{headline}
{
  \leavevmode%
  \hbox{%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,right]{section in head/foot}%
    \usebeamerfont{section in head/foot}\SecInHead\insertsectionhead\hspace*{2ex}
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,left]{subsection in head/foot}%
    \usebeamerfont{subsection in head/foot}\hspace*{2ex}\insertsubsectionhead
  \end{beamercolorbox}}%
  \vskip0pt%
}
\begin{document}

\begin{frame}
Test frame with un-numbered section
\end{frame}

\renewcommand\Switch{1}
\section{Section a}
\begin{frame}
Test frame with numbered section
\end{frame}

\renewcommand\Switch{0}
\begin{frame}
Test frame with un-numbered section
\end{frame}

\end{document} 
share|improve this answer
    
Thank you very much! It works. Minor change "\thesection." instead of "\thesection" makes exactly that what I want. –  godsdog May 29 '11 at 16:03
    
I am sorry very much. There is a bug. Section "0." appeared in title frame. Are there any ideas how to manage with it? –  godsdog May 29 '11 at 16:30
    
To place @Gonzalo Medina's piece of code AFTER the title frame is enough to solve the problem with "0." in header on title frame. –  godsdog May 29 '11 at 16:57
    
@Andriy Andrusyk: I presume that the undesired number appears for frames not belonging to any section? If this is so, a quick fix would be to redefine \thesection locally for those frames: {\renewcommand\thesection{} \begin{frame} Test frame \end{frame} }. Notice the grouping { and } braces to keep the redefinition local. –  Gonzalo Medina May 29 '11 at 16:57
1  
@Andriy Andrusyk: the problem with that redefinition is that now the dot will also appear if you label/cross-reference sections, and that might not be desirable. –  Gonzalo Medina May 30 '11 at 11:59
show 4 more comments

The solution of @Gonzalo Medina have to be a bit modified. If we want to manage with subsection number too we can modify the code in the following way:

\documentclass{beamer}
\usepackage{ifthen}
\usetheme{CambridgeUS}

\newcommand\Switch{0}
\newcommand\SecInHead{%
  \ifthenelse{\equal{\Switch}{1}}
    {\thesection.}{}}
\newcommand\SubSecInHead{%
  \ifthenelse{\equal{\Switch}{1}}
    {\thesubsection. }{}}
\newcommand\RealSubSecInHead{%
  \ifthenelse{\equal{\thesubsection}{0}}
    {}{\SecInHead\SubSecInHead}}

\setbeamertemplate{headline}
{
  \leavevmode%
  \hbox{%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,right]{section in head/foot}%
  \usebeamerfont{section in head/foot}\SecInHead\ \insertsectionhead\hspace*{2ex}
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.5\paperwidth,ht=2.25ex,dp=1ex,left]{subsection in head/foot}%
  \usebeamerfont{subsection in head/foot}\hspace*{2ex}\RealSubSecInHead\insertsubsectionhead
  \end{beamercolorbox}}%
  \vskip0pt%
}

\begin{document}

\begin{frame}
Test frame with un-numbered section
\end{frame}

\renewcommand\Switch{1}
\section{Section a}
\begin{frame}
Test frame with numbered section
\end{frame}

\renewcommand\Switch{0}
\begin{frame}
Test frame with un-numbered section
\end{frame}

\renewcommand\Switch{1}
\section{Section a2}
\begin{frame}
Test frame with numbered section again
\end{frame}

\subsection{Subsection a2.1}
\begin{frame}
Test frame with numbered subsection again
\end{frame}


\end{document} 
share|improve this answer
add comment

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.