# Problem statement

For many years, I used the following code to automatically insert the title of the current \section as the Beamer frame title, unless no section has yet been defined - in which case the title of the current \part should be used instead.

\newcommand{\autoframetitle}{%
\ifx\insertsection\empty%
\insertpart%
\else%
\insertsection%
\fi%
}%


With a recent Beamer upgrade (most likely the one to version 3.50), the above code ceased to work in situations where a \part but no \section has been defined. This will cause the above test to end up in the \else branch, and an (empty) section title will therefore be printed instead of the expected part title.

I am looking for a way to restore the original intended behavior.

# Minimal (non-)working example (MWE)

\documentclass{beamer}

\newcommand{\autoframetitle}{%
\ifx\insertsection\empty%
\insertpart%
\else%
\insertsection%
\fi%
}%

\begin{document}

\part{Part title}

\begin{frame}{\autoframetitle}%
TODO: This slide should print the part title, but instead prints the (still empty) section title.
\end{frame}

\section{Section title}

\begin{frame}{\autoframetitle}%
This slide prints (as expected) the (now no longer empty) section title.
\end{frame}

\end{document}


# Issue seems to be related to presence of \part{}

The following second MWE demonstrates that the \ifx\insertsection\empty statement above appears to yield FALSE as soon as a \part is defined. If one takes out the \part definition, the test yields TRUE as expected.

\documentclass{beamer}

\newcommand{\autoframetitle}{%
\ifx\insertsection\empty%
Neither part nor section defined
\else%
\insertsection%
\fi%
}%

\begin{document}

\begin{frame}{\autoframetitle}%
This slide correctly prints "Neither part nor section defined" fallback text.
\end{frame}

\section{Section title}

\begin{frame}{\autoframetitle}%
This slide prints (as expected) the (now no longer empty) section title.
\end{frame}

\end{document}

• As beamer automatically loads etoolbox here's a simple alternative \newcommand{\autoframetitle}{% \expandafter\ifstrempty\expandafter{\insertsection}{% \insertpart% }{% \insertsection% }% }% , but unfortunately I cannot explain the change. – TeXnician Jun 2 '18 at 18:38
• Thanks, I've just tested this and it works flawlessly. If only there was a way to accept both yours and David's answer... But since your comment contains a viable solution, I'll accept it if you write it up as an actual answer (unless an even simpler solution surfaces in the meantime). – Florian H. Jun 2 '18 at 19:04
• As your question does not seem to ask for a solution, but for an explanation, I think you should accept David's answer. – TeXnician Jun 2 '18 at 19:11
• That's a fair point, but is a flaw of the original question that I will address by editing it. – Florian H. Jun 2 '18 at 22:02
• Just replace \empty by \protectedempty in your code having done once \protected\def\protectedempty{}. – user4686 Jun 3 '18 at 8:58

This is not an explanation (David gave one), but a different approach which bases on beamer loading etoolbox by default. Hence you can replace your code with

\newcommand{\autoframetitle}{%
\expandafter\ifstrempty\expandafter{\insertsection}{%
\insertpart%
}{%
\insertsection%
}%
}%


which will not show the problem.

\part defines \insertsection by

\protected\def\insertsection{}


so it's empty but not \ifx equal to \empty which is defined by

\def\empty{}

• Hi David, thanks for explaining the source of this problem! I guess my question than is which other test could restore the original behavior effectively? Perhaps TeXnician's one above? Any alternatives that do not rely on etoolbox? – Florian H. Jun 2 '18 at 18:51
• @FlorianH. \if|\insertsection| perhaps which if \insertsection expands to nothing is \if|| which is true (assume \insertsection never starts with |) – David Carlisle Jun 2 '18 at 19:36

A test for emptiness can be

\if\relax\detokenize\expandafter{\insertsection}\relax
<code when the expansion of \insertsection is empty>
\else
<code when the expansion of \insertsection is not empty>
\fi


This will expand \insertsection once before applying \detokenize, so the nature of the tokens in the expansion of \insertsection is unimportant.