[Tex/LaTex] Save length with \let command

lengths

I want to save \fboxsep length in \oldfboxsep and use it later. The following code doesn't work:

\newlength{\oldfboxsep}
\let\oldfboxsep\fboxsep % 3pt by default
\setlength{\fboxsep}{8pt}
\fbox{\begin{minipage}{.9\textwidth}
  \setlength{\fboxsep}{\oldfboxsep}
  Text\fbox{Box}Text
\end{minipage}}

While the following code works:

\newlength{\oldfboxsep}
\setlength{\oldfboxsep}{\fboxsep} % 3pt by default
\setlength{\fboxsep}{8pt}
\fbox{\begin{minipage}{.9\textwidth}
  \setlength{\fboxsep}{\oldfboxsep}
  Text\fbox{Box}Text
\end{minipage}}

What are the differences between these two ways?

Best Answer

When you \let a length/dimension to another, the newly let length points to the same register. In similar respects one might think of this \letting as making a pointer (in programming language) to an existing data structure. For example, consider the following MWE:

enter image description here

\documentclass{article}
\begin{document}

\let\oldfboxsep\fboxsep % 3pt by default
\show\fboxsep
\show\oldfboxsep

\verb|\fboxsep|: \the\fboxsep\par
\verb|\oldfboxsep|: \the\oldfboxsep

\setlength{\fboxsep}{10pt}

\verb|\fboxsep|: \the\fboxsep\par
\verb|\oldfboxsep|: \the\oldfboxsep

\setlength{\oldfboxsep}{15pt}

\verb|\fboxsep|: \the\fboxsep\par
\verb|\oldfboxsep|: \the\oldfboxsep

\end{document}

To be more specific, the \show commands add the following to the .log:

> \fboxsep=\dimen36.
l.5 \show\fboxsep


> \oldfboxsep=\dimen36.
l.6 \show\oldfboxsep

Of course, the proper way is to define a new length and setting the one length to the other, as you did in your second example.

Perhaps, in a very likewise manner (from the TeX Book):

Is there a significant difference between \let\a=\b and \def\a{\b}?

Yes indeed. In the first case, \a receives the meaning of \b that is current at the time of the \let. In the second case, \a becomes a macro that will expand into the token \b whenever \a is used, so it has the meaning of \b that is current at the time of use. You need \let, if you want to interchange the meanings of \a and \b.

Related Question