[Tex/LaTex] Detect aspect ratio in beamer

beamerconditionals

I'm doing my slides with beamer. At the moment I am not sure about the aspect ratio that will be used at the presentation. I know I can change the ratio with class options of beamer.

The problem is that changing the aspect ratio changes the document dimension, so I have to adjust some parameters (figure height) in my presentation. I will like to design the presentation for 4:3 and 16:9 ration.

Is it possible to have something like

\ifratio{34}{do somthing}
\ifratio{169}{do ohters}

or

\ifratio{34}{do this}{else this}

Best Answer

The aspectratio option doesn't set any useful flag, apart from the page dimensions. One could check for these dimension pairs or else doing a direct check from the options list; here are two macros defined with the help of expl3 according to the latter strategy.

\documentclass[aspectratio=1610]{beamer}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\ifaspectratio}{mmm}
 {
  % Recover the option from those passed to the class
  \keys_set:nf { zunbeltz/beameroptions } { \use:c { opt@beamer.cls } }
  \str_if_eq:nVTF { #1 } \l_zunbeltz_aspectratio_tl { #2 } { #3 }
 }
\NewDocumentCommand{\xifaspectratio}{mO{}}
 {
  % Recover the option from those passed to the class
  \keys_set:nf { zunbeltz/beameroptions } { \use:c { opt@beamer.cls } }
  \str_case:Vnn \l_zunbeltz_aspectratio_tl { #1 } { #2 }
 }
% We need to define only one key, the other are treated as `unknown'
\keys_define:nn { zunbeltz/beameroptions }
 {
  aspectratio .tl_set:N = \l_zunbeltz_aspectratio_tl,
  aspectratio .initial:n = 43,
  unknown .code:n = {},
 }

% Generate the variants we need
\cs_generate_variant:Nn \keys_set:nn { nf }
\cs_generate_variant:Nn \str_if_eq:nnTF { nV }
\cs_generate_variant:Nn \str_case:nnn { V }
\ExplSyntaxOff

\begin{document}
\begin{frame}
\ifaspectratio{1610}{We're on 16:10}{We're not on 16:10}

\xifaspectratio{
  {169}{We're on 16:9}
  {43}{We're on 4:3}
}[None of the above]
\end{frame}
\end{document}

The macro \ifaspectratio takes three arguments, the ratio which we want to test, the true text and the false text.

The macro \xifaspectratio is more flexible: it takes one mandatory argument, which is a list of pairs {<ratio>}{<text>} and an optional argument for a "none of the above" case.

enter image description here

By adding to \keys_define:nn { zunbeltz/beameroptions } one could define similar macros for checking other options.