Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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=10000% 
\@starttoc{toc}%
\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?

share|improve this question
4  
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. –  Martin Scharrer Apr 15 '12 at 12:09
add comment

3 Answers 3

up vote 6 down vote accepted

A simple solution is:

\newcount\temphyphenpenalty
\temphyphenpenalty=\hyphenpenalty
\hyphenpenalty=10000
\@starttoc{toc}%
\hyphenpenalty=\temphyphenpenalty %% revert to default
share|improve this answer
    
Thanks, it works fine! –  Denis Apr 15 '12 at 12:17
add comment

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

\begingroup
\hyphenpenalty=10000
\@starttoc{toc}%
\par\endgroup

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:

\mathchardef\savedhyphenpenalty=\hyphenpenalty
\hyphenpenalty=10000
\@starttoc{toc}
\hyphenpenalty=\savedhyphenpenalty

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.

share|improve this answer
add comment

(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:
\newcount\yourtempcount

% Save and restore:
\yourtempcount=\hyphenpenalty
\hyphenpenalty=10000
\@starttoc{toc}%
\hyphenpenalty=yourtempcount

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:
\edef\myrestorecode{\hyphenpenalty=\the\hyphenpenalty\relax}
\hyphenpenalty=10000% 
\@starttoc{toc}%
\myrestorecode

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:

\edef\myrestorecode{%
     \hyphenpenalty=\the\hyphenpenalty\relax
     \someothercountordim=\the\someothercountordim\relax
     % ...
}

It works both count and dimension/skip registers.

share|improve this answer
    
Amazing, thanks for share it! –  Denis Apr 15 '12 at 12:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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