2

I have the following macro/command:

\def\definenewcounter#1{\newcount \csname#1\endcsname}

It is supposed to create a new count with it's name being whatever argument 1 is, but it is throwing errors. How can this be resolved?

Here is a fuller example:

\documentclass[]{article}

\def\definenewcounter#1{\newcount \csname#1\endcsname}

\begin{document}
\definenewcounter{countertest}
\end{document}
1
  • @DavidCarlisle that's not fair, the last question was using \advance which would scan for a number and hence auto-expand the \csname, whereas \newcount just takes the first token, which makes the \expandafter necessary.
    – Skillmon
    Apr 22 at 10:36

1 Answer 1

4

The code shown defines \csname to be a count register, and then will produce multiple errors.

You could use

\documentclass[]{article}

\def\definenewcounter#1{\expandafter\newcount \csname#1\endcsname}

\begin{document}
\definenewcounter{countertest}
\end{document}

But that seems a strange construct for LaTeX which already provides

\documentclass[]{article}

\begin{document}
\newcounter{countertest}
\end{document}
1
  • "But that seems a strange construct..." Indeed.
    – user287367
    Apr 22 at 0:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .