# \index command doesn't work in a simple newcommand

When I write the following code, it works.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage{makeidx}
\makeindex
\newcommand{\p}{\raisebox{2pt}{$\mathfrak{p}$}}

\begin{document}

$\p(E)$\index{$\p(E)$}

\printindex
\end{document}


But what I need is this:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage{makeidx}
\makeindex
\newcommand{\p}{\raisebox{2pt}{$\mathfrak{p}$}}

\newcommand{\idxx}[1]{$#1$\index{$#1$}}

\begin{document}

\idxx{\p(E)}

\printindex
\end{document}


But it fails. Why? How can I fix it?

-
\DeclareRobustCommand{\p}{\raisebox{2pt}{$\mathfrak{p}$}} –  Gonzalo Medina Mar 1 at 19:09
for situations like this, i'd recommend using a sort field for the index; i'm sure that "\raisebox" will not sort into any useful order by itself. –  barbara beeton Mar 1 at 19:23

Either you \protect the \p command inside \index, or better yet, use \DeclareRobustCommand in the definition:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage{makeidx}
\makeindex
\DeclareRobustCommand{\p}{\raisebox{2pt}{$\mathfrak{p}$}}

\newcommand{\idxx}[1]{$#1$\index{$#1$}}

\begin{document}

\idxx{\p(E)}

\printindex
\end{document}

-
\newcommand\p{\protect\raisebox{2pt}{$\mathfrak{p}$}}


However, in general it is not a good idea to define macros with a name of a single character. There are some internal ones predefined.

-