# An environment for setting \emergencystretch locally

The following environment, copied from this website about dealing with overfull hboxes, allows one to change the \tolerance parameter for just the paragraph(s) it encloses:

\newenvironment{tolerant}[1]{%
\par\tolerance=#1\relax
}{%
\par
}


Unfortunately, the website does not specify a similar environment for locally changing \emergencystretch. Since much of the code in the definition above (notably, the commands \par and \relax) is currently outside my understanding, I would not know how to go about making a similar environment for locally setting \emergencystretch--but I'm sure someone on TeX.SE would know.

How would one write an environment that changes the value of \emergencystretch locally (and makes sure the changes get applied)?

-

Change \tolerance to \emergencystretch and pass in a length rather than a number.

\par means end of paragraph and is same as a blank line. \relax is a primitive command which does nothing but it stops the scan for a number in case the text started with a digit.

\tolerance=1\relax 2 is ...


sets tolerance to 1 and typesets a 2 but without the \relax or a space then

 \tolerance=12 is..


would set it to 12 and not typeset the 2.

-

TeX uses the settings for \tolerance or \emergencystretch at the end of the paragraph. Therefore the first \par ends the current paragraph that is typeset with the old settings. The \par in \end{emergency} makes sure that also the latest paragraph in the environment gets finished and typeset with the special setting of the environment.

Because \emergencystretch is a dimen register, LaTeX's \setlength can be used to set \emergencystretch locally:

\newenvironment{emergency}[1]{%
\par
\setlength{\emergencystretch}{#1}%
}{%
\par
}

-

In fact, I'd do something like the following:

\newenvironment{xtolerant}[2]{%
\par
\ifx\empty#1\empty\else\tolerance=#1\relax\fi
\ifx\empty#2\empty\else\emergencystretch=#2\relax\fi
}{%
\par
}


That way, you can decide whether to set \tolerance or \emergencystretch or both. Just leave one environment argument empty for for not setting it.

Hence,

\begin{xtolerant}{300}{}
...
\end{xtolerant}


will set \tolerance to 300 and leave \emergencystretch alone,

\begin{xtolerant}{}{2em}
...
\end{xtolerant}


will set \emergencystretch to 2em and leave \tolerance alone, and

\begin{xtolerant}{300}{2em}
...
\end{xtolerant}


will set \tolerance to 300 and set \emergencystretch to 2em.

-