8

It's on my task list for a while now, and this post made me ask a follow up question which is, in my mind, a missing part when setting such a list.

As an example, think of the standard notation of an n-dim space; I would use the following code to typeset it: \mathbb{R}^n. Then, in order to make things quicker, I would define a macro \newcommand*{\Rn}{\mathbb{R}^n}. So far so good. Now, I want to have a list of symbols as well. In short, the question is: what is the best practice to generate a list of symbols for complicated symbols?

Let me consider the nomencl package, but I think the question is valid when one is using other alternatives as well. Currently, I have a file where I define all the macros I am using in my document, similar to the one I gave as an example above. Then, in order to generate a list's entry I would add

\nomenclature{$\mathbb{R}^n$}{$n$-dimensonal real space}

where I use my macro \Rn for the first time. Is this the right/best way to go? One big drawback here is that when I would like to change the notation in the future, I will have to do it in two different places. Is there a smarter way to go? Somehow unify the two tasks of generating a list's entry and a macro which will be used in order to typeset the symbol later?

lockstep
  • 250,273
Dror
  • 22,613

1 Answers1

9

As long as you're sure that the first occurrence of \Rn is not in a section title or other moving argument, then you can define \Rn to emit the desired \nomenclature command and redefine itself:

\newcommand*{\Rn}{%
  \nomenclature{$\mathbb{R}^n$}{$n$-dimensional real space}%
  \gdef\Rn{\mathbb{R}^n}\mathbb{R}^n%
}

You can of course automatize this in a general macro:

\newcommand*{\definesymbol}[3]{%
   \def#1{\nomenclature{$#2$}{#3}\gdef#1{#2}#2}%
}

and then say

\definesymbol{\Rn}{\mathbb{R}^n}{$n$-dimensional real space}

and similarly for other symbols.

Caution: this will break miserably if you say \section{Definition of $\Rn$} and this is the first appearance of \Rn.

If you plan using those symbols in section titles, the following modified definition should work:

\makeatletter
\newcommand*{\definesymbol}[3]{\def#1{%
  \ifx\protect\@unexpandable@protect
    #2%
  \else
    \nomenclature{$#2$}{#3}\gdef#1{#2}#2%
  \fi}
}
\makeatother
egreg
  • 1,121,712
  • Regarding caution: Will using optional argument for section like \section[Definition of $\Rn$]{definition of $\mathbb{R}^n$} over come this failure? –  May 14 '12 at 09:24
  • @HarishKumar Yes; definitely. The first appearance of \Rn the next time the document is compiled would be in the table of contents! Putting \mathbb{R}^n in the optional argument and \Rn in the mandatory one might solve the issue. – egreg May 14 '12 at 09:27
  • Trying to make sure I understand, if the first time \Rn appears is in a section's title, then it should be produced by \section[Definition of $\mathbb{R}^n$]{Definition of $\Rn$}? – Dror May 14 '12 at 09:38
  • @egreg: Sorry. I typed reverse. So \section[Definition of $\mathbb{R}^n$]{definition of $\Rn$} (This is what I meant) solves it. I wonder whether there is any other fix for this problem besides optional argument? It will be nice of you if you incorporate all these remedies in to your answer. (already upvoted +1). –  May 14 '12 at 09:59
  • @Dror Yes. If I find time I'll think to a better way. Today is lecture day, starting at 11:30 and ending at 18:30; but the last two hours are the LaTeX course. :-) – egreg May 14 '12 at 12:42
  • @egreg: This looks great. Can you think of a way how to extend it to support operators? For example, consider the following definition \newcommand*{\dotprod}[2]{\left<#1,#2 \right>} - how can it be added to the list "automatically"? – Dror May 14 '12 at 13:58
  • @Dror Why should you want to index the first occurrence of the dot product \dotprod{a}{b} with those values? – egreg May 16 '12 at 14:58
  • @egreg Ideally I'd want to index the first occurrence of \dotprod{foo}{bar}, such that in the nomenclature it will appear as \dotprod{\cdot}{\cdot}. However, I guess this can only be done manually... As far as I understand, your solution is mainly useful for non-operators' definitions. – Dror May 16 '12 at 19:46