13

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

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

? 
  • Why not directly \showthe\gluestretch\myskip? – Ulrike Fischer May 19 '17 at 17:59
  • 1
    @UlrikeFischer well yes if you just want to show it but I wanted to (one way) show how to grab the value for use in tex, then the \show to just show whet was grabbed – David Carlisle May 19 '17 at 18:00
  • 1
    I thought your z-reservoir was overflowing and you needed to push some out ;-). – Ulrike Fischer May 19 '17 at 18:02
  • @UlrikeFischer unfortunately I was just assuming pdftex otherwise I'd have used \🦆 – David Carlisle May 19 '17 at 18:03
  • 1
    \edef\🦆 actually works with pdflatex ;-) (but better don't load inputenc ...) – Ulrike Fischer May 19 '17 at 18:22
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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