14

This might be a silly question, but I can't seem to be able to find the answer anywhere: suppose I have this simple plain TeX code:

\newskip\myskip
\myskip = 10pt plus 4pt minus 2pt

Now, how can I access (read) those stretch and shrink parts, those 4pt and 2pt values?

After David's answer: this is what I wanted:

%%%! tex
\newskip\spaceskipFixed

% nonsensical values just for testing
\spaceskipFixed = 10pt plus 4pt minus 2pt
\spaceskip = \spaceskipFixed

% wrong, glue becomes dimen, stretchability and shrinkability gone
\def\modifyspaces #1 {%
    \spaceskip = #1\spaceskipFixed
}

\def\text{Aha oho haha hoho!}
\newbox\gauge \setbox\gauge = \hbox{\modifyspaces .5 \text}
\def\showline #1#2{
    \line{\hbox to\wd\gauge{#1\hfill}\hskip 2em {\spaceskip = 0pt\tt\% #2}\hfill}
}

\line{\text\hskip 2em{\spaceskip = 0pt\tt \% unmodified, wild spaces}\hfill}
\vbox to 0pt{\noindent\kern \wd\gauge\hbox{\vrule width .1pt height 0pt depth 15mm}\par\vskip -15mm}
\showline{\modifyspaces .5 \text}{simple text with modified spaces, width \the\wd\gauge}
\showline{\hbox to 92pt {\modifyspaces .5 \text}}{hbox to 92pt, no shrinkability => overfull}

% fixed
\def\modifyspaces #1 {%
    \spaceskip = #1\spaceskipFixed plus #1\gluestretch\spaceskipFixed minus #1\glueshrink\spaceskipFixed
}
\showline{\hbox to 92pt {\modifyspaces .5 \text}}{hbox to 92pt, shrinkability preserved, all shiny}
\bye

enter image description here

Marcel S.
  • 1,313
  • Related: https://tex.stackexchange.com/questions/229292/displaying-rubber-lengths-in-arbitrary-units – ShreevatsaR May 19 '17 at 20:47
  • After your edit: the answer to your real question is here: https://tex.stackexchange.com/questions/198954/scaling-a-glue-in-tex -- simply using \divide would be cleanest for the above (0.5) case. – ShreevatsaR May 20 '17 at 01:27

2 Answers2

11

if you have etex extensions enabled you can do

\newskip\myskip
\myskip = 10pt plus 4pt minus 2pt

\edef\z{\the\gluestretch\myskip}
\show\z
\edef\z{\the\glueshrink\myskip}
\show\z
\bye

which produces

> \z=macro:
->4.0pt.
l.5 \show\z

? 
> \z=macro:
->2.0pt.
l.7 \show\z

? 
David Carlisle
  • 757,742
6

You have to take care of infinite glue:

\documentclass{article}

\makeatletter
\newcommand{\extractnatural}[1]{%
  \the\dimexpr#1\relax
}
\newcommand{\extractstretch}[1]{%
  \strip@pt\dimexpr\gluestretch#1\relax
  \ifcase\gluestretchorder#1pt\or fil\or fill\or filll\fi
}
\newcommand{\extractshrink}[1]{%
  \strip@pt\dimexpr\glueshrink#1\relax
  \ifcase\glueshrinkorder#1pt\or fil\or fill\or filll\fi
}
\makeatother

\newlength{\test}

\begin{document}

\setlength{\test}{10pt plus 4pt minus 2pt}
\texttt{\the\test}: (\extractnatural{\test}) (\extractstretch{\test}) (\extractshrink{\test})

\setlength{\test}{10pt plus 4fil minus 2pt}
\texttt{\the\test}: (\extractnatural{\test}) (\extractstretch{\test}) (\extractshrink{\test})

\setlength{\test}{10pt plus 4filll minus 2fill}
\texttt{\the\test}: (\extractnatural{\test}) (\extractstretch{\test}) (\extractshrink{\test})

\edef\macro{(\extractnatural{\test}) (\extractstretch{\test}) (\extractshrink{\test})}
\texttt{\meaning\macro}

\end{document}

enter image description here

egreg
  • 1,121,712