I have different counters, and I want to increment them. After I do not want to increment them separately, I wanted to write a macro which does that for me, depending on the input. I have the following code:
\documentclass[10pt,a4paper]{book}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{tocloft}
\newcounter{countera}
\newcounter{counterb}
\newcommand{\toC}[1]{
\stepcounter{%
\expandafter\csname counter#1\endcsname%
}
}
\begin{document}
\toC{a}
\end{document}
When I now want to increment counter countera
, I just want to call \toC{a}
instead of \stepcounter{countera}
. By using the code above, I get a lot of errors, such as ! Missing \endcsname inserted.
, ! Extra \endcsname.
and ! LaTeX Error: No counter '\countera ' defined.
The macro \toC{}
will be expanded later, thus I would prefer to keep its structure like this, if possible. Why do I get those errors?
\csname counter#1\endcsname
here