Automatically numbering constants

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}

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.)

• I am not completely thrilled about accepting my own answer, but I feel that the package I linked to is a better solution then hacking my own commands. – Willie Wong Aug 2 '10 at 20:10

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

\newcounter{cnstcnt}
\newcommand{\newconstant}{%
\refstepcounter{cnstcnt}%
\ensuremath{c_\thecnstcnt}}
\newcommand{\oldconstant}{\ensuremath{c_{\ref{#1}}}}
\begin{document}
\newconstant\label{first}
\oldconstant{first}
\end{document}
• Ah! I didn't know about that command. Thanks! – Willie Wong Jul 31 '10 at 14:55
• How can you distinguish the labels from other labels, lets say, labeling the current section? – Ktree Mar 22 at 9:03

Since asking this question I've also discovered the constants package on CTAN. It does precisely what I want it for, and also saves me brain-cell from having to define my own commands and counters.

In particular one can declare new families of constants by

\newconstantfamily{name}{symbol=<symbol>}

(one can also tune how the subscripts are displayed etc.) And use this new family by

\C[name]
\Cl[name]{label}
\Cr{label}

(the first form gives the next constant in family name, the second also labels it with label, the third refers to whichever constant label points to. The default family has its symbol set to C. It also plays well with hyperref in the sense that no links are generated for the constants (the way I think it should be).

One note: the command by default is not aware of the mode you are in. So if you want to use mathmode symbols, it may be quicker to define a wrapper

\newconstantfamily{errorterms}{symbol=\mathfrak{e}}
\newcommand{\errorterm}{\ensuremath{\C[errorterms]}}
\newcommand{\errorterml}{\ensuremath{\C[errorterms]{#1}}}
...

to make typing in textmode more pleasant.

• (Re: no links are generated for the constants) ah, that's the thing I clearly missed – Grigory M Aug 2 '10 at 20:33
• Well, in your answer I think if I just use \ref* instead of \ref it will be okay with hyperref. Though with showkeys it will still generate a bunch of new keys in the margin, which one might argue is actually preferable. – Willie Wong Aug 2 '10 at 20:44
• Be aware that constant package has bug affecting biblatex -- tex.stackexchange.com/q/419686/8297 – ablmf Aug 15 '18 at 14:05
• @ablmf: thanks for pointing that out! I'm changing the accepted answer as a result. – Willie Wong Aug 15 '18 at 14:10