33

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?

SoftTimur
  • 19,767
  • 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 05 '14 at 14:15
  • Well, I made the OP a little bit more complicated... – SoftTimur May 05 '14 at 14:26
  • 3
    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 05 '14 at 14:31
  • OK, I don't need the floating point values... – SoftTimur May 05 '14 at 14:34
  • I just amended the OP... – SoftTimur May 05 '14 at 14:39

3 Answers3

44
\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-

enter image description here

Stefan Pinnow
  • 29,535
David Carlisle
  • 757,742
14

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}

enter image description here

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

egreg
  • 1,121,712
10

You can use counters 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}.
Zombo
  • 1