josephwright / beamer

A LaTeX class for producing presentations and slides
Other
1.4k stars 141 forks source link

Showing previous section after \part{} #615

Open weimiao1993 opened 4 years ago

weimiao1993 commented 4 years ago

I'm using the following code to create lecture notes. Page 1 of Lecture 2 is part of the previous section i.e., subsection 2.2. How can I show subsection 2.2 in the sidebar of Lecture 2, while keeping all the section numbers organized?

\documentclass{beamer}

\useoutertheme[width = 1.5cm, height = 1cm]{sidebar}

\makeatletter
\renewcommand<>{\part}{\alt#1{\@dblarg\beamer@part}{\beamer@gobbleoptional}}
\long\def\beamer@part[#1]#2{%
  \beamer@savemode
  \mode<all>%
  \ifbeamer@inlecture
    \refstepcounter{part}%
    \def\beamer@partname{#2}%
    \def\beamer@partnameshort{#1}%
    \addtocontents{nav}{\protect\headcommand{\protect\partentry{#2}{\the\c@part}}}%
    \xdef\partlink{{Navigation\the\c@page}{\noexpand\beamer@partname}}%
    \xdef\partlinkshort{{Navigation\the\c@page}{\noexpand\beamer@partnameshort}}%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \addtocontents{nav}{\protect\headcommand{%
        \protect\beamer@partpages{\the\beamer@partstartpage}{\the\beamer@tempcount}}}%
    \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@partstartpage=\c@page
    \beamer@sectionstartpage=\c@page
    \beamer@subsectionstartpage=\c@page
%    \setcounter{subsection}{0}%
    \protected\def\insertsection{}%
    \protected\def\insertsubsection{}%
    \protected\def\insertsubsubsection{}%
    \protected\def\insertsectionhead{}%
    \protected\def\insertsubsectionhead{}%
    \protected\def\insertsubsubsectionhead{}%
    \def\lastsubsection{}%
    \protected\def\insertpart{\expandafter\hyperlink\partlink}%
    \Hy@writebookmark{\the\c@part}{#1}{Outline\the\c@part}{1}{toc}%
    \hyper@anchorstart{Outline\the\c@part}\hyper@anchorend
    \beamer@atbeginpart
  \fi%
  \beamer@resumemode}%
\makeatother

\begin{document}

%=================== Lecture 1 ======================
\part{}
\section{section 1}
\subsection{subsection 1.1}
\subsection{subsection 1.2}
\section{section 2}
\subsection{subsection 2.1}
\subsection{subsection 2.2}
\begin{frame}{Lecture 1 -- Page 1}
     The section counter in Part \thepart is \thesection.\\
     The subsection counter in Part \thepart is \thesubsection.
\end{frame}

\part{}

\begin{frame}{Lecture 2 -- Page 1}
    The section counter in Part \thepart is \thesection.\\
     The subsection counter in Part \thepart is \thesubsection.
\end{frame}

\subsection{subsection 2.3}
\begin{frame}{Lecture 3 -- Page 2}
    The section counter in Part \thepart is \thesection.\\
     The subsection counter in Part \thepart is \thesubsection.
\end{frame}
\end{document}

image

samcarter commented 4 years ago

With \part only the navigation from the current part is shown in the sidebar and subsection 2.2 belongs to the previous part. As a workaround you could repeat the subsection (some links may go havoc ... )

\documentclass{beamer}

\useoutertheme[width = 1.5cm, height = 1cm]{sidebar}

\makeatletter
\renewcommand<>{\part}{\alt#1{\@dblarg\beamer@part}{\beamer@gobbleoptional}}
\long\def\beamer@part[#1]#2{%
  \beamer@savemode
  \mode<all>%
  \ifbeamer@inlecture
    \refstepcounter{part}%
    \def\beamer@partname{#2}%
    \def\beamer@partnameshort{#1}%
    \addtocontents{nav}{\protect\headcommand{\protect\partentry{#2}{\the\c@part}}}%
    \xdef\partlink{{Navigation\the\c@page}{\noexpand\beamer@partname}}%
    \xdef\partlinkshort{{Navigation\the\c@page}{\noexpand\beamer@partnameshort}}%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \addtocontents{nav}{\protect\headcommand{%
        \protect\beamer@partpages{\the\beamer@partstartpage}{\the\beamer@tempcount}}}%
    \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@partstartpage=\c@page
    \beamer@sectionstartpage=\c@page
    \beamer@subsectionstartpage=\c@page
%    \setcounter{subsection}{0}%
    \protected\def\insertsection{}%
    \protected\def\insertsubsection{}%
    \protected\def\insertsubsubsection{}%
    \protected\def\insertsectionhead{}%
    \protected\def\insertsubsectionhead{}%
    \protected\def\insertsubsubsectionhead{}%
    \def\lastsubsection{}%
    \protected\def\insertpart{\expandafter\hyperlink\partlink}%
    \Hy@writebookmark{\the\c@part}{#1}{Outline\the\c@part}{1}{toc}%
    \hyper@anchorstart{Outline\the\c@part}\hyper@anchorend
    \beamer@atbeginpart
  \fi%
  \beamer@resumemode}%
\makeatother

\begin{document}

%=================== Lecture 1 ======================
\part{}
\section{section 1}
\subsection{subsection 1.1}
\subsection{subsection 1.2}
\section{section 2}
\subsection{subsection 2.1}
\subsection{subsection 2.2}
\begin{frame}{Lecture 1 -- Page 1}
     The section counter in Part \thepart is \thesection.\\
     The subsection counter in Part \thepart is \thesubsection.
\end{frame}

\part{}
\addtocounter{subsection}{-1}
\subsection{subsection 2.2}

\begin{frame}{Lecture 2 -- Page 1}
    The section counter in Part \thepart is \thesection.\\
     The subsection counter in Part \thepart is \thesubsection.
\end{frame}

\subsection{subsection 2.3}
\begin{frame}{Lecture 3 -- Page 2}
    The section counter in Part \thepart is \thesection.\\
     The subsection counter in Part \thepart is \thesubsection.
\end{frame}

\section{section 3}
\subsection{subsection 3.1}
\begin{frame}{Lecture 3 -- Page 3}
    The section counter in Part \thepart is \thesection.\\
     The subsection counter in Part \thepart is \thesubsection.
\end{frame}
\end{document}
weimiao1993 commented 4 years ago

Thanks for the reply! If I manipulate the counter manually, then I have to type the subsection name again. Is there any way that I can automate this process?

Or is there any command other than \part that I can use? I'm looking for a command that displays the table of contents for this lecture, and tracks which section/subsection that the first page of this lecture belongs to.

samcarter commented 4 years ago

You can avoid retyping the subsection name by using something like

\part{}
\addtocounter{subsection}{-1}
\edef\foo{\subsecname}
\subsection{\foo}

However the problem with automating is that at the time the part command is used, it does not yet know what will come next. A new section might start right after it, then repeating the subsection would be the wrong thing to do.

weimiao1993 commented 4 years ago

You can avoid retyping the subsection name by using something like

\part{}
\addtocounter{subsection}{-1}
\edef\foo{\subsecname}
\subsection{\foo}

However the problem with automating is that at the time the part command is used, it does not yet know what will come next. A new section might start right after it, then repeating the subsection would be the wrong thing to do.

Thanks for the reply! I tried to do this, it seems that the sidebar is working well. But I found the bookmark for the subsection connection two lectures was misplaced. Besides, I got the warning `Package hyperref Warning: Difference (2) between bookmark levels is greater(hyperref) than one, level fixed on input line 62.'. Here is what I see from the PDF. The bookmark of subsection 2.2 in Lecture 2 is supposed to be at level 2. How should I fix it?

image

weimiao1993 commented 4 years ago

Another follow up question, can I display the name of current section in the sidebar, while keeping the subsection numbering, after the command \part{}? Using the current code, the sidebar of Lecture 2 displays as subsec 2.2 \\ subsec 2.3 \\ Section 3 \\ subsec 3.1'. I would like some customization so that the sidebar of Lecture 2 becomesSection 2 \ subsec 2.2 \ subsec 2.3 \ Section 3 \ subsec 3.1'.