TransWikia.com

Define numeric variables

TeX - LaTeX Asked by SoftTimur on October 29, 2020

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?

3 Answers

documentclass{article}

newcommandx{30}
newcommandy{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
 thenumexprx+yrelax. 

end{document}

output-

enter image description here

Correct answer by David Carlisle on October 29, 2020

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 
thenumexprvalue{x}+value{y}.

Answered by Steven Penny on October 29, 2020

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

documentclass{article}
usepackage{xfp}

newcommandx{30}
newcommandy{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).

Answered by egreg on October 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP