[Tex/LaTex] Beamer missing section causing Undefined control sequence error

beamererrors

I ran onto this error today, here is a minimum example:

\documentclass[]{beamer}

\begin{document}
% \section{A section}
\subsection{A subsection}

\begin{frame}  
\frametitle{Cursed frame}
This is awkward.
\end{frame}

\end{document}

The compilation gives: Undefined control sequence. [\end{frame}]
Apparently declaring a subsection without a section causes the error to appear as removing the comment, makes the error go way.

What also is weird is that despite the error, the .pdf is produced fine in both cases.
I'd like to know why this happens, as in other documents having subsections without section never was a problem. Is it a bug or a feature?

Best Answer

As Joseph Wright has mentioned in his comment, the fact that sectional units have to be appropriately nested is a design choice made by Till Tantau.

The "culprit" of the error you got can be found in the following lines of the definition of \beamer@subsection (given in beamerbasesection.sty) used by \subsection:

    \Hy@writebookmark{\the\c@subsection}{#2}{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}{3}{toc}%

Using the original definition and commenting out the mentioned lines, suppresses the error:

\documentclass[]{beamer}

\makeatletter
\def\beamer@subsection[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture%
    \refstepcounter{subsection}%
    \beamer@ifempty{#2}{\long\def\subsecname{#1}\long\def\lastsubsection{#1}}
    {%
      \long\def\subsecname{#2}%
      \long\def\lastsubsection{#1}%
      \addtocontents{toc}{\protect\beamer@subsectionintoc{\the\c@section}{\the\c@subsection}{#2}{\the\c@page}{\the\c@part}{\the\beamer@tocsectionnumber}}%
    }%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \addtocontents{nav}{%
      \protect\headcommand{\protect\beamer@subsectionentry{\the\c@part}{\the\c@section}{\the\c@subsection}{\the\c@page}{\lastsubsection}}%
      \protect\headcommand{\protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\the\beamer@tempcount}}%
    }%
    \beamer@subsectionstartpage=\c@page%
    \edef\subsectionlink{{Navigation\the\c@page}{\noexpand\subsecname}}%
    \def\insertsubsection{\expandafter\hyperlink\subsectionlink}%
    \def\insertsubsubsection{}%
    \def\insertsubsectionhead{\hyperlink{Navigation\the\c@page}{#1}}%
    \def\insertsubsubsectionhead{}%
    %\Hy@writebookmark{\the\c@subsection}{#2}{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}{3}{toc}%
    \hyper@anchorstart{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}\hyper@anchorend%
    \beamer@ifempty{#2}{\beamer@atbeginsubsections}{\beamer@atbeginsubsection}%
  \fi%
  \beamer@resumemode}
\makeatother
\begin{document}
\section{A section}
\subsection{A subsection}

\begin{frame}  
\frametitle{Cursed frame}
This is awkward.
\end{frame}

\end{document}

but this, of course, it's not advisable since it will mess up the bookmarks.

Another way of preventing the error is to load the bookmark package:

\documentclass[]{beamer}
\usepackage{bookmark}

\begin{document}
% \section{A section}
\subsection{A subsection}

\begin{frame}  
\frametitle{Cursed frame}
This is awkward.
\end{frame}

\end{document}

However, the best thing to do is not to use a subsection without a previous section (using a subsection without section doesn't make much sense, anyways).