# indexing woes with macro arguments

I have asked strip last character from parameter if it is 's' and I was given an answer. However, it turns out my case is little bit more complicated. Some of the monsters have hard to type names so I used a macros, e.g., \WviiSPOT, instead of directly typing the monster names. It turns out that combination of indexing and paragraphs fails to compile. Is there a way to make this work?

\documentclass{report}
\usepackage[xindy]{imakeidx}

\usepackage{xstring}
\newcommand{\indexMonster}[1]{\index{#1}}
\newcommand{\monster}[1]{%
\IfEndWith{#1}{s}{%
\StrGobbleRight{#1}{1}[\result]%
#1\indexMonster{\result}%
}{%
#1\indexMonster{#1}}%
}

\newcommand{\WviiSPOT}{\mbox{\monster{*S~P~O~T*}}}

\makeindex

\begin{document}
This works:
\monster{Red Pirannhas} \monster{Bambiphoots} \monster{Stag Weevils} \WviiSPOT{}

This does not:
\paragraph{But \WviiSPOT{} is insane!}
\end{document}

• Use \DeclareRobustCommand instead of \newcommand. – Manuel Jun 21 '16 at 7:46
• @Manuel: For which of the commands? – wilx Jun 21 '16 at 7:47
• All of them could be, but the “important” are \monster and \indexMonster, since the other one is just a shortcut. – Manuel Jun 21 '16 at 7:48
• @Manuel: Thank you. That seems to solve the issue. – wilx Jun 21 '16 at 7:51
• Glad this worked out. By the way, I think you added the wrong question in your url. – Runar Jun 21 '16 at 7:57

## 1 Answer

This is my answer there, including the new command. You just have to \protect it:

\documentclass{article}
\usepackage{stringstrings}
\usepackage{makeidx}\makeindex

\begin{document}

\newcommand{\indexMonster}[1]{%
\testmatchingchar{#1}{$}{s} \ifmatchingchar \substring[q]{#1}{1}{$-1}\index{\thestring}
\else
\index{#1}
\fi}
\newcommand{\monster}[1]{#1\indexMonster{#1}}

\monster{zombies}

\newcommand{\WviiSPOT}{\mbox{\monster{*S~P~O~T*}}}

\WviiSPOT

\pagebreak

\paragraph{But \protect\WviiSPOT is insane!}

\monster{zombie}

\printindex

\end{document}


The strinstrings package by Steven B. Segletes.