1

Much like the author of this question, I would like to automatically number some constants in my document. A relatively easy way to do this is by creating a new counter, assigning a label to each constant on its first use, and then referencing to it later in the document (see these answers).

However, I would like to be able to use my constants with the same command throughout the document, and number them based on first usage, so that the following examples would work:

Here are some constants $\C{first}, \C{second}$.

Later, we reference $\C{first}$.
Actually, we forgot that $\C{important}$ should come first!
Here are some constants $\C{first}, \C{second}$.

Later, we reference $\C{first}$.

Respectively resulting in:

Here are some constants $c_1, c_2$.

Later, we reference $c_1$.

Actually, we forgot that $c_1$ should come first! Here are some constants $c_2, c_3$.

Later, we reference $c_2$.

If it helps, you can assume I will be using the memoir class.

2
  • I've bad news for you: \C is not really a good choice for your command name, because \usepackage[unicode]{hyperref} would break the document. – egreg Sep 24 '20 at 8:02
  • @egreg Not to worry. I used \C in the question for conciseness, but in my document I used \const, which so far didn't result in any clashes. – A.P. Sep 24 '20 at 8:18
1

enter image description here

or with the first line uncommented

enter image description here

\documentclass{memoir}

\newcounter{Ccnt}
\makeatletter
\newcommand\C[1]{%
\@ifundefined{C-#1}%
  {\stepcounter{Ccnt}\expandafter\xdef\csname C-#1\endcsname{\arabic{Ccnt}}}%
  {}%
c_{\csname C-#1\endcsname}}
\makeatother
\begin{document}

%Actually, we forgot that $\C{important}$ should come first!
Here are some constants $\C{first}, \C{second}$.

Later, we reference $\C{first}$.


\end{document}
1
  • Thank you, this is exactly what I was looking for. – A.P. Sep 24 '20 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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