# Unexpected conditional branch with \ifdim

I have a problem with the following code:

\newtoks\sectoks
\sectoks={\noindent}

\newtoks\subsubjectstyle
\subsubjectstyle={\emitsectglue 1\the\sectoks}

\newtoks\postsectoks
\postsectoks={\par\smallskip\noindent\kern-1sp\hskip1sp}

\long\def\subsubject#1\par{%
{\the\subsubjectstyle#1}\the\postsectoks}

\def\emitsectglue#1{%
\ifdim\lastskip=1sp
\nobreak
\else
\vskip0pt plus#1\baselineskip
\penalty-\numexpr#1*100+50\relax
\vskip0pt plus-#1\baselineskip
\vskip#1\baselineskip
\fi}

\long\def\blockquote#1\eol{%
\emitsectglue 1
\begingroup
\raggedright\narrower\noindent #1
\smallskip
\endgroup\noindent}

\tracingall

\subsubject
The following subsubject will choose the first if's true-branch.

\subsubject
But the following blockquote will not. Why is that?

\blockquote
Be conservative in what you send, liberal in what you receive
\eol

\bye


When using the \subsubject command followed by another \subsubject, the macros work as expected and do not add the vertical glue & negative penalty.

However, when using the exact same code with another command, all of a sudden the right \if-branch doesn't get selected. I don't understand why is that.

-
Could you think of a better question title? Problem with X titles tend to be not the best titles ... –  doncherry Mar 14 '12 at 9:42
@doncherry: You're right, the title isn't good. But I'm having trouble coming up with something descriptive. Would you have a suggestion? –  morbusg Mar 14 '12 at 10:06
Not really, unfortunately. This question generally is too hardcore-TeX-y for me to understand. How about Plain-TeX \if doesn't work as expected? But perhaps @egreg can come up with an even better title? –  doncherry Mar 14 '12 at 10:16
That italic backslash looks funny :) –  doncherry Mar 14 '12 at 10:17
@doncherry: hmm, \if is a TeX primitive, not plain-tex IIRC, so maybe this title is better? –  morbusg Mar 14 '12 at 13:34
show 5 more comments

Add \relax after \hskip1sp: TeX expands tokens when looking for a skip specification and, when \blockquote follows, the specification is not yet complete, so the expansion evaluates \ifdim before the skip is inserted.

If \subsubject follows, there is no problem, because the first token in the replacement text is { which stops expansion.

I would brace the argument to \emitsecglue and get rid of a couple of possible spurious space sources:

\newtoks\sectoks
\sectoks={\noindent}

\newtoks\subsubjectstyle
\subsubjectstyle={\emitsectglue{1}\the\sectoks}

\newtoks\postsectoks
\postsectoks={\par\smallskip\noindent\kern-1sp\hskip1sp\relax}

\def\subsubject#1\par{%
{\the\subsubjectstyle#1}\the\postsectoks}

\def\emitsectglue#1{%
\ifdim\lastskip=1sp
\nobreak
\else
\vskip0pt plus#1\baselineskip
\penalty-\numexpr#1*100+50\relax
\vskip0pt plus-#1\baselineskip
\vskip#1\baselineskip
\fi}

\long\def\blockquote#1\eol{%
\emitsectglue{1}%
\begingroup
\raggedright\narrower\noindent #1%
\smallskip
\endgroup\noindent}

\subsubject
The following subsubject will choose the first if's true-branch.

\subsubject
But the following blockquote will not. Why is that?

\blockquote
Be conservative in what you send, liberal in what you receive
\eol

\bye

-