[Tex/LaTex] Best practice: LaTeX constants for possibly changing repeatedly used symbol

best practices

Say I'm writing a long document and I want to use the letter $n$ to stand for dimension e.g. in $\mathbb R^n$ all over the paper. Late in the writing process I realize that I want $n$ to stand for something else and decide I want to use $m$ instead. I can't just search and replace the letter n; a regex search might be too broad, but e.g. replacing $\mathbb R^n$ might be too narrow as it might be $\mathbb R^{2n+1}$.

To avoid that scenario, is it good practice to use a command e.g. \newcommand{\dimn}{n} and use $\mathbb R^{\dimn}$ everywhere in the document, a la constants in a programming language? Or is this misuse / abuse or otherwise not a good idea? I will be collaborating with others on this document.

Best Answer

It is best to define commands (or control sequences) for these things, since it allows changes to be made on a global scale. Perhaps, to go one step further, you should even define commands in other hierarchical items

\newcommand{\Rn}[1]{\mathbb{R}^{#1}}

since you might be interested in using \mathcal rather than \mathbb later down the line. For more on this, as well as some other consistency suggestions, see Consistent typography. You'll note that @egreg's answer provides exactly this suggestion in order to maintain consistency. A macro-based language like (La)TeX accommodates (and perhaps encourages) this by default.

Another suggestion would be to define some generic command and use optional arguments or starred versions therefore to make the generated output more specific. For this, the interface provided by xparse may be very helpful. Take my above example for instance: Instead of defining \Rn and then (say) \Cn you could define

\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\Rn}{O{R} m}{\mathbb{#1}^{#2}}%

which would allow you to use \Rn{\ndim} or \Rn[C]{\ndim}. That is, only one command, with an optional argument that allows for small modifications to your notation. In essence it boils down to the same thing, so it may be up to personal preference.