I am using the fontdimen code in this answer to make spaces stretch in inline code samples (credit to Stefan Kottwitz):
\documentclass{minimal}
\usepackage{lipsum}
\newcommand*\justify{%
\fontdimen2\font=0.4em% interword space
\fontdimen3\font=0.2em% interword stretch
\fontdimen4\font=0.1em% interword shrink
\fontdimen7\font=0.1em% extra space
\hyphenchar\font=`-% allowing hyphenation
}
\begin{document}
\texttt{\justify\lipsum[1]}
\end{document}
Unfortunately, the effect of this \justify command is global: all subsequent \texttt material has variable-width spaces, as can be seen by adding an additional \texttt{\lipsum[1]} at the end of the document: the previous \justify command applies to it as well.
How can I save and restore \fontdimen parameters, so as to locally change the width and stretch of interword spaces?


minimal; it's not for examples. – egreg Nov 28 '21 at 21:28