# Only 3 items in navigation bar, is that possible?

I saw a beamer presentation that shows only 3 items in navigation bar (the last one, the current -highlighted- and the next). How is that possible? I am using Warsaw theme, and I have (until now) 7 sections, so the navigation bar is too long…

-

## 2 Answers

Here's a possible solution; the idea is to have two auxiliary commands \prevsection and \nextsection to hold the names of the previous and next section to the current section; those commands will be used in the headline template to display the required information instead of the usual navigation bar.

The updating of \prevsection, \nextsection is done internally using a redefinition of \beamer@section.

All that you have to do is to fill in the mandatory argument for the commands \sectioni, \sectionii, ... ,\sectionviii with the information to be displayed in the navigation bar.

\documentclass{beamer}
\usetheme{Warsaw}

\newcounter{prevsection}
\newcounter{nextsection}

\newcommand\prevsection{}
\newcommand\nextsection{}

\makeatletter
\long\def\beamer@section[#1]#2{%
\beamer@savemode%
\mode<all>%
\ifbeamer@inlecture%
\refstepcounter{section}%
\beamer@ifempty{#2}%
{\long\def\secname{#1}\long\def\lastsection{#1}}%
{\global\advance\beamer@tocsectionnumber by 1\relax%
\long\def\secname{#2}%
\long\def\lastsection{#1}%
\addtocontents{toc}{\protect\beamer@sectionintoc{\the\c@section}{#2}{\the\c@page}{\the\c@part}%
{\the\beamer@tocsectionnumber}}}%
{\let\\=\relax\xdef\sectionlink{{Navigation\the\c@page}{\noexpand\secname}}}%
\beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
\beamer@ifempty{#1}{}{%
\addtocontents{nav}{\protect\headcommand{\protect\sectionentry{\the\c@section}{#1}{\the\c@page}{\secname}{\the\c@part}}}%
\addtocontents{nav}{\protect\headcommand{\protect\beamer@sectionpages{\the\beamer@sectionstartpage}{\the\beamer@tempcount}}}%
\addtocontents{nav}{\protect\headcommand{\protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\the\beamer@tempcount}}}%
}%
\beamer@sectionstartpage=\c@page%
\beamer@subsectionstartpage=\c@page%
\def\insertsection{\expandafter\hyperlink\sectionlink}%
\def\insertsubsection{}%
\def\insertsubsubsection{}%
\def\insertsectionhead{\hyperlink{Navigation\the\c@page}{#1}}%
\def\insertsubsectionhead{}%
\def\insertsubsubsectionhead{}%
\def\lastsubsection{}%
\Hy@writebookmark{\the\c@section}{\secname}{Outline\the\c@part.\the\c@section}{2}{toc}%
\hyper@anchorstart{Outline\the\c@part.\the\c@section}\hyper@anchorend%
\beamer@ifempty{#2}{\beamer@atbeginsections}{\beamer@atbeginsection}%
\fi%
\beamer@resumemode
\setcounter{prevsection}{\thesection}%
\setcounter{nextsection}{\thesection}%
\addtocounter{prevsection}{-1}%
\gdef\prevsection{\csname section\romannumeral\theprevsection \endcsname}%
\addtocounter{nextsection}{1}%
\renewcommand\nextsection{\csname section\romannumeral\thenextsection \endcsname}%
}%

\setbeamertemplate{headline}
{%
\leavevmode%
\@tempdimb=2.4375ex%
\ifnum\beamer@subsectionmax<\beamer@sectionmax%
\multiply\@tempdimb by\beamer@sectionmax%
\else%
\multiply\@tempdimb by\beamer@subsectionmax%
\fi%
\ifdim\@tempdimb>0pt%
\advance\@tempdimb by 1.125ex%
\begin{beamercolorbox}[wd=.5\paperwidth,ht=\@tempdimb,right,rightskip=1em]{section in head/foot}%
\vbox to \@tempdimb{%
\ifnum\thesection=1 \else%
\vfill{\color{fg!40!bg}\prevsection}%
\fi%
\vfill\insertsectionhead%
\ifnum\thesection=\beamer@sectionmax \else%
\vfill{\color{fg!40!bg}\nextsection}%
\fi\vfill%
}%
\end{beamercolorbox}%
\begin{beamercolorbox}[wd=.5\paperwidth,ht=\@tempdimb]{subsection in head/foot}%
\vbox to\@tempdimb{\vfil\insertsubsectionnavigation{.5\paperwidth}\vfil}%
\end{beamercolorbox}%
\fi%
}%
\makeatother

% Here you put the names that will go in the navigation bar
\newcommand\sectioni{test one}
\newcommand\sectionii{test two}
\newcommand\sectioniii{test three}
\newcommand\sectioniv{test four}
\newcommand\sectionv{test five}
\newcommand\sectionvi{test six}
\newcommand\sectionvii{test seven}
\newcommand\sectionviii{test eight}

\begin{document}

\section{test one}
\begin{frame}test\end{frame}

\section{test two}
\begin{frame}test\end{frame}

\section{test three}
\begin{frame}test\end{frame}

\section{test four}
\begin{frame}test\end{frame}

\section{test five}
\begin{frame}test\end{frame}

\end{document}


Some images of the headlines:

-
Nothing to say… just perfect!! Thank you!!! For me, this question has a positive answer (I don't know if this question must/should be closed or not, and how to if necessary) – F_Jofre_A May 8 '12 at 20:56
@F_Jofre_A: You're welcome! No need to close the question; up-voting good responses and marking the best one as accepted (as you did) is the standard procedure around here ;-) – Gonzalo Medina May 8 '12 at 21:08
Thank you for this very useful trick. How can I remove vertical spaces between section names ? – Antoine Pinsard May 10 '14 at 11:56
I managed to do it by editing tempdimb value. – Antoine Pinsard May 10 '14 at 12:03

I derived a simpler solution from this code: Beamer-Warsaw: Reduced No. of subsections in title

Except for the first page I get the three section titles right. If you dont mind only the first two titles showing up, just comment out the headline-command code for the first page.

\documentclass{beamer}
\usetheme{Warsaw}
\makeatletter
% A new section definition to automatically set labels with the name: sec:sectionnumber
\let\oldsection\section
\renewcommand{\section}[1]{\oldsection{#1}\label{sec:\thesection}}
% A new subsection definition to automatically set labels with the name: sec:sectionnumber.subsectionnumer
\let\oldsubsection\subsection
\renewcommand{\subsection}[1]{\oldsubsection{#1}\label{sec:\thesection.\thesubsection}}
%init some calculators
\newcounter{calculator}
\newcounter{othercalculator}
\newcounter{calcmaxsec}
\newcounter{calcmaxsubsec}

\newcommand{\calcsubsection}[1]%procedure to get the name of section  thissection+i
{
\setcounter{calculator}{\thesubsection}\addtocounter{calculator}{#1}
\@ifundefined{r@sec:\thesection.\thecalculator}{}{\color{fg!40!bg}
\nameref{sec:\thesection.\thecalculator}}
}
\newcommand{\calcsection}[1]%procedure to get the name of subsection  thissection.thissubsection+i
{
\setcounter{othercalculator}{\thesection\addtocounter{othercalculator}{#1}
\@ifundefined{r@sec:\thesection}{}{\color{fg!40!bg}\nameref{sec:\theothercalculator}}
}
\newcommand{\Testframe}%some testframe definition
{
\begin{frame}
The Section: \thesection\\
The Subsection: \thesubsection\\
The Calculator: \thecalculator\\
The section calculator: \theothercalculator\\
Count\thesection
\end{frame}
}
%Defining the layout%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setbeamertemplate{headline}
{%
\leavevmode%
\@tempdimb=1.5ex%
\ifnum\beamer@subsectionmax<\beamer@sectionmax%
\multiply\@tempdimb by\beamer@sectionmax%
\else%
\multiply\@tempdimb by\beamer@subsectionmax%
\fi%
\ifdim\@tempdimb>0pt%
\advance\@tempdimb by 1.1ex%
\begin{beamercolorbox}[wd=.5\paperwidth,ht= \@tempdimb,right,rightskip=1em]{section in head/foot}%
\vbox to \@tempdimb{%
\setcounter{calcmaxsec}{\thesection}\addtocounter{calcmaxsec}{-\beamer@sectionmax}
%this gives the first and the last headlines
\ifnum\thesection=1\vfill{\calcsection{+2}}\fi%
\ifnum\thesection=\beamer@sectionmax\vfill{\calcsection{-2}}\fi%
%this tells where the last and nextsection is
\ifnum\thesection>1\vfill{\calcsection{-1}}\fi%
\ifnum\thesection>0\vfill\insertsectionhead\fi
\ifnum\beamer@sectionmax>1{\ifnum\thecalcmaxsec<0  \vfill{\calcsection{1}}\fi}\fi
\vfill%
}%
\end{beamercolorbox}%
\begin{beamercolorbox}[wd=.5\paperwidth,ht=\@tempdimb]{subsection in head/foot}%
\vbox to \@tempdimb{%
\setcounter{calcmaxsubsec}{\thesubsection}\addtocounter{calcmaxsubsec}{-\beamer@subsectionmax}
\ifnum\thesubsection>1 \vfill{\calcsubsection{-1}}\fi%
\ifnum\thesubsection>0 \vfill\insertsubsectionhead\fi
\ifnum\beamer@subsectionmax>1{\ifnum\thecalcmaxsubsec<0\vfill{\calcsubsection{1}}\fi}\fi
\vfill%
}%
\end{beamercolorbox}%
\fi%
}%
\makeatother %%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\section{test one}\Testframe
\subsection{sub 1}\Testframe
\subsection{sub 2}\Testframe
\subsection{sub 3}\Testframe
\subsection{sub 4}\Testframe
\subsection{sub 5}\Testframe
\subsection{sub 6}\Testframe
\subsection{sub 7}\Testframe
\section{test two}\Testframe
\subsection{sub 1}\Testframe
\subsection{sub 2}\Testframe
\section{test three}\Testframe
\subsection{sub 1}\Testframe
\subsection{sub 2}\Testframe
\subsection{sub 3}\Testframe
\section{test four}\Testframe
\subsection{sub 1}\Testframe
\subsection{sub 2}\Testframe
\subsection{sub 3}\Testframe
\subsection{sub 4}\Testframe
\section{test five}\Testframe
\subsection{sub 1}\Testframe
\section{test six}\Testframe
\subsection{sub 1}\Testframe
\end{document}


btw is there some way to make this code-preview-window smaller?

-
If there is "still some issue", does this answer the question? – Werner May 6 at 20:03
This way of answering the question doesn't imply setting extra section-titles. You can still use the usual section-command. Which makes it easier. In addition this answer also includes the subsections in the precedure, which the previous answer did not. However in practise especially for short presentations it might be a good idea to comment out the first and last headline calculators. – 5th May 7 at 10:12