TransWikia.com

How to number constants based on first use?

TeX - LaTeX Asked on December 7, 2020

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.

One Answer

enter image description here

or with the first line uncommented

enter image description here

documentclass{memoir}

newcounter{Ccnt}
makeatletter
newcommandC[1]{%
@ifundefined{C-#1}%
  {stepcounter{Ccnt}expandafterxdefcsname C-#1endcsname{arabic{Ccnt}}}%
  {}%
c_{csname C-#1endcsname}}
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}

Correct answer by David Carlisle on December 7, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP