# Define numeric variable in terms of another variable

So, I have a variable called number which I define like so:

\def\number{0.25}

I'd like to automatically define a number which will give me 1-number (in this case, which would give me 0.75). I tried doing this:

\def\othernumber{1-\number}

However, this didn't seem to work. It seems like the engine doesn't realize that I want to subtract \number from 1. Could anyone help me fix this syntax? Thanks :)

• \def\number{0.25} is a very bad thing to do: \number is a TeX primitive. Can you tell something more about the usage of these macros? – egreg May 26 '17 at 20:34
• Oh, whoops. I just put that there – I probably wouldn't have used such a generic term. Let's say it's \spanvalue instead. Also, hello! – Skeleton Bow May 26 '17 at 20:36
• Depending on what you want to use the numbers for, tex.stackexchange.com/questions/245635/… may be useful. – Willie Wong May 26 '17 at 20:49
• Note that when you say \def\spanvalue{0.25}, you should not read it as “\spanvalue is now equal to the number 0.25”. This is not what happens; \def is meant for text substitution, and what happens is that TeX simply records the replacement text for the macro\spanvalue as being the sequence of tokens 0, ., 2 and 5. TeX does have some rudimentary support for arithmetic, but \def (or even \newcommand in LaTeX) is not it. And the support from packages like xfp and pgf (as in the answers) is much greater. – ShreevatsaR May 26 '17 at 22:00
• Ohh, now I understand! That definitely clears up why I'm having these problems. Thanks a lot for explaining! – Skeleton Bow May 26 '17 at 23:43

You can use pgf package for this. Instead TeX primitive \def one need use \pgfmathsetmacro

\documentclass{article}
\usepackage{pgf}

\begin{document}
\pgfmathsetmacro{\first}{0.25}
\pgfmathsetmacro{\second}{1-\first}

The value of \verb|\first| is \first

The value of \verb|\second| is \second
\end{document}

• Note that \pgfmathsetmacro will happily redefine existing commands without any warning. – egreg May 27 '17 at 8:33

You can use xparse and xfp (the latter allows computations on the fly):

\documentclass{article}
\usepackage{xparse,xfp}

\ExplSyntaxOn
\NewDocumentCommand{\defineconstant}{mm}
{
\cs_new:Npx #1 { \fp_eval:n { #2 } }
}
\ExplSyntaxOff

\begin{document}

\defineconstant{\constantA}{0.25}
\defineconstant{\constantB}{1-\constantA}
\defineconstant{\constantC}{round(sqrt(\constantA),2)+\constantB/2}

The value of \verb|\constantA| is \constantA

The value of \verb|\constantB| is \constantB

The value of \verb|\constantC| is \constantC

\fpeval{round(\constantA-\constantC+exp(2),4)}

\end{document}

• the power of l3 – Moriambar May 26 '17 at 20:51