# Remove math from command parameter to get only the letters

I use a little command, which prints new defined terms in bold, adds a label and adds them to the index.

\newcommand*{\defined}[1]{{\bf #1}\label{#1}\index{#1}}

Now if I do \defined{$G$-invariant} the label isn't working and the index entry is ordered in the wrong place. I think that's got to do with the math dollar sign. Is there a way to strip this and get for label and index just the G?

-
Welcome to TeX.sx!. Don't use commands like \bf It's obsolete: See: ctan.org/tex-archive/info/l2tabu or this answer: tex.stackexchange.com/questions/29448/… –  Marco Daniel Feb 17 '13 at 13:14
Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Marco Daniel Feb 17 '13 at 13:18
Next comment: The issue with \label can't work. There is no reference point. –  Marco Daniel Feb 17 '13 at 13:21
@MarcoDaniel Why? \label writes not only a number, but also the page number and it's common to use a \label for referring to the page with \pageref –  egreg Feb 17 '13 at 14:07
@egreg: I would work with \href. If you use nameref it fails –  Marco Daniel Feb 17 '13 at 14:17

Here's how I'd fix it: add a second, optional parameter as the sort key and label:

\documentclass{article}
\usepackage{makeidx}
\usepackage{xparse}
\NewDocumentCommand{\defined}{mg}{%
{\bfseries #1}%
\IfNoValueTF{#2}
{\index{#1}\label{#1}}
{\index{#2@#1}\label{#2}}
}
\makeindex
\begin{document}
\defined{$G$-invariant}    % Probably does it wrong

\defined{$G$-invariant}{G-invariant}
\printindex
\end{document}


As an alternative, in accordance with @egreg's suggestion, using the 'Traditional' way of doing optional arguments:

\documentclass{article}
\usepackage{makeidx}
\usepackage{xparse}
\NewDocumentCommand{\defined}{om}{%
{\bfseries #2}%
\IfNoValueTF{#1}
{\index{#2}\label{#2}}
{\index{#1@#2}\label{#1@#2}}
}
\makeindex
\begin{document}
\defined{$G$-invariant}

\defined[G-invariant]{$G$-invariant}
\printindex
\end{document}

-
Why the g argument instead of a more usual om syntax? A mg syntax is quite confusing, in my opinion. The \label has its use, for \pageref. –  egreg Feb 17 '13 at 14:05
@egreg I sort of robbed it from this answer. I'm still only beginning to try out \NewDocumentCommand; and I do really like the idea of having the optional things last. –  Bianca Lobo Feb 17 '13 at 14:08
If it's optional, then use m, not g. –  egreg Feb 17 '13 at 14:09
@egreg Oh, OK, I'll have to go back to the manual. Is my answer actually wrong? (That is, even worse than confusing?) –  Bianca Lobo Feb 17 '13 at 14:12
No, it's not wrong; but the vast majority of LaTeX commands have the optional argument first and in brackets [...], so users generally expect this form rather than an optional argument in braces after the mandatory one. Having a consistent interface helps. –  egreg Feb 17 '13 at 14:15

I'd definitely go with Bianca Lobo's solution (the alternative one, really). But, just for fun, here's another way.

It's possible to strip $ symbols, but this is not the only problem: if you strip $ from \defined{$\Gamma$-function} you'll get into troubles. Moreover, simply stripping the $ would leave incorrect entries in the index in any case. However one can use the @ feature of \index: with \index{something@something else}  MakeIndex will use something for the sorting, while writing "something else" as the index entry. Here's a complicated way to do what you want: \documentclass{article} \usepackage{xparse,l3regex} \usepackage{imakeidx} \makeindex \ExplSyntaxOn \NewDocumentCommand{\defined}{om} { \textbf{#2} \IfNoValueTF{#1} % No optional argument: do the complicated test { \heinz_index:n {#2} } % Optional argument: use the simplest way { \label{#1}\index{#1@#2} } } \tl_new:N \l__heinz_index_tl \cs_new_protected:Npn \heinz_index:n #1 { % test if a$ is present in the argument
\tl_if_in:nnTF { #1 } { $} { \__heinz_strip:n { #1 } } % a$ is scanned
{ \index{#1} }              % no $, simple case } \cs_new_protected:Npn \__heinz_strip:n #1 { % stringify the argument \tl_set:Nx \l__heinz_index_tl { \tl_to_str:n { #1 } } % remove$, backslash, spaces
\regex_replace_all:nnN { ( \$| \\ | \s ) } { } \l__heinz_index_tl % produce the \index command and the label \__heinz_do_double_index:nV { #1 } \l__heinz_index_tl } \cs_new_protected:Npn \__heinz_do_double_index:nn #1 #2 { \exp_args:NV \label \l__heinz_index_tl \index{#2@#1} } \cs_generate_variant:Nn \__heinz_do_double_index:nn { nV } \ExplSyntaxOff \begin{document} \defined{face} \defined{$G$-invariant} \defined{group} \defined{$\Gamma$-function} \defined[C-transform]{$\mathcal{C}$-transform} \pageref{G-invariant} \clearpage \printindex \end{document}  However, in some cases you'll have to express it in the form \defined[<sorting key>]{<argument>}  as the stripping performed in the case of $\mathcal{C}$-transform will do no good. One might add to the list of tokens to be stripped off, but this is error prone and it doesn't seem really a sensible thing to do. Note also that you have to use the "stripped" version in \pageref, so do \pageref{G-invariant} and not \pageref{$G$-invariant}. In any case a label such as $\Gamma$-function would be illegal. - +1 for fun with LaTeX3 ;-) – Marco Daniel Feb 17 '13 at 15:08 Using the etex \scantokens you can locally set $ to be ignored. This removes it from the label key and also from the sort key used by makeindex. The original math string is used after the @ field so it is typeset in the index.

\documentclass{article}
\usepackage{makeidx}

\newcommand*{\defined}[1]{%
{\catcode\$=9 \scantokens{\gdef\tmp{#1}}}% \textbf{#1}\label{\tmp}\index{\tmp@#1}} \makeindex \begin{document} \defined{$G\$-invariant}

\defined{foo-invariant}

\printindex
\end{document}
`
-