2

I'm trying to understand why the hook AtBeginEnvironment{frame} does not seem to apply in this MWE:

\documentclass{beamer}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usetheme{Madrid}

\usepackage[T1]{fontenc}
\usepackage[french]{babel}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
% Redéfinition de l'environnement frame
\AtBeginEnvironment{frame}{%
    \frametitle{\thesection - \insertsection}%
    \framesubtitle{\thesubsection - \insertsubsection}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
\begin{document}
 
\section{section n°1}
\subsection{sous-section 1-1}
 
\begin{frame}
Texte du slide.
\end{frame}
 
\end{document}

Normally, it should print a title and a subtitle in the frame, but for some reason that I don't understand it does not.

Thanks for helping.

3
  • 1
    note your example, as posted here, is necessarily in UTF-8 so \usepackage[latin1]{inputenc} needs to be deleted Oct 29, 2022 at 17:31
  • 1
    frame is not a standard environment but a rather special command. Oct 29, 2022 at 17:32
  • So, doing such thing is impossible? Maybe in a different way? Oct 29, 2022 at 17:35

2 Answers 2

0

Here is a way:

\documentclass{beamer}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usetheme{Madrid}

\usepackage[T1]{fontenc}
\usepackage[french]{babel}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
% Redéfinition de l'environnement frame
\let\oldframe\frame
\def\newframe{%
\oldframe\frametitle{\thesection - \insertsection}%
\framesubtitle{\thesubsection - \insertsubsection}%
}
\def\endnewframe{\endframe}

\renewenvironment{frame}{\newframe}{\endnewframe}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
\begin{document}
 
\section{section n°1}
\subsection{sous-section 1-1}
 
\begin{frame}
Texte du slide.
\end{frame}
 
\end{document}
2
  • Thanks! Indeed, it works pretty fine in Overleaf. Sadly, this is failing when using Pandoc to convert a markdown file to beamer. That's extremely weird. Oct 29, 2022 at 18:27
  • @PolDellaiera Not weird at all. Pandoc uses the extremly annoying syntax \begin{frame}{frametitle}{framesub} to set the frame title and subtitle and by redefining the frame environment you'll loose the ability to use this syntax as well as optional arguments. Oct 29, 2022 at 21:08
0

Setting the frametitle at the start of the frame environment is too early. As there are people - including pandoc users - who use the extremely annoying syntax \begin{frame}{frametitle}{framesubtitle}, beamer needs to jump through a lot of hoops to deal with this complicate syntax. You have to wait with \frametitle after beamer is done with this.

Instead of messing with the frame environment, disable beamer's checks for the existence of a frametitle and patch the frametitle to show the section/subsection if the frametitle is empty:

\documentclass{beamer}

\usepackage{xpatch}
\makeatletter
\patchcmd\beamer@@tmpl@frametitle{\insertframetitle}{%
\ifx\insertframetitle\@empty
  \thesection{} - \insertsection
  \ifx\insertframesubtitle\@empty
  \ifnum\thesubsection>0
    \def\insertframesubtitle{\thesubsection{} - \insertsubsection}
  \fi\fi
\else
  \insertframetitle
\fi
}{}{}
\makeatother


\makeatletter
  \CheckCommand*\beamer@checkframetitle{\@ifnextchar\bgroup\beamer@inlineframetitle{}}
  \renewcommand*\beamer@checkframetitle{\global\let\beamer@frametitle\relax\@ifnextchar\bgroup\beamer@inlineframetitle{}}
\makeatother


\begin{document}

\section{section 1}
\subsection{sub 1}
\begin{frame}
\end{frame}

\begin{frame}
\frametitle{frametitle}
\framesubtitle{framesub}

\end{frame}
\end{document}
4
  • Thanks again SamCarter. This is indeed working a bit better in Pandoc. I'm trying to figure out how to add the current section in the subtitle. Replacing the occurrences of title with subtitle in your snippet does not seems to work... investigating. Oct 30, 2022 at 6:36
  • And there is another issue, your snippet add a title on the \titlepage and \sectionpage Oct 30, 2022 at 6:37
  • @PolDellaiera Sure it does add them to the title page etc. Your question does not ask to exclude any special frames, so why should an answer exclude them? However you can exclude them as shown in tex.stackexchange.com/questions/661314/… Oct 30, 2022 at 10:58
  • Thanks, I will see what I can do with all these information. Oct 30, 2022 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .