Search code examples
latexbeamer

Vim beamer: dynamic frame title


I am trying to create a new command in Latex Beamer to automatically add the frame title and subtitle with respected to section and subsection. In general, my command will look like:

\newcommand {\myframe}[1] {
    \begin{frame}
    if in a section {
        \frametitle{\secname}
    }
    if in a subsection {
        \framesubtitle{\subsecname}
    }
    #1
    \end{frame}
}

How can I detect whether the frame is in a section or subsection?


Solution

  • You can add conditionals to the \section and \subsection command, which you can use to test within your \myframe macro:

    enter image description here

    \documentclass{beamer}
    
    \let\Tiny\tiny% http://tex.stackexchange.com/a/94159/5764
    
    \newif\ifinsection
    \newif\ifinsubsection
    
    \let\oldsection\section
    \renewcommand{\section}{%
      \global\insectiontrue% In section
      \global\insubsectionfalse% Not in subsection
      \oldsection}
    \let\oldsubsection\subsection
    \renewcommand{\subsection}{%
      %\global\insectionfalse% No in section
      \global\insubsectiontrue% In subsection
      \oldsubsection}
    
    \newcommand {\myframe}[1] {%
      \begin{frame}
      \ifinsection\frametitle{\secname}\fi
      \ifinsubsection\framesubtitle{\subsecname}\fi
      #1
      \end{frame}
    }
    
    \begin{document}
    
    \begin{frame}
      \frametitle{A frame}
    \end{frame}
    
    \section{A section}
    
    \myframe{Some content}
    
    \subsection{A subsection}
    
    \myframe{Some content}
    
    \end{document}