Beamer 中的章节编号(“CambridgeUS”主题)

Beamer 中的章节编号(“CambridgeUS”主题)

有人知道如何让乳胶在标题中显示某个部分的编号吗?

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

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

先感谢您。

答案1

对于那些使用外部主题的主题infolines,例如CambridgeUS,您可以将以下几行添加到序言中:

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

编辑:在下面的例子中,我定义了一个\Switch命令,允许打开/关闭标题中各部分的编号;最初,它被设置为0(关闭编号),但重新定义为1将打开编号;可以根据需要多次重新定义该命令:

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

答案2

@Gonzalo Medina 的解决方案需要稍微修改一下。如果我们也想使用子节编号进行管理,我们可以按以下方式修改代码:

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

相关内容