An easy and intuitive solution is to use the package calculator
. It enables variables handling (definition, use and calculation) easily and without needing LuaTeX, nor eTeX.
To affect a value to a variable, you use \COPY{<value>}{\myVariable}
(yes, it's upper case). You can then print/use your variable value as simply as \myVariable
.
To do some calculation, you do \ADD{<a>}{<b>}{<command_that_is_a+b>}
(the same with \SUBTRACT
,\MULTIPLY
,\DIVIDE
... see manual p. 6 and after for more detail).
Below are shown three MWE that compute following output (twice the same sentence, with only a change in variables values):

- MWE1: Intuitive and easy to implement (but does not follow your syntax).
=> good for those who want to do some floating point calculations in LaTeX.
- MWE2: Implements the
\bind{<letter>}{<value>}
into MWE 1, which uses TeX
macros (so less obvious to replicate for LaTeX
-learners). It follows your syntax, except for the \use
-command.
- MWE3: [Work in progress] Follows all your syntax.
MWE1
This MWE changes your syntax. It is however easy to use and reuse, since it only loads a LaTeX
-package and used macro are explicit. This might thus be adequate for people who wants to do "floating point calculations in LaTeX".
\documentclass{scrartcl}
\usepackage{calculator}
\usepackage{xspace}% xspace "smartely" adds or not a space at the end of the macro
\newcommand*{\printExpectedValue}{%
\MULTIPLY{\myT}{\myS}{\myV}% compute "V" value
\GLOBALCOPY{\myV}{\myV}% makes "V" a global constant: needed to compute SucessfulRate
$\myV$\xspace% print "V" in mathmode
}
\newcommand*{\printSucessfulRate}{%
\SUBTRACT{\myV}{\myF}{\intermediateResultA}% does the intermediary calculation
\DIVIDE{\intermediateResultA}{\myV}{\intermediateResultB}%
\MULTIPLY{\intermediateResultB}{100}{\intermediateResultC}%
\ROUND[0]{\intermediateResultC}{\mySucessfulRate}%
$\mySucessfulRate$\%\xspace% prints result in math-mode
}
\begin{document}
\COPY{21}{\myT}% defines \myT
\COPY{13}{\myS}% defines \myS
\COPY{37}{\myF}% defines \myF
The first experiment included running a battery of $\myT$ tests on $\myS$ subjects, for a total of \printExpectedValue expected values.
However, since $\myF$ values were defective, our successful measurement rate per subject was \printSucessfulRate.
\COPY{42}{\myT}% redefines \myT
\COPY{17}{\myS}% redefines \myT
\COPY{210}{\myF}% redefines \myT
The second experiment included running a battery of $\myT$ tests on $\myS$ subjects, for a total of \printExpectedValue expected values.
However, since $\myF$ values were defective, our successful measurement rate per subject was \printSucessfulRate.
\end{document}
MWE2
This MWE replicates the \bind{<letter>}{<value>}
command. However, it therefor uses plain-TeX commands, which might be not indicated if you don't know what you are doing.
\documentclass{scrartcl}
\usepackage{calculator}
\usepackage{xspace}% xspace "smartely" adds or not a space at the end of the macro
\newcommand*{\bind}[2]{%
\expandafter\def\csname my#1\endcsname{#2}% defines the \myX macro
$#2$\xspace%\prints the "X" value in math-mode
}
\newcommand*{\printExpectedValue}{%
\MULTIPLY{\myT}{\myS}{\myV}% compute "V" value
\GLOBALCOPY{\myV}{\myV}% makes "V" a global constant: needed to compute SucessfulRate
$\myV$\xspace% print "V" in mathmode
}
\newcommand*{\printSucessfulRate}{%
\SUBTRACT{\myV}{\myF}{\intermediateResultA}% does the intermediary calculation
\DIVIDE{\intermediateResultA}{\myV}{\intermediateResultB}%
\MULTIPLY{\intermediateResultB}{100}{\intermediateResultC}%
\ROUND[0]{\intermediateResultC}{\mySucessfulRate}%
$\mySucessfulRate$\%\xspace% prints result in math-mode
}
\begin{document}
The first experiment included running a battery of \bind{T}{21} tests on \bind{S}{13} subjects, for a total of \printExpectedValue expected values.
However, since \bind{F}{37} values were defective, our successful measurement rate per subject was \printSucessfulRate.
The second experiment included running a battery of \bind{T}{42} tests on \bind{S}{17} subjects, for a total of \printExpectedValue expected values.
However, since \bind{F}{210} values were defective, our successful measurement rate per subject was \printSucessfulRate.
\end{document}
MWE3
Still struggling with it. Any help is welcome (-;
calculator
packages seems also indicated, since it's easy to implement, and works withpdflatex
.