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.

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.

share|improve this question
7  
This is a perfectly valid use case for using macros. –  Aditya Jan 26 '12 at 21:10
2  
Related: tex.stackexchange.com/questions/21394/… –  N.N. Jan 26 '12 at 21:25

1 Answer 1

up vote 11 down vote accepted

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.

share|improve this answer
    
Sound advice; thanks. –  Philip Jan 27 '12 at 4:40

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.