# Store result of \ratio calculation for repeated use

I am using pdflatex with the beamer package to create a presentation template that is similar to the PowerPoint template provided by my company.

I want the company logo to have the same size in my template as in the original one. I know the width of the logo in the PowerPoint template, the page width of the PowerPoint slides and the width of the slides in beamer.

At this moment, I manage to achieve this scaling as follows:

\RequirePackage{calc}

\newlength{\pptwidth}
\setlength{\pptwidth}{254mm} % width of PPT slide
\newlength{\beamerwidth}
\setlength{\beamerwidth}{160mm} % width of beamer slide
\newlength{\logowidth}
\setlength{\logowidth}{110mm} % width of logo in PPT slide

\includegraphics[width=\logowidth*\ratio{\beamerwidth}{\pptwidth}]{logo.png}


This works just fine. Nevertheless, I will also need to scale many other figures and lengths in the same manner. For this purpose, I would like to be able to store the result of \ratio{\beamerwidth}{\pptwidth}, such that I can write something like

\newcommand{\widthscale}{\ratio{\beamerwidth}{\pptwidth}}
\includegraphics[width=\logowidth*\widthscale]{logo.png}


However, this does not seem to work. I also tried solutions with \def, \edef, \expandafter that I found elsewhere, but they all seem to fail.

Is it possible to save such a \ratio in a variable, such that it can be used later on?

• Macro \SetToRatio from tex.stackexchange.com/a/383700/4686 ? – user4686 Jun 22 '18 at 8:22
• @jfbu For the specific case that I am asking about, that might indeed also work. However, I plan to use this same method for other scalings, where the arguments of \ratio might have different dimensions (eg. pt and mm). For those situations I think the \ratio command from the calc package is very useful. – JorenV Jun 22 '18 at 8:47
• As long as the arguments you use, whether with pt, mm, or in are genuine dimension expressions (or dimen registers or LaTeX length variables), \SetToRatio will work. – user4686 Jun 22 '18 at 9:00
• calc package was for an era pre-dating wide use of e-TeX extension. It is sort of obsoleted by \dimexpr and friends. Although of course you can enjoy its \widthof, but this is but one line of TeX code or two. – user4686 Jun 22 '18 at 9:02
• The main reason calc package is used is that the reference books (Lamport's, the Companion) describe an old era, which is obsoleted since 2004 for TeXLive. And of course many packages use it, so its there even without the LaTeX user asking for it, often. – user4686 Jun 22 '18 at 9:45

The \ratio command in calc does not compute the quotient directly, it is an intermediate construction for use in other calculations. What you need to is to include this in a complete calculation so the result becomes a real. The simplest way to do this, is to multiply by 1:

 \edef\widthscale{1*\ratio{\beamerwidth}{\pptwidth}}


\documentclass{article}

\usepackage{graphicx}
\usepackage{calc}

\newlength{\pptwidth}
\setlength{\pptwidth}{254mm} % width of PPT slide
\newlength{\beamerwidth}
\setlength{\beamerwidth}{160mm} % width of beamer slide
\newlength{\logowidth}
\setlength{\logowidth}{110mm} % width of logo in PPT slide

\newcommand{\widthscale}{1*\ratio{\beamerwidth}{\pptwidth}}

\begin{document}

\includegraphics[width=\logowidth*\ratio{\beamerwidth}{\pptwidth}]{example-image-a.png}

\includegraphics[width=\logowidth*\widthscale]{example-image-a.png}
\end{document}

• @jfbu Thanks for the suggestion! Using \newcommand instead of \edef makes sure that things do not get defined multiple times I guess? – JorenV Jun 22 '18 at 8:53
• @AndrewSwann you have updated the MWE but not the first paragraph :) (deleting my comments) – user4686 Jun 22 '18 at 10:08

I would use expl3 instead of calc:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand\dimratio { m m }
{
\fp_eval:n
{
\dim_to_fp:n { #1 } /  \dim_to_fp:n { #2 }
}
}
\ExplSyntaxOff

\newlength{\pptwidth}
\setlength{\pptwidth}{254mm} % width of PPT slide
\newlength{\beamerwidth}
\setlength{\beamerwidth}{160mm} % width of beamer slide
\newlength{\logowidth}
\setlength{\logowidth}{110mm} % width of logo in PPT slide

\newcommand\myscale{\dimratio{\beamerwidth}{\pptwidth}}
\usepackage{graphicx}
\begin{document}

\includegraphics[width=\myscale\logowidth]{example-image.png}

\end{document}


You can also store the computed value with

 \edef\myscale{\dimratio{\beamerwidth}{\pptwidth}}