# Create macro depending on counter at point of definition

I have a document with various numbered objects, e.g., term rewrite systems with numbered function symbols like this:

While I need a numbered output for these symbols (it should illustrate the output of an automated analysis - and in fact the variables will also be numbered in the end), in my examples they correspond to some semantical objects. So for the system in the picture I would have code like this:

\documentclass{article}

\newcommand{\initstate}{f_1}
\newcommand{\processstate}{f_2}

\begin{document}

$\initstate(x,y,z) \to \processstate(x,z)$

$\initstate(0,0,z) \to z$

$\processstate(s(x),y) \to \initstate(x,x,y)$

\end{document}


Now the problem is that whenever I would like to extend such a system by a new symbol which is not the last one (or if want to reorder the numbers), I need to change all the commands manually (e.g., if I add another symbol and use the command \storestate for the symbol f_2, then I need to change the command \processstate to f_3). So the example would then look like this:

\documentclass{article}

\newcommand{\initstate}{f_1}
\newcommand{\storestate}{f_2}
\newcommand{\processstate}{f_3}

\begin{document}

$\initstate(x,y,0) \to \storestate(x,y)$

$\initstate(x,y,z) \to \processstate(x,z)$

$\initstate(0,0,z) \to z$

$\storestate(x,y) \to \processstate(y,x)$

$\processstate(s(x),y) \to \initstate(x,x,y)$

\end{document}


What I would like to have is that the number in the commands is inserted depending on the order of command definitions (and not specified explicitly in the commands). So if I switch the order in which \initstate and \processstate are defined, their corresponding symbols should also switch their numbers. Likewise, if I add another command definition between the two existing ones, the numbers in all commands should be updated automatically. The number "assigned" to a command that way should not change anymore during usage of the new commands. So in the end, the code should look like this for the two examples (and produce the same output):

\documentclass{article}

\newcounter{funcsyms}
\setcounter{funcsyms}{1}
\numberedcommand{\initstate}{f_}{funcsyms}{}
\numberedcommand{\processstate}{f_}{funcsyms}{}

\begin{document}

$\initstate(x,y,z) \to \processstate(x,z)$

$\initstate(0,0,z) \to z$

$\processstate(s(x),y) \to \initstate(x,x,y)$

\end{document}


Second example:

\documentclass{article}

\newcounter{funcsyms}
\setcounter{funcsyms}{1}
\numberedcommand{\initstate}{f_}{funcsyms}{}
\numberedcommand{\storestate}{f_}{funcsyms}{}
\numberedcommand{\processstate}{f_}{funcsyms}{}

\begin{document}

$\initstate(x,y,0) \to \storestate(x,y)$

$\initstate(x,y,z) \to \processstate(x,z)$

$\initstate(0,0,z) \to z$

$\storestate(x,y) \to \processstate(y,x)$

$\processstate(s(x),y) \to \initstate(x,x,y)$

\end{document}


So I would like to specify 4 things: the name of the new command, its definition before the number, the counter used for the number, and its definition after the number. The number should basically be the position of the corresponding command's definition, i.e., if I define the command \initstate first, its number should be 1. If this command is defined as the second command, its number should be 2 etc.

If I change the order of the command definitions in the last example like this:

\documentclass{article}

\newcounter{funcsyms}
\setcounter{funcsyms}{1}
\numberedcommand{\storestate}{f_}{funcsyms}{}
\numberedcommand{\processstate}{f_}{funcsyms}{}
\numberedcommand{\initstate}{f_}{funcsyms}{}

\begin{document}

$\initstate(x,y,0) \to \storestate(x,y)$

$\initstate(x,y,z) \to \processstate(x,z)$

$\initstate(0,0,z) \to z$

$\storestate(x,y) \to \processstate(y,x)$

$\processstate(s(x),y) \to \initstate(x,x,y)$

\end{document}


The output should be:

If I just reference a counter in such a \numberedcommand and increase it, I will get different numbers in the several places where I use the new command. So that would not solve the problem. If I increase the counter between the definitions, I get the same number for all symbols. How can I define macros that automatically get numbered according to their point of definition (i.e., how to define \numberedcommand in the example above)?

• \processstate should always print the name with the successor of the number appended to \initstate? – egreg Dec 14 '15 at 17:29
• Also, what should \storestate do? Please, expand with some more examples of intended usage. – egreg Dec 14 '15 at 17:35
• @egreg I added an example for the use of \storestate. What the semantics of these commands is, is irrelevant. What matters is that they produce output containing a number and this number should be set according to the position of definition of the command. – cryingshadow Dec 14 '15 at 17:42
• Sorry, but I can't understand what you're after. – egreg Dec 14 '15 at 17:44
• @egreg I added another example for the reordering of command definitions and desired output. What I want is that the numbers in these commands directly correspond to the position of the command definition. – cryingshadow Dec 14 '15 at 17:51

In my point of view, it's easier to contract the call to \numberedcommand from two calls into one and provide the macro names of \initstate and \processstate as arguments.

Anyway, \number\value{foo} will evaluate the counter value and use it as an index. \the\numexpr\value{foo}+1 will use the next integer value without really increasing the counter.

\documentclass{article}

\newcounter{funcsyms}
\setcounter{funcsyms}{0}

\providecommand{\initstate}{f_}
\providecommand{\processstate}{f_}

\newcommand{\numberedcommand}[4]{%
\renewcommand{#1}{#2{\number\value{#3}}}%
\renewcommand{#4}{#2{\the\numexpr\value{#3}+1}}%
\stepcounter{#3}
}

\numberedcommand{\initstate}{f_}{funcsyms}{\processstate}

\begin{document}

$\initstate(x,y,z) \to \processstate(x,z)$

$\initstate(0,0,z) \to z$

$\processstate(s(x),y) \to \initstate(x,x,y)$

\numberedcommand{\initstate}{f_}{funcsyms}{}{\processstate}

$\initstate(x,y,z) \to \processstate(x,z)$

$\initstate(0,0,z) \to z$

$\processstate(s(x),y) \to \initstate(x,x,y)$

\end{document}


Update

Edit with \xdef (although a little bit unsatisfactory)

\documentclass{article}

\newcounter{funcsyms}
\setcounter{funcsyms}{0}

\providecommand{\initstate}{f_}
\providecommand{\processstate}{f_}
\providecommand{\storestate}{f_}

\newcommand{\numberedcommand}[4]{%
\stepcounter{#3}%
\xdef#1{#2{\number\value{#3}}#4}
}

\numberedcommand{\storestate}{f_}{funcsyms}{}
\numberedcommand{\processstate}{f_}{funcsyms}{}
\numberedcommand{\initstate}{f_}{funcsyms}{}

\begin{document}

$\initstate(x,y,0) \to \storestate(x,y)$

$\initstate(x,y,z) \to \processstate(x,z)$

$\initstate(0,0,z) \to z$

$\storestate(x,y) \to \processstate(y,x)$

$\processstate(s(x),y) \to \initstate(x,x,y)$

\end{document}

• How would that work with more than two commands? I would like to have a generic method to define arbitrarily many commands. With your solution, I would have to change everything manually again if I insert a new command between the existing two, don't I? – cryingshadow Dec 14 '15 at 17:44
• @cryingshadow: Your question isn't really clear, I think. I can change, but I have other work to do – user31729 Dec 14 '15 at 17:46
• I tried to clarify the question by more examples. The essence is that if I have a command \foo, then I want the number used in the output of \foo to correspond to the position of the definition of \foo. So when \foo` is the third command that I defined, the number should be 3. If it is the second command, it should be 2 etc. – cryingshadow Dec 14 '15 at 17:54
• @cryingshadow: I'll see what I can do – user31729 Dec 14 '15 at 18:50
• @cryingshadow: I've updated my answer (see at the bottom, please) – user31729 Dec 14 '15 at 20:55