客观的
我正在尝试创建一个beamer
演示文稿(在 LyX 中),其中框架有条件地显示包含部分和子部分导航的标题,具体取决于 (a) 框架是否处于部分环境中以及 (b) 部分环境是否包含任何子部分。具体来说,应适用以下三个条件:
- 如果框架在部分之外:不显示标题(例如标题框架、目录框架)
- 如果框架内的节没有任何子节:仅显示节内所有框架的标题部分,其中包含节导航
- 如果框架内的节包含子节:则显示节内所有框架的标题部分(包含节导航)和子节导航部分
以下 MWE 应阐明期望的结果:
\documentclass{beamer}
% define colors
\definecolor{black}{RGB}{0,0,0}
\definecolor{darkblue}{RGB}{0,41,103}
\definecolor{lightblue}{RGB}{0,118,188}
\setbeamercolor*{section in head/foot}{fg=white, bg=lightblue}
\setbeamercolor*{section in head/foot shaded}{fg=white, bg=darkblue}
\setbeamercolor{subsection in head/foot}{fg=white, bg=darkblue}
% Create headline environment
\setbeamertemplate{headline}{%
\begin{beamercolorbox}[colsep=1.5pt]{upper separation line head}
\end{beamercolorbox}
\begin{beamercolorbox}{section in head/foot}
\vskip2pt\insertsectionnavigationhorizontal{\paperwidth}{}{\hfill\hfill}\vskip2pt
\end{beamercolorbox}%
\begin{beamercolorbox}[ht=10pt]{subsection in head/foot}%
\vskip2pt\insertsubsectionnavigationhorizontal{\paperwidth}{}{\hfill\hfill}\vskip2pt
\end{beamercolorbox}%
\begin{beamercolorbox}[colsep=1.5pt]{lower separation line head}
\end{beamercolorbox}
}
\begin{document}
\frame{\frametitle{Frame outside section environment}
This frame should display neither the section, nor the subsection portion of the headline
}
\section{Section 1 (no subsections)}
\frame{\frametitle{Frame in section environment with no subsections}
This frame should display the section portion of the headline, but not the subsection portion
}
\section{Section 2 (with subsection)}
\subsection{Subsection 2.1}
\frame{\frametitle{Frame in section environment (post-subsection)}
This frame should display both the section and subsection portions of the headline
}
\section{Section 3 (with)}
\frame{\frametitle{Frame in section environment (pre-subsection)}
This frame should display both the section and subsection portions of the headline
}
\subsection{Subsection 3.1}
\section{Section 4 (with subsections)}
\frame{\frametitle{Frame in section environment pre-subsection}
This frame should display both the section and subsection portions of the headline
}
\subsection{Subsection 4.1}
\frame{\frametitle{Frame in section environment post-subsection}
This frame should display both the section and subsection portions of the headline
}
\subsection{Subsection 4.2}
\end{document}
问题)
我怎样才能实现这种复杂的条件性?
我知道可以使用它来\ifx
检查某个环境是否已定义,或者元素是否位于某个环境中。例如,要检查是否处于部分环境中,可以使用:
\ifx\insertsection\empty
%do this if NOT in section environment%
\else
%do this if in section environment%
\fi
不幸的是,这只解决了上述三个条件中的第一个。后两个条件实际上需要代码“搜索”一个部分以查找定义的子部分,如果找到任何子部分,则计算结果为“真”,如果没有找到,则计算结果为“假”。我曾尝试通过尝试使用包\pretocmd
的选项etoolbox
和外部主题中使用的方法来解决这个问题miniframes
,但我担心我对所述方法的理解太少,以至于我实际上只是在黑暗中摸索。
我很感激任何关于如何解决这种情况的指导。
答案1
首先:该\section
指令(在标准 LaTeX 中)不会启动环境。但是,您有点预见到您需要根据上一条更改界面。至少我的解决方案需要将部分分组到环境内。
条件逻辑最终并没有那么复杂。假设你有两个条件\ifinsertsection
和\ifinsertsubsection
。然后它们简单地嵌套在标题设置中,就像
\setbeamertemplate{headline}{%
\ifinsertsection
<do section headline>
\ifinsertsubsection
<do subsection headline>
\fi
\fi
}
现在实际的工作仍然是切换开关。第一个相当简单,因为当一个部分开始时它必须是 true。为此,您可以说
\let\ltx@section\section
\def\section{\insertsectiontrue\ltx@section}
或者采用环境方法:
\newenvironment{customsectionenv}[2][]{%
\insertsectiontrue
\section[#1]{#2}
}{}
完整的解决方案将使用此方法的版本,不仅因为需要控制子部分切换,而且因为可以知道某个部分在何处结束。
现在,子部分切换将与部分切换位于同一位置,但只有当部分主体至少包含一个时才应切换\subsection
,如所要求。为此,您需要收集节环境的主体,将其存储到宏中(这包的用例environ
)并扫描环境中的替换文本是否\BODY
包含任何\subsection
,可以通过以下方式解决这使用该包的问题substr
:
\IfSubStringInString{\detokenize{\subsection}}{\detokenize\expandafter{\BODY}}
{\insertsubsectiontrue}{}
完整的代码如下
\documentclass[handout]{beamer}
%Define colors
\definecolor{black}{RGB}{0,0,0}
\definecolor{darkblue}{RGB}{0,41,103}
\definecolor{lightblue}{RGB}{0,118,188}
\setbeamercolor*{section in head/foot}{fg=white, bg=lightblue}
\setbeamercolor*{section in head/foot shaded}{fg=white, bg=darkblue}
\setbeamercolor{subsection in head/foot}{fg=white, bg=darkblue}
\usepackage{environ,substr}% required for the solution
%Create headline environment
\newif\ifinsertsection
\newif\ifinsertsubsection
\NewEnviron{beamersection}[2][]{%
\IfSubStringInString{\detokenize{\subsection}}{\detokenize\expandafter{\BODY}}
{\insertsubsectiontrue}{}
\insertsectiontrue
\if\relax\detokenize{#1}\relax
\section{#2}
\else
\section[#1]{#2}
\fi
\par\BODY
}
\setbeamertemplate{headline}{%
\ifinsertsection
\begin{beamercolorbox}[colsep=1.5pt]{upper separation line head}
\end{beamercolorbox}
\begin{beamercolorbox}{section in head/foot}
\vskip2pt\insertsectionnavigationhorizontal{\paperwidth}{}{\hfill\hfill}\vskip2pt
\end{beamercolorbox}%
\ifinsertsubsection
\begin{beamercolorbox}[ht=10pt]{subsection in head/foot}%
\vskip2pt\insertsubsectionnavigationhorizontal{\paperwidth}{}{\hfill\hfill}\vskip2pt
\end{beamercolorbox}%
\begin{beamercolorbox}[colsep=1.5pt]{lower separation line head}
\end{beamercolorbox}
\fi
\fi
\vspace{20pt}
}
\begin{document}
\frame{%
\frametitle{Frame outside section environment}
This frame should display neither the section, nor the subsection portion of the headline
}
\begin{beamersection}{Section 1 (no subsections)}
\frame{%
\frametitle{Frame in section environment with no subsections}
This frame should display the section portion of the headline, but not the subsection portion
}
\end{beamersection}
\begin{beamersection}{Section 2 (with subsection)}
\subsection{Subsection 2.1}
\frame{%
\frametitle{Frame in section environment (post-subsection)}
This frame should display both the section and subsection portions of the headline
}
\end{beamersection}
\begin{beamersection}{Section 3 (with subsections)}
\frame{%
\frametitle{Frame in section environment pre-subsection}
This frame should display both the section and subsection portions of the headline
}
\subsection{Subsection 3.1}
\frame{%
\frametitle{Frame in section environment post-subsection}
This frame should display both the section and subsection portions of the headline
}
\end{beamersection}
\end{document}
几点说明:
- 我取消了第四个案例/部分。它相当于第三个案例/部分。
- 如上所述,使用此解决方案时,各部分的用户界面会发生变化;各部分将被编码为
\begin{beamersection}[<opt. TOC title>]{<full title>} ... \end{beamersection}
- 输出图片由
\usepackage{pgfpages}\pgfpagesuselayout{2 on 1}[letterpaper,border shrink=5mm]