Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

2 Answers

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}
share|improve this answer
    
Ah! I didn't know about that command. Thanks! –  Willie Wong Jul 31 '10 at 14:55
add comment
up vote 11 down vote accepted

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}[1]{\ensuremath{\C[errorterms]{#1}}}
...

to make typing in textmode more pleasant.

share|improve this answer
    
(Re: no links are generated for the constants) ah, that's the thing I clearly missed –  Grigory M Aug 2 '10 at 20:33
2  
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.