# Beamer missing section causing Undefined control sequence error

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?

-

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}%
}%
}%
\beamer@subsectionstartpage=\c@page%
\def\insertsubsubsection{}%
%\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).

-
Indeed: Till's attitude on this sort of thing is quite clear if you read the code. Sections are only really supported by beamer if properly nested, and while \subsubsection is defined it's not really fully implemented as Till didn't want people to use it! – Joseph Wright Apr 11 '14 at 14:25
@JosephWright in some minutes I'll add some remarks along the lines of your comment to my answer. – Gonzalo Medina Apr 11 '14 at 16:26
I think that this covers my question thank you. Indeed it does not make sense using subsections without sections in beamer. I arrived at the situation because I commented out part of my slides to avoid compilation. – Evangelos Bempelis Apr 11 '14 at 16:56
@VagelisBebelis I've added some new information to my answer that could be of interest for you. – Gonzalo Medina Apr 11 '14 at 22:24
Yes, I saw it, now my question is completely answered! Thanks once more. – Evangelos Bempelis Apr 12 '14 at 11:37