# Set variable with mathematical expression

I need to do something like the following,

\includegraphics[width=(\textwidth - 50mm)/2]{myfig.eps}

What keywords can I use to search for doing this operation? Everything I've searched for was about mathematical typing instead of setting variable.

-

\includegraphics[width=\dimexpr(\textwidth - 50mm)/2]{myfig.eps}

difficult to know that one has to search for \dimexpr

-

## The LaTeX way: package calc

Package calc redefines \setlength, \addtolength, \setcounter, \addtocounter to support expressions. If another package uses these macros for setting length and counters with user provided values, then it inherits the power from package calc. It is enough to load the package, the other package does not even know about calc:

\usepackage{graphicx}
\usepackage{calc}
...
\includegraphics[width=(\textwidth - 50mm)/2]{myfig.eps}

## The eTeX way: \dimexpr

If the eTeX extensions are enabled (usually the default for LaTeX based formats nowadays), \dimexpr can be used, where a TeX length is expected (see Herbert's answer):

\includegraphics[width=\dimexpr(\textwidth-50mm)/2]{myfig.eps}

## Comparison

• Dependencies: Package calc vs. eTeX
• eTeX's \dimexpr, \numexpr, … are expandable.
• \dimexpr also works, if internally \setlength is not used, e.g. \vspace.
• I expect calc to be slower, because of the internal macro work that needs to be done.
• \dimexpr calculates with higher precision.
• Package calc truncates: 5/2 = 2
eTeX rounds: 5/2 = 3
• Package calc provides additional operators (\widthof, \heightof, \totalheightof, \maxof, \minof).
-
The TeX primitive divide truncates too. –  Marco Daniel May 16 '13 at 19:15