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?


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.

  • 3
    You can add that you have to add \the if you want to print the result. Mar 23 '14 at 16:11
  • Thanks @karlkoeller, \the\numexpr (2 + 2) \relax even works within siunitx (e.g. \SI{\the\numexpr (2 + 2) \relax}{\meter})
    – F1iX
    Aug 5 '20 at 8:59

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

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.