In order to complete one custom package command I needed an internal macro, I called it \@test@count{❬argument❭}
, and should accomplish the following:
- Internally defines a new counter
\❬argument❭
and sets it to1
. Checks if the counter
\❬argument❭
is already defined, so it has to be conditionally dependent of the existence of previous counters:2.1. If
\❬argument❭
is already defined, then it'll not overwrite a previous value stored by\❬argument❭
(as if it has saved its declaration) but it will advance it by1
.2.2. If
\❬argument❭
isn't defined, then it'll simply create it.
If all of the above requirements are met, then the MWE (in this case \@test@count{❬argument❭}
is used to obtain an identical task for the package command \mycommand
):
\documentclass{article}
%
\makeatletter
\def\@test@count#1{%
%mysterious (La)TeX sorcery should happen here
}
\def\mycommand[#1]#2{%
\@test@count{#1}%
This is the value of the counter~"#1":~\expandafter\the\csname #1\endcsname~#2\par%
}
\makeatother
%
\begin{document}
%
\mycommand[myfoo]{lorem}
\mycommand[mybar]{ipsum}
\mycommand[mybaz]{dolor}
\mycommand[myfoo]{sit}
\mycommand[mybar]{amet}
\mycommand[myfoo]{consectetur}
\mycommand[mybaz]{adipisci}
%
\end{document}
should yield:
The text
has been inserted to show that the secondary argument can be filled without restrictions.
In the example, \mycommand[myfoo]{...}
defines the counter \myfoo
and sets it to 1
, because there are no other counters \myfoo
declared before; then \mycommand[mybar]{...}
defines \mybar
and so forth. When \mycommand[myfoo]{...}
is called again, then a \@test@count{myfoo}
will test whether \myfoo
is defined, in this case it yields "true" and then the counter is advanced by 1
. The same reasoning follows for any successive declaration of mybar
and mybaz
.
The rule for \@test@count{myfoo}
will be similar to an allocation manager, because when a new counter is defined by \mycommand[myfoo]{...}
it "saves" its previous value \myfoo
until another \myfoo
is declared: if this condition is met, then \myfoo
won't be overwritten, but instead incremented.
So far I managed little to none progress to define \@test@count#1{...}
, as this task requires this kind of knowledge that it's difficult for me to understand at the moment.