9

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?

  • 3
    \let is used for command (macro) definitions only, not lengths. – Paul Gessler Jun 30 '14 at 2:11
6

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.

7

You want to save the value of \fboxsep and not its meaning. If you

\show\fboxsep

the answer will be

\fboxsep=\dimen36

In particular, \fboxsep is not a macro, so doing \let\foo\fboxsep will simply define \foo to be the same as \dimen36.

You can still do without defining a new length, by using the mighty \the (see The \the command):

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

When the inner \setlength instruction is performed, TeX will translate it into

\fboxsep=\savedfboxsep\relax

and it will expand \savedfboxsep because after = TeX is looking for a specification.


A possibly better way for realizing the same construction is using a box:

\newsavebox{\fminipagebox}
\newenvironment{framedminipage}
  {\begin{lrbox}{\fminipagebox}\begin{minipage}}
  {\end{minipage}\end{lrbox}%
   \setlength{\fboxsep}{8pt}%
   \fbox{\usebox{\fminipagebox}}}

The setting of \fboxsep to 8pt will be undone as soon as the framedminipage environment ends; it doesn't influence any \fbox command inside the environment, because that text has already been typeset.

Note that, since the \begin part ends with \begin{minipage}, you can (and should) specify the same arguments as if it were minipage, including optional ones.

\documentclass{article}
\usepackage{lipsum}
\newsavebox{\fminipagebox}
\newenvironment{framedminipage}
  {\begin{lrbox}{\fminipagebox}\begin{minipage}}
  {\end{minipage}\end{lrbox}%
   \setlength{\fboxsep}{8pt}%
   \fbox{\usebox{\fminipagebox}}}

\begin{document}
\lipsum[2]

\medskip

\begin{framedminipage}{.9\textwidth}
  Text\fbox{Box}Text
\end{framedminipage}

\medskip

\lipsum[3]

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.