Latex gives seemingly unrelated errors if I use a macro with an @ in it while defining a macro. Errors are consistent across tectonic, pdflatex, and Overleaf, although Overleaf seems to power through the errors and produce a doc anyway.
Drilling down into an error from using \@whilenum in a macro definition, I got to the following minimal latex that reproduces the error:
\documentclass{article}
\makeatletter
\def\@atcommand #1{}
\makeatother
\begin{document}
\newcommand{\mymacro}{
\newcounter{x}
\makeatletter
\@atcommand{\value{x}}
\makeatother
}
\mymacro
Notempty % Ensure we don't get emptydoc
\end{document}
This gives the error:
! You can't use `\spacefactor' in vertical mode.
\@ ->\spacefactor
\@m {}
l.12 \mymacro
Or, if in a tikzpicture (as the original snippet was), Missing number, treated as zero.
Any of the following prevents there being an error: removing the @ from both the definition and use of the atcommand macro; lifting the definition of mymacro outside of a macro and running it directly; and calling atcommand with an integer value instead of the value of a counter.
Just curious what is going on. The question originated from a student asking why their document didn't compile: I've given them an alternative way to achieve what they wanted, but I want to get them a solid explanation of what was wrong with their first attempt.
\makeatletterinside the definition is doing nothing, you need it outside the definition – David Carlisle Sep 28 '22 at 11:31\newcounteroutside the definition, counters are a finite resource and you are allocating a new one on each use – David Carlisle Sep 28 '22 at 11:33\makeatletterbe used in a macro definition? – User3472 Sep 28 '22 at 11:48\@atcommandsame reason\verbcan not be used inside an argument – David Carlisle Sep 28 '22 at 11:52\csname @atcommand\endcsnamewhich instruct TeX to construct (and use) a macro with the name@atcommandmore or less whatever their catcodes. – Qrrbrbirlbel Sep 28 '22 at 12:53