在 beamer 中修补 \insertshorttitle

在 beamer 中修补 \insertshorttitle

在此回答@cfr 向我展示了如何在 beamer 中保留短标题颜色,同时允许超链接工作。他的解决方案是修改命令\insertshorttitle(在 中定义beamerbasetitle.sty),如下所示:

\makeatletter
\renewcommand\insertshorttitle[1][]{%
  \beamer@setupshort{#1}%
  \let\thanks=\@gobble%
  \ifnum\c@page=1%
  \hyperlinkpresentationend{\beamer@insertshort{\usebeamercolor*[fg]{title in head/foot}\beamer@shorttitle}}%
  \else%
  \hyperlinkpresentationstart{\beamer@insertshort{\usebeamercolor*[fg]{title in head/foot}\beamer@shorttitle}}%
  \fi}
\makeatother

这很好用,但我想知道是否可以\insertshorttitle使用 来修补命令etoolbox。我试过

\makeatletter 
\patchcmd{\insertshorttitle}{\beamer@shorttitle}{\usebeamercolor*[fg]{title in head/foot}\beamer@shorttitle}{}{} 
\makeatother

但它不起作用。etoolbox 建议该命令无法修补,因为“该宏可能在与当前类别代码制度不同的类别代码制度下定义”。

所以我的问题是:我能以某种方式修补该命令吗?这是一个完整的 MWE:

\documentclass[leqno]{beamer}

\usetheme{Madrid}
\usecolortheme{whale}

\usepackage{lmodern}
\usepackage{etoolbox}
\hypersetup{colorlinks=true, allcolors=blue}

\makeatletter 
\patchcmd{\insertshorttitle}{\beamer@shorttitle}{\usebeamercolor*[fg]{title in head/foot}\beamer@shorttitle}{}{} 
\makeatother

\title[Short title]{Title}
\author{John Doe}
\date{\today}

\begin{document}
\begin{frame}
  \begin{equation}
  \label{eq:foo}
      2 + 2 = 4
  \end{equation}

  Hyperlink: \ref{eq:foo}
\end{frame}
\end{document}

答案1

当尝试补丁时,\tracingpatches在日志文件中添加并查看相关信息非常有用(交互式编译更好)。在这种情况下,你会得到

[debug] tracing \patchcmd on input line 12
[debug] analyzing '\insertshorttitle'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] -- macro cannot be retokenized cleanly
[debug] -> the macro may have been defined under a category
[debug]    code regime different from the current one
[debug] -> the replacement text may contain special control
[debug]    sequence tokens formed with \csname...\endcsname;
[debug] -> the replacement text may contain carriage return,
[debug]    newline, or similar characters

这意味着补丁没有成功。我经常还会将\ddt(或任何其他未定义的命令)放在最后一个参数中,以便在补丁失败时执行,因此 LaTeX 运行将在出现该消息后中断。

发生的情况是它\insertshorttitle有一个可选参数,所以它无法修补,\patchcmd这也是我编写该包的原因之一xpatch

如果你\usepackage{xpatch}

\makeatletter
\xpatchcmd{\insertshorttitle}
  {\beamer@shorttitle}
  {\usebeamercolor*[fg]{title in head/foot}\beamer@shorttitle}
  {}{}
\makeatother

调试信息将会显示

[debug] tracing \patchcmd on input line 12
[debug] analyzing '\\insertshorttitle'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] ++ macro can be retokenized cleanly
[debug] ++ search pattern found in replacement text
[debug] ++ patching possible
[debug] == retokenizing macro now

表示成功。但是这个补丁还不够,因为它只添加了\usebeamercolor*[fg]{title in head/foot}一次。事实上,\xshowcmd\insertshortitle在补丁命令之后添加会显示

> \\insertshorttitle=\long macro:
[#1]->\beamer@setupshort {#1}\let \thanks =\@gobble \ifnum \c@page =1\hyperlink
presentationend {\beamer@insertshort {\usebeamercolor *[fg]{title in head/foot}
\beamer@shorttitle }}\else \hyperlinkpresentationstart {\beamer@insertshort {\b
eamer@shorttitle }}\fi .

您必须添加第二个补丁,完整的补丁集将是

\makeatletter
\xpatchcmd{\insertshorttitle}
  {\beamer@shorttitle}
  {\usebeamercolor*[fg]{title in head/foot}\beamer@shorttitle}
  {}{}
\xpatchcmd{\insertshorttitle}
  {{\beamer@shorttitle}}
  {{\usebeamercolor*[fg]{title in head/foot}\beamer@shorttitle}}
  {}{}
\makeatother

第二个补丁必须有所不同,因为我们不想在之前再次添加代码第一的的发生\beamer@shorttitle

您可以使用实验包仅使用一个补丁regexpatch

\usepackage{regexpatch}
\makeatletter
\xpatchcmd*{\insertshorttitle}
  {\beamer@shorttitle}
  {\usebeamercolor*[fg]{title in head/foot}\beamer@shorttitle}
  {}{}
\makeatother

*-variant 会修补每个事件。请注意,您不能同时加载xpatchregexpatch

相关内容