# How do \lastskip and \ifdim work?

I thought I knew the answer to this question, but I can't understand the following plain TeX code:

\noindent\hskip1pt\ifdim\lastskip=0pt lastskip was 0pt\fi\par
\noindent\hskip1pt\showthe\lastskip
\bye


In the first paragraph (started with \noindent) I put a skip of 1pt, but the test for the last skip yields 0pt. In the second paragraph, \showthe shows that the last skip was 1.0pt. What is happening here?

(I was led to this question by trying to answer morbusg's question.)

-

You are missing a \relax here: \ifdim is expandable, and so \hskip will keep expanding looking for a plus. Now, it does not find one but the test result has already been decided (true) before the \hskip takes place. In the second line, \showthe is not expandable, so \hskip stops and inserts the skip before \showthe is executed.

As Hendrik point's out in a comment, TeX will expand looking for an optional space for any dimension. Thus something like

\newskip\mydim
\mydim4pt\ifdim\mydim=4pt\else \ARG\fi
\showthe\mydim
\bye


gives a different result to

\newskip\mydim
\mydim4pt \ifdim\mydim=4pt\else \ARG\fi
\showthe\mydim
\bye


as in the later case the space terminates the assignment before the \ifdim test.

However, \hskip is a skip (rubber length), not a dimension. It therefore has optional stretch and shrink components in addition to the fixed length. So modifying the original code to

\noindent\hskip1pt \ifdim\lastskip=0pt lastskip was 0pt\fi\par
\noindent\hskip1pt \showthe\lastskip
\bye


does not change the outcome. TeX finds the optional space for 1pt but continues to expand looking for plus or minus. So the assignment can only take place on the first line when it hits the l, by which time the \ifdim result has already been determined.

-
Thanks, with \relax it works, great! The same happens for \kern1pt and \lastkern. I guess that TeX keeps expanding looking for an optional space after pt. Correct? – Hendrik Vogt Jan 6 '11 at 13:57
@Hendrik. This is a glue item, so there is an optional plus and/or minus to check for. – Joseph Wright Jan 6 '11 at 15:32
@Joseph: The optional plus or minus can only come before the number 1, or am I mistaken? I've had a look at the TeXbook. On page 270 it says <unit of measure> −-> <optional spaces><internal unit> | <optional true><physical unit><one optional space>. So now I'm pretty sure it's the one optional space after the physical unit pt. – Hendrik Vogt Jan 6 '11 at 20:14
@Hendrik. See the edit to my answer. – Joseph Wright Jan 6 '11 at 20:27
@Joseph: Thanks for the confirmation! (I totally misread your comment. I thought of an optional + (or --) that could come before the number 1, not about the optional plus and minus (which I do know, so I perfectly understood the first version of your answer, and somehow didn't expect you'd point those plus and minus out again). – Hendrik Vogt Jan 6 '11 at 21:30