5

I've ported from PowerPoint my university's template in Beamer, and added some code to display sections (and subsections when they exists) in the header.

This is the code snippet I've put in the headline template for this purpose:

\begin{beamercolorbox}[wd=0.6\paperwidth,ht=0.1\beamer@PoliMI@headheight]{section in head/foot}%
    \usebeamerfont{section in head/foot}\par%
    \vfill\strut\insertsectionhead%
    \ifx\insertsubsection\empty\else%
    ~--~\insertsubsectionhead%
    \fi%
    \strut\par%
\end{beamercolorbox}\\%

However, when "exiting" from subsection (issuing \subsection*{}), the dash after the section name is still printed.

How can I test if the subsection name is void?
As an alternative, how can I make \insertsubsection empty?

MWE:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}\normalfont
\usepackage[T1]{fontenc}

\setbeamertemplate{navigation symbols}{}

\setbeamertemplate{headline}{%
  \begin{beamercolorbox}[wd=0.6\paperwidth,ht=0.016\paperwidth]{section in head/foot}%
    \usebeamerfont{section in head/foot}%
    \par\vfill\strut\insertsectionhead%
    \ifx\insertsubsection\empty\else~--~\insertsubsectionhead%
    \fi\strut\par%
  \end{beamercolorbox}%
}

\begin{document}
 \section{aaa}
  \begin{frame}~\end{frame}
  \subsection{bbb}
   \begin{frame}~\end{frame}
  \subsection*{}
   \begin{frame}~\end{frame}
\end{document}

In the third frame, only "aaa" must show up, and not "aaa -".

UPDATE: If subsection is not yet defined, \insertsubsection is empty, whereas if a \subsection*{} is issued, the macro is expanded in \expandafter \hyperlink \subsectionlink, either the subsection is empty (\insertsubsectionhead expands to \hyperlink {Navigation\the \c@page }{}) or not (\insertsubsectionhead expands to \hyperlink {Navigation\the \c@page }{bbb} in the second frame).

2
  • 1
    Welcome to TeX.SE. Thanks for posting the problem code snippets. However you should also post a fully compilable MWE that reproduces the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. It is still going to be useful in testing that a proposed solution actually worked. – Peter Grill Oct 1 '14 at 8:33
  • @PeterGrill: please see edited question. – Astrinus Oct 1 '14 at 9:05
6

I have replaced the code snippet with

\ifx\insertsubsection\empty\else%
 \ifdefempty{\subsecname}{\relax}{%
  ~--~\insertsubsectionhead%
 }%
\fi

and IT WORKS!!!
Unfortunately needs the etoolbox package to be loaded...

EDIT:

\setbox0=\hbox{\subsecname\unskip}\ifdim\wd0=0pt\else%
 ~--~\insertsubsectionhead%
\fi%

also works and does not require etoolbox.
I don't know if this is the "cleaner" solution.

1

I can't comment but I had issues with the 2nd solution of @Astrinus and wanted to share it nonetheless.

\subsecname is not deleted by default when a new section is created. Replacing it with \insertsubsectionhead worked to solve it for me.

\setbox0=\hbox{\insertsubsectionhead\unskip}\ifdim\wd0=0pt\else%
        - \insertsubsectionhead%
        \fi%
0

I had issues with recognizing end of subsection by:

\ifx\insertsubsection\empty

Limiting the SubSection in braces made it working:

{
\subsection{SubSection Title}
<subsection frames>
}
<other frames>

The condition correctly differed between frames inside and outside that SubSection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.