[Tex/LaTex] How to tell if the current section is defined in Beamer

beamerconditionalsheader-footersectioning

I am customising the footer of my Beamer slides. The footer displays the title of the slides, followed by a ":", and then the current section title. This works fine if the current section is defined. However, if the section is not defined, I am left with the current title followed by a dangling colon. I would like the colon not to be displayed if the current section is not defined.

So, how do I determine if the current slide's section is defined, and how do I use this in a conditional in the footer? For completeness, it would also be helpful to know how one would determine if the current subsection is defined.

My attempt so far:

\begin{beamercolorbox}[wd=.85\paperwidth,ht=2.25ex,dp=1ex,left]{author in head/foot}%
   \usebeamerfont{author in head/foot}
   \hspace*{2ex}\insertshorttitle  % insert title in footer       
   \, : \insertsection             % insert current section [should only be done if section defined] 
\end{beamercolorbox}%

% more code to insert the slide number in the footer (not shown)

Best Answer

If no previous \section exists \insertsection is simply empty (at least in my tests). So you could test for this using:

\ifx\insertsection\empty
     % it is empty
\else
    it is not
\fi

Applied to your code:

\begin{beamercolorbox}[wd=.85\paperwidth,ht=2.25ex,dp=1ex,left]{author in head/foot}%
   \usebeamerfont{author in head/foot}
   \hspace*{2ex}\insertshorttitle  % insert title in footer       
   \ifx\insertsection\empty\else
      \, : \insertsection             % insert current section [should only be done if section defined] 
   \fi
 \end{beamercolorbox}%