Define numeric variables

\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


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


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