Following on from this answer by samcarter_is_at_topanswers.xyz in relation to the question section title in \frametitle beamer, I have used the provided answer:
\makeatletter
\patchcmd\beamer@@tmpl@frametitle{\insertframetitle}{\insertsection-\insertframetitle}{}{}
\makeatother
in my main beamer document. I have modified it so that instead of \insertsection it is \insertsubsection as my subsections are more relevant. However, using this is predictably affecting all of my frame's regardless of whether the \insertsubsection is empty (such as for TOC pages). My main document currently looks like this (I used \insertsection\ - \insertframetitle instead, notice the spaces around the hyphen):
How can I test for or only include the \patchcmd's effect when there is a subsection present (or via any method/division that works)? I am also including a TOC per section using:
\AtBeginSection[]
{
\ifnum \value{framenumber}>1
\begin{frame}<beamer>
\frametitle{Outline}
\tableofcontents[currentsection]
\end{frame}
\else
\fi
}
and references and perhaps a glossary at the end. One possible solution that I can see is to just repatch the \beamer@@tmpl@frametitle command before and after slide(s) that I desire or do not desire the the subsection being in the title but perhaps there is a better way that is beyond my knowledge that could be within others reach, hence the question. I attempted this to save asking a redundant question and it failed like so:
using:
\newcommand\showSubsec{
\makeatletter
\patchcmd\beamer@@tmpl@frametitle{\insertframetitle}{\insertsubsection\ - \insertframetitle}{}{}
\makeatother
}
\newcommand\hideSubsec{
\makeatletter
\patchcmd\beamer@@tmpl@frametitle{\insertsubsection\ - \insertframetitle}{\insertframetitle}{}{}
\makeatother
}
MWE:
\documentclass{beamer}
\usepackage{xpatch}
\makeatletter
\patchcmd\beamer@@tmpl@frametitle{\insertframetitle}{\insertsubsection\ - \insertframetitle}{}{}
\makeatother
% For TOC at each section
\AtBeginSection[]
{
\ifnum \value{framenumber}>1
\begin{frame}<beamer>
\frametitle{Outline}
\tableofcontents[currentsection]
\end{frame}
\else
\fi
}
\begin{document}
\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}
\begin{frame}
\frametitle{Introduction}
Introduction
\end{frame}
\section{First section}
\subsection{First subsection}
\begin{frame}
\frametitle{First slide Title}
Text
\end{frame}
\subsection{Second subsection}
\begin{frame}
\frametitle{Second slide Title}
\small
Text
\end{frame}
\section{Conclusions and Future Work}
\subsection{Conclusions}
\begin{frame}
\frametitle{Conclusions}
\centering
Text
\end{frame}
\subsection{Future Work}
\begin{frame}
\frametitle{Future Work}
\centering
Text
\end{frame}
\section{Back matter}
\subsection{References}
\begin{frame}
\frametitle{References}
References
\end{frame}
\end{document}
MWE output:
Ideally I want the subsection title in the relevant frames but only if the subsection exists, not in TOC/reference/introduction slides etc. If this is not possible then I will revert to not having the subsections in the \frametitle but it would be nice if possible please.


