I need to change 'hyphenpenalty' in some parts of my document, but I have no idea how to properly restore a previous value after that.

For example:

\hyphenpenalty=50 %% revert to default (??)

Not a good solution as I think. How can I save the previous value into a temporary variable and restore from it?

  • 6
    BTW: In the case of \someregister=10000 you don't need and should not add a %. The newline acts as space and a TeX will take a space as terminator for the number and remove it afterwards. With the % TeX actually looks at the beginning of the next line for further digits of the number and might even expand tokens/macros to find them until it finds something which isn't expandable or no digit. So having a space/linebreak or \relax after the number is actually a good thing. Apr 15, 2012 at 12:09

3 Answers 3


A simple solution is:

\hyphenpenalty=\temphyphenpenalty %% revert to default

There's no need to set counters for remembering the value:


will do without any need to remember or reset the old value, since TeX will restore the value of \hyphenpenalty at group end. I've added a final \par to be sure that the last paragraph will be set with the correct value of \hyphenpenalty.

If you don't want the group (in different circumstances it may be necessary), you can do without wasting a register:


A \mathchardef token can be used anywhere TeX is looking for a number. The only limitation is that the number to be saved must be nonnegative and less than 32768. However, the \hyphenpenalty is usually in this interval.


(La)TeX "variables" are either macros or registers. The registers can be counters (integers), dimensions, skips (both fractional numbers with units) or read/write handlers etc. Note that you can change them inside a group, i.e. either { .. } or \begingroup ... \endgroup or a LaTeX environment, then their old value is restored at the end of the group automatically.

The integer settings like \hyphenpenalty are basically special count registers. You can copy their current value into any other count register and copy it back afterwards, using \somecount=\someothercount, where the = is optional. (Note that count is a TeX type, the LaTeX counters are based on them)

LaTeX provides two temporary count registers called \@tempcnta and \@tempcntb. In general it is a good idea to only change them in a local group, but if you can add one you don't need to temporary store the value anyway. Some other code used by you might change such general temporary registers, so sometimes it might be required to use an own count register using \newcount\yourtempcount.

% In the preamble, only once:

% Save and restore:

It is also possible to do it without a temporary register, by putting the restore code into a (usually temporary) macro. Here the current value is expanded using \the:

% Save and restore:

This defines \myrestorecode to \hyphenpenalty=<current value, e.g. "50">\relax, because \hyphenpenalty on its own is not expandable. The \relax is added as an endmarker for the number. A single space (\space) would also be find. The benefit here is that you don't need a register (which requires to be declared first) and that this also works with multiple values:

     % ...

It works both count and dimension/skip registers.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .