# Define numeric variables

I would like to define some variables in a tex document. For example, I write in .tex something similar as: Our tool succeeds to validate x samples by Method A, and y samples by Method B. Thus its score is z. Where z is defined as x+y, and x (resp. y) is instanced somewhere else by a number, eg., 30 (resp. 50). As a result, after the compilation the text turns out to be Our tool succeeds to validate 30 samples by Method A, and 50 samples by Method B. Thus its score is 80.

The advantage of this, is that I just need to change the value of some variables, to change all the numbers in the text...

Does anyone know how to achieve this?

• Your example doesn't need any arithmetic, just \newcommand\x{80} ... \x score is \x\% if you need arithmetic does it just need integers or floating point values (quite different answers in those cases) – David Carlisle May 5 '14 at 14:15
• Well, I made the OP a little bit more complicated... – SoftTimur May 5 '14 at 14:26
• You didn't answer the question about whether you need floating point values. \the\numexpr 5 / 6\relax divides 5 by 6 returning an integer but TeX doesn't have floating point values so if you need non integer values you'd need to use lengths \the\dimexpr 5pt /6 \relax and then remove the pt (or use a package that does that) – David Carlisle May 5 '14 at 14:31
• OK, I don't need the floating point values... – SoftTimur May 5 '14 at 14:34
• I just amended the OP... – SoftTimur May 5 '14 at 14:39

\documentclass{article}

\newcommand\x{30}
\newcommand\y{50}
\begin{document}

I would like to define some variables in a tex document. For example,
I write in .tex something similar as: Our tool succeeds to validate \x\
samples by Method A, and \y\ samples by Method B. Thus its score is
\the\numexpr\x+\y\relax.

\end{document}


output-

• Thank you... I see how you define x and y, and what if I still want to define a newcommand z which is x+y? How to write this definition? – SoftTimur May 5 '14 at 23:00
• @SoftTimur \newcommand\myadd[2]{\the\numexpr(#1)+(#2)\relax}} ... \myadd\x\y – David Carlisle May 5 '14 at 23:05
• That works... just another question: how much space does the second \ of \x\ add after a number? is it exactly equivalent to a normal space after a number? – SoftTimur May 5 '14 at 23:53
• @SoftTimur yes. – David Carlisle May 6 '14 at 0:04
• @DavidCarlisle why does this not support in pgf-umlcd? I try - \begin[text width = \x\ cm]{interface}{\x\, \y\}. – alhelal Oct 15 '17 at 13:44

You can use newcounter for this:

\newcounter{x}
\setcounter{x}{30}
\newcounter{y}
\setcounter{y}{50}
Our tool succeeds to validate \arabic{x} samples by Method A, and \arabic{y}
samples by Method B. Thus its score is \the\numexpr\value{x}+\value{y}.


You can use expl3 features, in particular the xfp package, which features \fpeval and \inteval.

\documentclass{article}
\usepackage{xfp}

\newcommand\x{30}
\newcommand\y{50}

\begin{document}

I would like to define some variables in a \TeX{} document. For example,
I write in \texttt{.tex} something similar as: Our tool succeeds to
validate \inteval{\x} samples by Method~A, and \inteval{\y} samples by
Method~B. Thus its score is \inteval{\x+\y}.

\end{document}


Of course you could also use \x{} instead of \inteval{\x}, but with the \inteval method you can also do

\newcommand{\z}{\x+3*\y}


and use \inteval{\z}.

This assumes operations on integer; if you need floating point, you can use \fpeval instead of \inteval (but things get more complex).