3

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?

6

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.

  • 2
    You can add that you have to add \the if you want to print the result. – karlkoeller Mar 23 '14 at 16:11
2

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
\advance\MyNumber by 10\relax%
\tp{ + 10} \the\MyNumber
\multiply\MyNumber by 100\relax%
\tp{ * 100} \the\MyNumber

\medskip
Adding numbers:
\newcount\MyNumberA
\newcount\MyNumberB
\MyNumberA=30
\MyNumberB=60
\the\MyNumberA + \the\MyNumberB = \the\numexpr\MyNumberA + \MyNumberB\relax
\end{document}

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.