I wish to define a meta-command that, whenever invoked, will define an instance of another command; but this inner command must include a progressive number (somewhat similar to what happens with numbered equations, theorems etc).
However, in my attempts so far, the number appears to be evaluated too late (at the time of processing the inner command, rather than at the time of defining it) because all inner commands end up with the final value of the counter (off by one, no less) instead of the value the counter when each of the inner commands was defined.
Perhaps the best way to explain the problem is with a small commented example, consisting of two files. The first file is meant to be included by several other files. The second is one of the several other files that would include the first.
First file (testdef.tex):
% This file may be included by several other files.
% In this example, we are creating a number of proverb-like "rules"
% that other documents will need to refer to.
% All the rules are defined in this file, each with a RULENAME and a
% text. Each rule is given a number, starting from 1, based on the
% order in which they appear here. By defining a rule, we create a new
% internal macro \ruleRULENAME that expands to the text of the rule,
% prefixed by the rule number. The command \showrule{RULENAME}
% displays the rule in the approved way (say in a box) and defines the
% anchor for the label RULENAME: \ref{RULENAME} gives the rule number
% and \pageref{RULENAME} gives the page where \showrule{RULENAME} was
% first issued. Because \showrule{RULENAME} is the anchor for the
% matching label, you cannot issue \showrule{RULENAME} more than
% once. If you need to show the rule without (re)defining the label, use
% \showrulenolabel{RULENAME}.
\newcounter{rule}
\newcommand{\defrule}[2]{
\stepcounter{rule}
\expandafter\newcommand\csname rule #1\endcsname{\textbf{Rule \therule:} #2}
}
\newcommand{\showrulenolabel}[1]{
\noindent\framebox{\csname rule #1\endcsname}
}
\newcommand{\showrule}[1]{
\label{#1}
\refstepcounter{rule}
\showrulenolabel{#1}
}
% the actual rules are defined here
\defrule{vegetables}{Eat more vegetables}
\defrule{smile}{Smile}
\defrule{stairs}{Take the stairs}
Second file, which includes the first:
\documentclass{report}
\begin{document}
\input{testdef}
This is one of several documents that will include the rules file.
In here I should be able to display any rule by name, for example\ldots
\showrule{smile} (should be 2)
%The actual text of that rule is ``\rulesmile'' but I am not really supposed
%to be using that. Strangely, if I do, it says undefined control sequence.
The rules I know about are, in order:
\showrulenolabel{vegetables} (should be 1)
\showrulenolabel{smile} (should be 2)
\showrulenolabel{stairs} (should be 3)
and I should be able to say that
\end{document}
Processing the second file yields a document in which all the rules are numbered 4, rather than the expected 1, 2, 3.
I am reasonably comfortable with Latex but not with plain Tex and I suspect this is why I can't quite get at the correct answer. The most relevant suggestions I found came from this other question and I tried applying some of them but my limited understanding of the raw Tex commands prevented me from achieving the desired results.