TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my document, I have a user-defined variable like:


Suppose, if I want to use a derivative of \MyNumber, it could be \MyNumber+10, 100-\MyNumber, 5*\MyNumber of \MyNumber/5, can that be done?

One step forward, if I have:



Can I use, \MyNumberA+\MyNumberB?

share|improve this question
There are several solutions. For example, my package calculator. – Robert Fuster Mar 23 '14 at 12:43
Possible duplicate: How can I define a mathematical function as a LaTeX macro? – Henri Menke Mar 23 '14 at 13:01
up vote 6 down vote accepted

You can use etex capabilities for simple calculations:

\numexpr (\MyNumberA + 2* \MyNumberN)/3 \relax `

computes a weighted means of \MyNumberA and \MyNumberB.

Similarly, you can do calculations with dimensions, using \dimexpr.

share|improve this answer
You can add that you have to add \the if you want to print the result. – karlkoeller Mar 23 '14 at 16:11

You can do all of this using Plain TeX/LaTeX counters.

\pagestyle{empty}% for cropping
\def\tp#1{\texttt{\string\MyNumber #1} =}%
\tp{} \the\MyNumber
\advance\MyNumber by 10\relax%
\tp{ + 10} \the\MyNumber
\multiply\MyNumber by 100\relax%
\tp{ * 100} \the\MyNumber

Adding numbers:
\the\MyNumberA + \the\MyNumberB = \the\numexpr\MyNumberA + \MyNumberB\relax

enter image description here

share|improve this answer

Your Answer


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.