[Tex/LaTex] “Missing \cr inserted”, but it looks to me like it’s already there!

plain-tex

I'm trying to create a Plain TeX macro for typesetting two-column proofs with automatic step-numbering and step cross-referencing; currently, the "best" I have is:

\newcount\stepno
\def\twocols#1{%
 \everycr={\advance\stepno by 1}
 \def\mkstep##1{%
    \expandafter \ifx \csname##1step\endcsname \relax%
       \expandafter\edef\csname##1step\endcsname{\the\stepno}%
    \else \message{^^JWarning: attempt to define ##1 more than once^^J^^J}%
      \stepref{##1}%
    \fi}%
 \def\stepref##1{%
    \expandafter \ifx \csname##1step\endcsname \relax {\tt[##1]}%
    \else \csname##1step\endcsname%
    \fi}%
 $$\vbox{
  \offinterlineskip
  \halign{
   \strut\the\stepno. ##\hfil & \vrule ##\hfil\cr%
   %\noalign{\hrule\smallskip}
   #1 \cr
  }
 }$$
}

\twocols{
 \mkstep{A} $(p\to q)\land (q\to r)$ & premise \cr
 \mkstep{B} $p\to (q\land(q\to r))$ & \stepref{A}, logic07 \cr
 $p\to r$ & \stepref{B}, {\it modus ponens}
}

\bye

Unfortunately, trying to typeset this yields the following inexplicable error:

This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
 %&-line parsing enabled.
entering extended mode
(./twocols02.tex
! Missing \cr inserted.
<inserted text>
                \cr 
<to be read again>
                   }
\twocols ... ##\hfil & \vrule ##\hfil \cr #1 \cr }
                                                   }$$ 
l.28 }

? X
No pages of output.
Transcript written on twocols02.log.

Why is it claiming there's a missing \cr where there clearly appears to be one? I've tried removing the \cr from the macro definition and adding a \cr to the end of the macro argument, replacing the \cr with \crcr, removing the newline between the two closing braces there, using two \crs, dispensing with the macro and trying to typeset what it should expand to, and various combinations thereof, yet I keep getting this error, and I'm out of ideas. What's going on?

(Also, I suspect that using \mkstep the way I am will cause the resulting reference macros to be scoped to just the column in which \mkstep is used, rather than to the whole \twocols; is there a way to fix that without making the reference macros global?)

Best Answer

 \everycr={\noalign{\global\advance\stepno by 1}}%

you are inserting tokens after the last explicit \cr so need to make sure they don't start a new row.