Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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

difficult to know that one has to search for \dimexpr

share|improve this answer

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).
share|improve this answer
    
The TeX primitive divide truncates too. –  Marco Daniel May 16 '13 at 19:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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