I define a command \test
that does nothing but define a control sequence \c[count]
, where [count]
is the current text value of the counter count
at the time \test
is executed. I want \c[count]
to output the text "Number \thecount
", where \thecount
should be the value of count
at time of execution.
\documentclass[12pt]{article}
\newcounter{count}
\newcommand{\test}{\expandafter\def\csname c\thecount\endcsname{Number \thecount~}}
\begin{document}
\setcounter{count}{1}
\test
\setcounter{count}{2}
\test
\setcounter{count}{3}
\test
\csname c1\endcsname
\csname c2\endcsname
\csname c3\endcsname
\end{document}
This outputs: "Number 3 Number 3 Number 3". I believe this means that \thecount
in the output of \test
was not expanded during each \test
.
How can I fix this to display "Number 1 Number 2 Number 3" as intended? I'm unsure how to use \expandafter
for this, my attempts keep breaking everything. I relatively new to this sort of LaTeX, so I'd appreciate any help on this matter.
\def
by\edef
. You want the\thecount
to be expanded at the time it's defined, otherwise\thecount
will take the value of the counter at the time it's used