# Is there a way to update a length which has been computed from other lengths?

If I define a length by using others lengths, is there a way to update this length at a later point without giving the definition again?

Let's say I define a few lengths as follows

\newlength{\Test}\setlength{\Test}{20pt}
\newlength{\Tist}\setlength{\Tist}{30pt}
\newlength{\Tost}\setlength{\Tost}{\dimexpr \Test + \Tist}

\the\Test\ +
\the\Tist\ =
\the\Tost


This will yield

20.0pt + 30.0pt = 50.0pt


If I then at a later point redefine one of the lengths used in the computation, like,

\setlength{\Test}{40pt}


is there a way to "update" the dependant length without explicitely giving the computation formula? Currently it does not update:

\the\Test\ +
\the\Tist\ =
\the\Tost


will now yield

40.0pt + 30.0pt = 50.0pt

• Don't use a length for \Tost but a macro: \newcommand*{\Tost}{\dimexpr \Test+\Tist\relax}. Oct 5 '17 at 14:46

If you define \Tost as a macro, it will be expanded at usage time and therefore the result will always use the current values of \Test and \Tist:

\documentclass{article}
\newlength{\Test}\setlength{\Test}{20pt}
\newlength{\Tist}\setlength{\Tist}{30pt}
\newcommand*{\Tost}{\dimexpr \Test + \Tist\relax}

\begin{document}
\the\Test\ +
\the\Tist\ =
\the\Tost

\setlength{\Test}{40pt}

\the\Test\ +
\the\Tist\ =
\the\Tost

\end{document}


Will result in:

You could do something like this:

\documentclass{article}
\newlength{\Test}\setlength{\Test}{20pt}
\newlength{\Tist}\setlength{\Tist}{30pt}
\newlength{\Tost}\setlength{\Tost}{\dimexpr \Test + \Tist}

\newcommand{\updatelength}[2]{%
\setlength{#1}{#2}%
\setlength{\Tost}{\dimexpr \Test + \Tist}%
}

\begin{document}
\the\Test\ +
\the\Tist\ =
\the\Tost

\updatelength{\Test}{40pt}

\the\Test\ +
\the\Tist\ =
\the\Tost
\end{document}


I think you need a macro and not a length for this. setlength command works once and gives the value. A macro can be called at any time and calculate again. So you can put the calculation inside a macro:

\documentclass{article}
\newlength{\Test}\setlength{\Test}{20pt}
\newlength{\Tist}\setlength{\Tist}{30pt}
\newlength{\Tost}
\def\recalc{
\setlength{\Tost}{\dimexpr \Test + \Tist}
}

\begin{document}
\recalc

\the\Test\ +
\the\Tist\ =
\the\Tost

\setlength{\Test}{40pt}
\recalc

\the\Test\ +
\the\Tist\ =
\the\Tost

\end{document}


Output: