# Perform simple calculations on user-defined variables

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

 \newcommand{\MyNumber}{30}


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:

\newcommand{\MyNumberA}{30}

\newcommand{\MyNumberB}{60}


Can I use, \MyNumberA+\MyNumberB?

-
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

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.

-
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.

\documentclass{article}
\pagestyle{empty}% for cropping
\begin{document}
\obeylines%
\newcount\MyNumber%
\MyNumber=30%
\def\tp#1{\texttt{\string\MyNumber #1} =}%
\tp{} \the\MyNumber
\tp{ + 10} \the\MyNumber
\multiply\MyNumber by 100\relax%
\tp{ * 100} \the\MyNumber

\medskip
`