2
\documentclass{beamer}

\begin{document}

\begin{frame}
\frametitle{Contents}
\tableofcontents
\end{frame}

\section[Sec. A]{Section A}
\subsection{SubSec a}
\begin{frame}
\frametitle{\insertsection: \insertsubsection}
\end{frame}

\subsection{SubSec b}
\begin{frame}
\frametitle{\insertsection: \insertsubsection}
\end{frame}

\section[Sec. B]{Section B}
\subsection{SubSec a}
\begin{frame}
\frametitle{\insertsection: \insertsubsection}
\end{frame}

\subsection{SubSec b}
\begin{frame}
\frametitle{\insertsection: \insertsubsection}
\end{frame}

\end{document}

  • I often use \frametitle{\insertsection: \insertsubsection} (or similar) to automatically generate frame titles in beamer.
  • Sometimes I would like to use the short version of the title, such as in \section[Sec. A]{Section A} --> \insertshortsection (= Sec. A) which is not available (see also here).

Is there an easy way to access the short version of section and friends? With "easy" I mean that the solution does not have many side effects.


Probably Related


Solution

\documentclass{beamer}

\let\oldsection\section
\makeatletter
\def\section{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\xdef\myshortsec{#1}
\oldsection*{#2}%
}
\def\@StarredWithout#1{
\xdef\myshortsec{#1}
\oldsection*{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\xdef\myshortsec{#1}
\oldsection[#1]{#2}%
}
\def\@nonStarredWithout#1{%
\xdef\myshortsec{#1}
\oldsection{#1}%
}
\makeatother

\begin{document}

\begin{frame}
\frametitle{Contents}
\tableofcontents
\end{frame}

\section[Sec. A]{Section A}
\subsection{SubSec a}
\begin{frame}
\frametitle{\insertsection\ \texttt{(long)}: \myshortsec\ \texttt{(short)}: \insertsubsection}
\end{frame}

\subsection{SubSec b}
\begin{frame}
\frametitle{\insertsection\ \texttt{(long)}: \myshortsec\ \texttt{(short)}: \insertsubsection}
\end{frame}

\end{document}

enter image description here

2 Answers2

8

You can use \insertsectionhead:

\documentclass{beamer}

\begin{document}

\begin{frame}
\frametitle{Contents}
\tableofcontents
\end{frame}

\section[Sec. A]{Section A}
\subsection{SubSec a}
\begin{frame}
\frametitle{\insertsectionhead: \insertsubsection}
\end{frame}

\end{document}

enter image description here

Ulrike Fischer
  • 327,261
2

Here is an answer stolen from this one: https://tex.stackexchange.com/a/380116/120578

(It is mine too and have been used at least 5-6 times for solving different problems, but for some reason has still zero votes :( :P )

\documentclass{beamer}

\let\oldsection\section
\makeatletter
\def\section{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\xdef\myshortsec{#1}
\oldsection*{#2}%
}
\def\@StarredWithout#1{
\xdef\myshortsec{#1}
\oldsection*{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\xdef\myshortsec{#1}
\oldsection[#1]{#2}%
}
\def\@nonStarredWithout#1{%
\xdef\myshortsec{#1}
\oldsection{#1}%
}
\makeatother

\begin{document}

\begin{frame}
\frametitle{Contents}
\tableofcontents
\end{frame}

\section[Sec. A]{Section A}
\subsection{SubSec a}
\begin{frame}
\frametitle{sect-\insertsection : short-\myshortsec: \insertsubsection}
\end{frame}

\subsection{SubSec b}
\begin{frame}
\frametitle{sect-\insertsection: short-\myshortsec : \insertsubsection}
\end{frame}


\end{document}

If we redefine sectioning commands this way we can do many things to use their arguments as I noticed in the original answer too :P

koleygr
  • 20,105
  • "for some reason has still zero votes" --> not anymore – Dr. Manuel Kuehner Feb 26 '18 at 21:53
  • I will wait if there are some other solutions if that's ok with you. I am on a business trip and probably be back on Thursday night. – Dr. Manuel Kuehner Feb 26 '18 at 22:01
  • 1
    No problem... You already have a simpler... – koleygr Feb 26 '18 at 22:02
  • Right - Ulrike's answer is simpler in this case. I will use your code for studying the idea behind. It will help me in the future. Thanks again. – Dr. Manuel Kuehner Feb 26 '18 at 22:07
  • Welcome... (and 5 more to go) – koleygr Feb 26 '18 at 22:09
  • Could you add some explanation of the basic idea of your solution? – Dr. Manuel Kuehner Feb 26 '18 at 22:26
  • 1
    The idea is to redefine section [used commands 1) \@ifstar to check if the section is a starred or a non starred 2) \ifnextchar to find out if after the possible star there follows an optional argument 3) 3) Then used the old definition of section after kept (stored) the wanted argument via a \xdef]. I could do anything with the arguments in there (capitalize replace words etc). – koleygr Feb 26 '18 at 22:35