# Detect aspect ratio in beamer

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}


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.

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

You can use the ifthen package in this way

\documentclass{beamer}
%\documentclass[aspectratio=169]{beamer}
\usepackage{ifthen}

\makeatletter
\newcommand{\prova}{Aspect ratio is 4:3 (default)}
\ifthenelse{%
\lengthtest{\beamer@paperwidth=16cm}}% condition (16cm is the width of the frame with an aspect ratio = 16:9)
{\renewcommand{\prova}{Aspect ratio is 16:9}}% do this if condition is true
{}% do nothing elsewhere
\makeatother

\begin{document}

\begin{frame}
\prova
\end{frame}

\end{document}


And you can add others constructs like this

\ifthenelse{%
\lengthtest{\beamer@paperwidth=16cm}}% condition (16cm is the width of the frame with an aspect ratio = 16:9)
{\renewcommand{\prova}{Aspect ratio is 16:9}}% do this if condition is true
{}% do nothing elsewhere


to add other "supported" aspect ratios. But if you want to create something more universal (not only for you and not only for 16:9 or 4:3 aspect) you have to be careful because different aspect ratios can have the same paperwidth or the same paperheight. A solution can be to have two \ifthenelse one inside the other.

This is how the paper dimensions are defined for different aspect ratio into the beamer.cls class file

\DeclareOptionBeamer{aspectratio}[43]{%
\ifnum#1=1610%
\beamer@paperwidth 16.00cm%
\beamer@paperheight 10.00cm%
\else\ifnum#1=169%
\beamer@paperwidth 16.00cm%
\beamer@paperheight 9.00cm%
\else\ifnum#1=149%
\beamer@paperwidth 14.00cm%
\beamer@paperheight 9.00cm%
\else\ifnum#1=54%
\beamer@paperwidth 12.50cm%
\beamer@paperheight 10.00cm%
\else\ifnum#1=43%
\beamer@paperwidth 12.80cm%
\beamer@paperheight 9.60cm%
\else\ifnum#1=32%
\beamer@paperwidth 13.50cm%
\beamer@paperheight 9.00cm%
\fi\fi\fi\fi\fi\fi%
}


I went for your 2nd suggestion and created a command called \ifratio to be used as follows:

\ifratio{43}{code in case aspect ratio is 43}{code otherwise}


You can use with both aspect ratio options 43 and 169; no other, though, because I'm too lazy, but you could easily extend the command for it to handle more cases.

No need for ifthenelse.

%\documentclass{beamer}
\documentclass[aspectratio=169]{beamer}

\makeatletter
\newcommand\ifratio[3]{%
\ifnum#1=169%
\ifdim\beamer@paperwidth=16.00cm\relax%
\ifdim\beamer@paperheight=9.00cm\relax%
#2%
\else%
#3%
\fi%
\else%
#3%
\fi%
\else%
\ifnum#1=43%
\ifdim\beamer@paperwidth=12.80cm\relax%
\ifdim\beamer@paperheight=9.60cm\relax%
#2%
\else%
#3%
\fi%
\else%
#3%
\fi%
\fi%
\fi%
}
\makeatother

\begin{document}
\begin{frame}
\ifratio{169}{Code for 169}{Code for aspect ratio other than 169}
\end{frame}
\end{document

• My and your answers are a little complicated, does anybody knows how to directly access to the value of a class option?
– Red
Jul 8 '13 at 16:01

I was not entirely happy with any of the given answers. The answer by @egreg was probably the best. I now use this solution:

\ifthenelse{\lengthtest{\beamer@paperwidth=16.00cm}\AND\lengthtest{\beamer@paperheight=10.00cm}}{
\def\beamer@aspectratio{1610}
}{}
\ifthenelse{\lengthtest{\beamer@paperwidth=16.00cm}\AND\lengthtest{\beamer@paperheight=9.00cm}}{
\def\beamer@aspectratio{169}
}{}
\ifthenelse{\lengthtest{\beamer@paperwidth=12.80cm}\AND\lengthtest{\beamer@paperheight=9.60cm}}{
\def\beamer@aspectratio{43}
}{}
\def\theratio{\beamer@aspectratio}

\newcommand\ifratio[3]{%
\ifthenelse{\beamer@aspectratio=#1}{#2}{#3}
}


This requires the package ifthen`. It is easily expandable for other aspect ratio's.