4

I would like to have a command which executes a simple division of numbers. This \newcommand{\mylen}[1]{1.0 / #1} just prints 1.0 / 3 when invoked as \mylen{3} and not the desired 0.33333. What do I have to do?

Edit: The result should be usable in an expression like \includegraphics[width=\mylen{3}\textwidth]{a}

  • For the simple usage using \dimexpr is OK. For something more complicated, you might want to use `luatex. See the section on Arithmetic without using abacus on the ConTeXt wiki. – Aditya Aug 18 '13 at 21:57
5

If you want this for lengths then it is easy,

\the\dimexpr 1pt/3\relax

produces 0.33333pt. If you don't want the explicit units then the simplest way is to first add pt then remove it from the result.

Or there are several packages that do more exact floating point arithmetic, notably pgf or fp or l3fp

  • I would like to use the result in an expression like \includegraphics[width=\mylen{3}]{...} or actually with \setkeys{Gin}{width=\mylen{3}\textwidth} :) – Danvil Aug 18 '13 at 20:23
  • [width=\dimexpr\textwidth/3] is all you need in that case – David Carlisle Aug 18 '13 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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