Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
There are several solutions. For example, my package calculator. –  Robert Fuster Mar 23 at 12:43
    

2 Answers 2

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
2  
You can add that you have to add \the if you want to print the result. –  karlkoeller Mar 23 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
\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

share|improve this answer

Your Answer

 
discard

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.