What is the syntax for storing some width in order to change it temporarily and then restore to the original?


%save length here
some text
more text
%restore length here
  • 2
    Put a {...} pair around your whole instruction or set the with inside your environment. – user31729 Sep 3 '16 at 19:03
  • Can I do something like \setlength\anotherlength{\somelength} then reverse that? Or do I need to somehow define \anotherlength first? – ajeh Sep 3 '16 at 19:03
  • 1
    Lengths have to be defined first anyway, but you don't need to use this explicit setting. Just use groups! – user31729 Sep 3 '16 at 19:09

Lengths are group safe, i.e. any change inside a {...} will be restricted to that group, unless explicitly made global. For example, lengths being set in tabular cell are changed in that cell only, in the next cells the value is not affected by this!

Either you place a {...} (or a \begingroup...\endgroup pair) or use the setting inside of the environment (if possible) -- environments form groups as well.

Third possibility: Define a scratch width with \newlength and either say \setlength{\helperwidth}{\somewidth} or the more TeX - like \helperwidth=\somewidth or even shorter: \helperwidth\somewidth, if really needed.

The definition of an extra length (or using another one that is not needed at the moment) has its advantages if the old definition ('value') must be used for some extra calculations etc.

The 'right' method depends on the actual requirements.


\newlength{\helperwidth}% Just for fun

Width is \the\somewidth%



more text
Now width is \the\somewidth%

Outside the width is \the\somewidth

Or using a group:

Now width is \the\somewidth%

Outside of the group the width is \the\somewidth

% Or \helperwidth\somewidth
Now width is \the\somewidth%

Outside of the environment the width is \the\somewidth

Now restoring the width: \the\somewidth


enter image description here

The same group feature is true for TeX count registers -- they are group safe, however LaTeX counters are not group safe!

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.