I met the error You can't use `\spacefactor' in vertical mode. when using \@whilenum for a loop. Below is a minimum not-working example. Why is this wrong?
\documentclass{article}
\usepackage[utf8]{inputenc}
\title{loop}\author{}
\begin{document}
\maketitle
\@whilenum\value{section}<6\do{\section{more}}
\end{document}
Error Message
You can't use `\spacefactor' in vertical mode. \@->\spacefactor \@m {} l.6 \@ whilenum\value{section}<6\do{\section{more}} Sorry, but I'm not programmed to handle this case; I'll just pretend that you didn't ask for it. If you're in the wrong mode, you might be able to return to the right one by typing `I}' or `I$' or `I\par'.! Missing $ inserted. <inserted text> $ l.6 @ whilenum\value{section}<6\do{\section{more}} I've inserted a begin-math/end-math symbol since I think you left one out. Proceed, with fingers crossed.

@is not a valid character in a macro name (at least not in a.texfile), so you need to make it valid with\makeatletterbefore you intend to use it. – Lupino Dec 11 '22 at 04:34