# Patch \insertshorttitle in beamer

In this answer @cfr showed me how to preserve short title color in beamer while allowing hyperlinks to work. His solution was to modify the \insertshorttitle command (defined in beamerbasetitle.sty) like this:

\makeatletter
\renewcommand\insertshorttitle[1][]{%
\beamer@setupshort{#1}%
\let\thanks=\@gobble%
\ifnum\c@page=1%
\else%
\fi}
\makeatother


This works just fine but I want to know if it possible to just patch the \insertshorttitle command using etoolbox. I tried doing

\makeatletter
\makeatother


but it didn't work. etoolbox suggested that the command is not patchable because "the macro may have been defined under a category code regime different from the current one".

So my question is: can I patch the command somehow? Here is a complete MWE:

\documentclass[leqno]{beamer}

\usecolortheme{whale}

\usepackage{lmodern}
\usepackage{etoolbox}

\makeatletter
\makeatother

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

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

\end{frame}
\end{document}

• \insertshorttitle is defined with an optional argument, so \patchcmd doesn't work. Use \xpatchcmd of the xpatch package. See Please tutor the usage of \patchcmd and xpatch – egreg Oct 5 '14 at 22:23
• For the record, that's a quote from etoolbox's output. I said it suggested 3 possible reasons of which 2 did not apply, which only left that one. What I mean is, I did not pretend to understand that explanation ;)! – cfr Oct 5 '14 at 22:33
• @cfr feel free to edit my question accordingly. – petobens Oct 5 '14 at 22:35
• @egreg If I try this in a simple-minded way, it doesn't work. The command is patched but the subtitle only shows in the correct colour on the first of the two slides in my MWE, whereas when I \renewcommand (as I did there), it works correctly even in the second frame. The hyperlink is created but the text does not show in the patched colour. – cfr Oct 5 '14 at 22:38
• @cfr Two patches are needed, because \patchcmd (on which \xpatchcmd of xpatch is based) just patches the first occurrence. – egreg Oct 5 '14 at 22:41

When a patch is tried, it's very useful to add \tracingpatches and look at the relevant message in the log file (interactive compilation is better). In this case you'd get

[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


which means that the patch didn't succeed. Often I also place \ddt (or any other undefined command) in the last argument, which is executed in case the patch fails, so the LaTeX run will be interrupted after that very message.

What happens is that \insertshorttitle has an optional argument, so it's not patchable with \patchcmd and this is one of the reasons I wrote the xpatch package.

If you say \usepackage{xpatch} and

\makeatletter
\xpatchcmd{\insertshorttitle}
{\beamer@shorttitle}
{}{}
\makeatother


the debugging message will say

[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


meaning success. However this patch is not sufficient, because it adds \usebeamercolor*[fg]{title in head/foot} only once. Indeed, adding \xshowcmd\insertshortitle after the patching command will reveal

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


You must add a second patch, the complete set would be

\makeatletter
\xpatchcmd{\insertshorttitle}
{\beamer@shorttitle}
{}{}
\xpatchcmd{\insertshorttitle}
{{\beamer@shorttitle}}
{}{}
\makeatother


The second patch must be different, because we don't want to add again the code before the first occurrence of \beamer@shorttitle.

You could do with just one patch by using the experimental package regexpatch:

\usepackage{regexpatch}
\makeatletter
\xpatchcmd*{\insertshorttitle}
{\beamer@shorttitle}

The *-variant patches every occurrence. Note that you can't load both xpatch and regexpatch.
• (+1) but, just out of curiosity, is this preferable to using \renewcommand? (And, if so, why?) Or is it just an alternative? – cfr Oct 5 '14 at 22:45