[Tex/LaTex] Automatically numbering constants

automationcounterscross-referencing

I would like to somehow use LaTeX to automatically number constants.

Some background

As a mathematician, in my daily work there are a lot of throw-away constants which are not extremely important in the discourse. So I will sometimes write inequalities like

\[ f(x) \leq c_1 g(x) \leq c_2 (g'(x) + h(x)^2 -3) \leq c_3 \int h(x)^2 \]

Since the constants themselves tend not to be important, to conserve alphabets, I call them all 'c' with a subscript. Now, it is a bit of a hassle to

  1. keep track manually which numbers I've used up to a given point in the paper, especially if I am working on a large document

  2. renumber the constants if I modified an argument. Say if I remove the middle inequality from the example above, for aesthetic purposes it'd much better to have \[ f(x) \leq c_1 g(x) \leq c_2 \int h(x)^2 \] instead of c_3 in the last term. But then I'd have to go back through the document to renumber all the constants.

Now sometimes I can solve the problem using the convention that 'c' with no subscripts stands for a constant that can change from line to line. But often that is not feasible (suppose I do need to refer to individual of those constants and verify that they are indeed 'constant' enough for my purpose).

What I'd like

Now I am aware of the newcounter command to define a new counter. I even know how to use it to automatically increment the counter. So I can define a command that prints c_\thecounter and increments the counter so I get a running list of constants. So that solves half the problem.

The other half of the problem that I don't know how to deal with is how to refer to a constant defined previously. Naively using label and ref, of course, doesn't work. (I actually don't quite understand the inner workings of those two commands; so an explanation of why they don't work will also be helpful.)

As an illustration of what I'd like, it'd be great to have a pair of commands \newconstant and \oldconstant which can be used like this

% Create some new constants
\[ f(x) \leq \newconstant g(X) \leq \newconstant h(x) \]
% Create a new constant with a label
\[ m \geq \newconstant[cnst:m] m' \]
% Refer to an already defined constant
The constant $\oldconstant{cnst:m}$ defined above is actually 1. 

Which will display something like

f(x) <= c_1 g(x) <= c_2 h(x)
m >= c_3 m'
The constant c_3 defined above is actually 1

What I know how to do is to define, say

\newcounter{cnstcnt}
\newcommand{\newconstant}{\ensuremath{c_\thecnstcnt}\addtocounter{cnstcnt}{1}}

and this will print the running list.

Is there already a package that does this? If not is there a way to get the behaviour I described? (Especially the ability to refer back to a previous constant.)

Best Answer

Using \refstepcounter instead of \addtocounter{...}{1} will make \ref work:

\newcounter{cnstcnt}
\newcommand{\newconstant}{%
\refstepcounter{cnstcnt}%
\ensuremath{c_\thecnstcnt}}
\newcommand{\oldconstant}[1]{\ensuremath{c_{\ref{#1}}}}
\begin{document}
\newconstant\label{first}
\oldconstant{first}
\end{document}
Related Question