2

This question already has an answer here:

I am writing a maths paper where I have a lot of constants. I am indexing them to differenciate them. Basically my text looks like

blabla $c_1$ blablabla $c_2$ blabla $c_3$ blabla $c_1$ blabla $c_4$ blabla $c_5$ ...

I would like to have a commande, let say "\constant" such that each time I call it prints $c_i$ where $i$ is an increasing index. Also I would like to be able to use labels, like in my example above where $c_1$ appears more then one time.

Any idea of a good way to do it?

marked as duplicate by egreg, Andrew Swann, Heiko Oberdiek, Thorsten, Guido Apr 30 '14 at 12:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

This is one idea:

\documentclass{article}
\usepackage{xparse}
\newcounter{const}
\setcounter{const}{0}
\NewDocumentCommand{\constant}{o}
 {% #1 = number (optional)
  \IfValueTF{#1}%    
  {$c_{#1}$}%
  {\refstepcounter{const}%
  $c_{\theconst}$}%
 }
\begin{document}
    blabla \constant{} blablabla \constant{} blabla \constant{} blabla \constant[1] blabla \constant{} blabla \constant{}
\end{document}

enter image description here

If you want a specific constant say c_3, then use \constant[3]

  • Great that works fine. Just a remark. One can write: blabla \constant{} blablabla \constant{}\label{const:2} blabla \constant{} blabla \constant[\ref{const:2}] blabla \constant{} blabla \constant{} – Gilles Bonnet Apr 30 '14 at 11:29

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