[Tex/LaTex] Beamer Warsaw theme: Reduce the number of subsections visible in the navigation bar

beamernavigationspacing

I give a lecture with 10 sections and maybe 5-10 subsections.
Using the Warsaw theme would make the available space too small.

Is there a way to make the navigation bar show:

  • only the last section, current section and the next two sections?
  • only the last subsection, current subsection and the next two subsections?

If there is not an option, one may have to change the \insertsubsectionnavigation-function.

Maybe something like

\insertsubsectionnavigation["\thissectionnumber-1" to "\thissectionnumber+2"] 

The problem is: I don't understand the way this function works.

Does anyone have an idea or a link to a similar topic?

Here is an example of how it should NOT look:

\documentclass{beamer}
\usetheme{Warsaw}
\begin{document}
    \section{Section 1}
    \section{Section 2}
    \section{Section 3}
    \section{Section 4}
        \subsection{SubSection a}\begin{frame}Test\end{frame}
        \subsection{SubSection b}\begin{frame}Test\end{frame}
        \subsection{SubSection c}\begin{frame}Test\end{frame}
        \subsection{SubSection d}\begin{frame}Test\end{frame}
        \subsection{SubSection e}\begin{frame}Test\end{frame}
        \subsection{SubSection f}\begin{frame}Test\end{frame}
        \subsection{SubSection g}\begin{frame}Test\end{frame}
        \subsection{SubSection h}\begin{frame}Test\end{frame}
        \subsection{SubSection i}\begin{frame}Test\end{frame}
    \section{Section 5}
    \section{Section 6}
    \section{Section 7}\begin{frame}Test\end{frame}
\end{document}

A slide would look like:
normal style

I want it to look like:
enter image description here

Best Answer

This is an extension of @GonzaloMedina answer from Only 3 items in navigation bar, is that possible? that covers subsections too (long Beamer code follows).

\documentclass{beamer}
\usetheme{Warsaw}

\newcounter{prevsection}
\newcounter{nextsection}
\newcounter{prevsubsection}
\newcounter{nextsubsection}


\newcommand\prevsection{}
\newcommand\nextsection{}
\newcommand\prevsubsection{}
\newcommand\nextsubsection{}

\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}%
}%

\def\beamer@subsection[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture%
    \refstepcounter{subsection}%
    \beamer@ifempty{#2}{\long\def\subsecname{#1}\long\def\lastsubsection{#1}}
    {%
      \long\def\subsecname{#2}%
      \long\def\lastsubsection{#1}%
      \addtocontents{toc}{\protect\beamer@subsectionintoc{\the\c@section}{\the\c@subsection}{#2}{\the\c@page}{\the\c@part}{\the\beamer@tocsectionnumber}}%
    }%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \addtocontents{nav}{%
      \protect\headcommand{\protect\beamer@subsectionentry{\the\c@part}{\the\c@section}{\the\c@subsection}{\the\c@page}{\lastsubsection}}%
      \protect\headcommand{\protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\the\beamer@tempcount}}%
    }%
    \beamer@subsectionstartpage=\c@page%
    \edef\subsectionlink{{Navigation\the\c@page}{\noexpand\subsecname}}%
    \def\insertsubsection{\expandafter\hyperlink\subsectionlink}%
    \def\insertsubsubsection{}%
    \def\insertsubsectionhead{\hyperlink{Navigation\the\c@page}{#1}}%
    \def\insertsubsubsectionhead{}%
    \Hy@writebookmark{\the\c@subsection}{#2}{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}{3}{toc}%
    \hyper@anchorstart{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}\hyper@anchorend%
    \beamer@ifempty{#2}{\beamer@atbeginsubsections}{\beamer@atbeginsubsection}%
  \fi%
  \beamer@resumemode
  \setcounter{prevsubsection}{\thesubsection}%
    \setcounter{nextsubsection}{\thesubsection}%
    \addtocounter{prevsubsection}{-1}%
    \gdef\prevsubsection{\csname subsection\romannumeral\theprevsubsection \endcsname}%
     \addtocounter{nextsubsection}{1}%
    \renewcommand\nextsubsection{\csname subsection\romannumeral\thenextsubsection \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{%
      \ifnum\thesubsection=1 \else%
        \vfill{\color{fg!40!bg}\prevsubsection}%
      \fi%
        \vfill\insertsubsectionhead%
      \ifnum\thesubsection=\beamer@subsectionmax \else%
        \vfill{\color{fg!40!bg}\nextsubsection}%
     \fi\vfill%
     }%
    \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\subsectioni{sub 1}
\newcommand\subsectionii{sub 2}
\newcommand\subsectioniii{sub 3}
\newcommand\subsectioniv{sub 4}

\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}
\subsection{sub 1}
\begin{frame}test\end{frame}
\subsection{sub 2}
\begin{frame}test\end{frame}
\subsection{sub 3}
\begin{frame}test\end{frame}
\subsection{sub 4}
\begin{frame}test\end{frame}

\end{document}

enter image description here