2

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?

Clément
  • 4,004
  • I should add that I've seen https://tex.stackexchange.com/questions/308872/saving-and-restoring-font-settings , but grouping does not save and restore font dimensions. – Clément Nov 28 '21 at 20:27
  • Please, don't use minimal; it's not for examples. – egreg Nov 28 '21 at 21:28
  • @egreg I copied the example literally from the answer that I linked to; I didn't want to change it in any way. – Clément Nov 28 '21 at 22:14

2 Answers2

3

You can save the values and re-set

\documentclass{minimal}

\usepackage{lipsum}

\def\savefdims{% \edef\restorefdims{ \fontdimen2\the\font=\the\fontdimen2\font\relax \fontdimen3\the\font=\the\fontdimen3\font\relax \fontdimen4\the\font=\the\fontdimen4\font\relax \fontdimen7\the\font=\the\fontdimen7\font\relax \hyphenchar\the\font=\the\hyphenchar\font\relax}% }

\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{\savefdims \typeout{\meaning\restorefdims}% \justify\lipsum[1]\restorefdims}

\texttt{\lipsum[1]}

\end{document}

enter image description here

The \typeout just to show the form of the saved command, the terminal will show (linebreaks added for clarity)

macro:->
   \fontdimen 2\OT1/cmtt/m/n/10 =5.24995pt\relax
   \fontdimen 3\OT1/cmtt/m/n/10 =0.0pt\relax
   \fontdimen 4\OT1/cmtt/m/n/10 =0.0pt\relax
   \fontdimen 7\OT1/cmtt/m/n/10 =5.24995pt\relax
   \hyphenchar \OT1/cmtt/m/n/10 =-1\relax 
David Carlisle
  • 757,742
3

\fontdimen assignments are implicitly global, as described in the TeXbook. Similarly also the assignment of \hyphenchar is global.

Instead of changing the \fontdimen parameters you can use \spaceskip and \xspaceskip.

You may define a \textttj command that does the job.

\documentclass[twocolumn]{article}
\usepackage{kantlipsum}

\DeclareRobustCommand{\textttj}[1]{% \begingroup \ttfamily \hyphenchar\font=`- % allowing hyphenation \setlength{\spaceskip}{0.8em plus 0.4em minus 0.2em}% \setlength{\xspaceskip}{1em plus 0.4em minus 0.2em}% #1% \endgroup } \AddToHook{cmd/ttfamily/after}{\hyphenchar\font=-1 }

\begin{document}

\textttj{\kant[1]}

\pagebreak

\texttt{\kant[1]}

\end{document}

Note that the em for the typewriter font is usually the same as the widths of letters, so I doubled the values.

enter image description here

egreg
  • 1,121,712