0

How do I pass the document class options to a beamer theme?

I am writing a beamer theme. I would like to use the beamer class option aspectratio to have individual layout settings for the 43 and 169 options. I tried to read the options with \DeclareOptionBeamer and \ProcessOptionsBeamer in my sty file:

\ProvidesPackage{beamerthememytheme}[2020/03/10]

\newif\ifwidescreen
\widescreenfalse
\DeclareOptionBeamer{aspectratio}[43]{%
    \ifnum#1=169 %
        \widescreentrue%
    \else\ifnum#1=43 %
        \widescreenfalse%
    \fi\fi
}
\ProcessOptionsBeamer
\mode<presentation>

The beamer class option aspectratio is not passed to my theme:

\documentclass[aspectratio=169]{beamer}

\usetheme{mytheme}

\begin{document}

\begin{frame}
    \ifwidescreen%
        16:9
    \else%
        4:3
    \fi
\end{frame}

\end{document}

However, it works as expected if I load the theme with the argument \usetheme[aspectratio=169]{mytheme}. But I would like that the theme uses the same option as specified in the document class options if no option is provided.

  • I changed the title to make it a little clearer – daleif Mar 10 at 17:40
  • beamer themes can only inherit class options without =. beamer does this inside macro \beamer@filterclassoptions. For example, in usage \documentclass[key1=val1, key2]{beamer}, only key2 is passed to themes. So, do you want a general solution, or a specific way to detect the value passed to aspectratio? – muzimuzhi Z Mar 10 at 23:39
  • The aspectratio option accepts different values : 1610, 169, 149, 141, 54, 43 and 32. Which values should set \ifwidescreen to true? – Paul Gaborit Mar 10 at 23:44
  • @PaulGaborit At the moment I am considering 169 and 43 only. So, 169 should set \ifwidesceen to true. – luki Mar 11 at 10:52
1

Currently,

  • Usually, LaTeX classes store options in \@classoptionslist, and later loaded packages read this macro to inherit options from class.
  • However, beamer class stores key only options in \@classoptionslist (by using macro \beamer@filterclassoptions). This means, any class options containing =, e.g., aspectratio=<ratio>, is filtered out.
  • \ProcessOptionsBeamer seems to handle key only options, too. (I am not sure.)

The following example provides a workaround to overcome that:

\RequirePackage{etoolbox}
\RequirePackage{scrlfile}
\makeatletter
\AfterPackage{beamerbaseoptions}{%
  % store full class options in \@classoptionslist@full
  \pretocmd{\beamer@filterclassoptions}
    {\let\@classoptionslist@full=\@classoptionslist}
    {}{\fail}%
}
\makeatother

\documentclass[aspectratio=169]{beamer}

\makeatletter
% extended \usetheme which inherits key=val class options
\newrobustcmd*\usethemeX[2][]{%
  \let\@classoptionslist=\@classoptionslist@full
  \usetheme[#1]{#2}%
  \let\@classoptionslist=\beamer@filteredclassoptionslist
}

% patch \ProcessOptionsBeamer
\patchcmd{\ProcessOptionsBeamer}
  {\@ifundefined{KV@\@currname @\CurrentOption}}
  {\expandafter\get@key\CurrentOption=\@nil
   \@ifundefined{KV@\@currname @\CurrentOption@key}}
  {}{\fail}
% helper macro
\def\get@key#1=#2\@nil{\def\CurrentOption@key{#1}}
\makeatother

\usethemeX{mytheme}

\begin{document}
\begin{frame}
  \ifwidescreen
    16:9
  \else
    4:3
  \fi
\end{frame}
\end{document}

By the way, the line containing \if#1=43 in OP's example should be \ifnum#1=43.

| improve this answer | |
  • Wow, thanks for the workaround! This was more complicated than I expected. – luki Mar 11 at 10:50
  • @luki Hi, I have simplified the workaround, hope that helps. – muzimuzhi Z Mar 11 at 16:49
  • Great, thanks a lot! – luki Mar 15 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.