Moving arguments inside group

I have a setup similar to the MWE below in which I have a macro that needs to be called in the document, and also regularly becomes a moving argument (in this case, inserted into the index, though it could really be anything). This macro (\arobustcmd in the example) simply performs some operation on another macro that's hidden away from the end user (making \someothercmd capital or lowercase in this example). It occasionally needs to be changed in a scoped fashion (eg. inside the \begin{@empty}). This scoping works as expected when printing the command (inside the environment it's the new text, outside the environment it's the old text), however, since \arobustcmd gets passed to the index and expanded later, the index entry is always the old text.

I'd like to have two index entries, one for the old text, and one for the text set inside the \begin{@empty}. Is there a way to force the \arobustcmd to expand before being written to the index file?

I can always rework it to declare two commands (eg. \arobustcmd and \anotherrobustcmd and redefine both of them with \protected@edef inside the \setthetext command instead of the way I've done it here, but I'm curious if there's a way to make my setup work.

\documentclass{article}

\usepackage[splitindex]{imakeidx}
\makeindex[name=anidx]

\def\someothercmd{Some text}
\newcommand*{\arobustcmdStar}{\expandafter\MakeUppercase\someothercmd}
\newcommand*{\arobustcmdNoStar}{\expandafter\MakeLowercase\someothercmd}
\makeatletter
\DeclareRobustCommand*{\arobustcmd}{\@ifstar\arobustcmdStar\arobustcmdNoStar}
\makeatother
\newcommand*{\setthetext}[1]{\def\someothercmd{#1}}

\begin{document}

\begin{@empty}
\setthetext{new text}
\makeatletter\imki@wrindexentry{anidx}{\arobustcmd}{3}\makeatother
Some output: \arobustcmd
\end{@empty}

\makeatletter\imki@wrindexentry{anidx}{\arobustcmd}{3}\makeatother
Some output: \arobustcmd

\printindex[anidx]

\end{document}

• Usage of \DeclareRobustCommand is already a problem; the *-variant is another; no expansion can take you past it. – egreg Sep 12 '13 at 17:28
• The star variant doesn't really matter, I can remove that. I kind of figured that would be the answer (since that's the whole point of \protected and \DeclareRobustCommand), but I was hoping their would be some other non-obvious solution that I was missing. Thanks anyways though. – Sam Whited Sep 12 '13 at 18:01
• A \protected macro is insensitive to \edef, but it isn't to \expandafter. I didn't really understand your motivation, though. Be careful in using internal commands such as \imki@wrindexentry in a document. – egreg Sep 12 '13 at 18:10
• @egreg It's not actually in a document; it's in a DTX that defines a package and a class. I just made it a document for the purpose of the MWE. – Sam Whited Sep 12 '13 at 18:27
• If someone wants to write a nice explanation of why my original idea doesn't work, I'll happily accept that as the answer. Otherwise I've posted my reworked example which does essentially the same thing (but without the star command). – Sam Whited Sep 12 '13 at 20:00

Since this is probably impossible; I've modified my example to look something like this which works for my use case (even if it isn't quite the same):

\documentclass{article}

\usepackage[splitindex]{imakeidx}
\makeindex[name=anidx]

\def\arobustcmd{Some text}
\def\anotherrobustcmd{some text}
\newcommand*{\setthetext}[1]{%
\protected@edef\arobustcmd{\expandafter\MakeUppercase#1}%
\protected@edef\anotherrobustcmd{\expandafter\MakeLowercase#2}%
}

\begin{document}

\begin{@empty}
\setthetext{new text}
\makeatletter\imki@wrindexentry{anidx}{\arobustcmd}{3}\makeatother
Some output: \arobustcmd
\end{@empty}

\makeatletter\imki@wrindexentry{anidx}{\arobustcmd}{3}\makeatother
Some output: \arobustcmd

\printindex[anidx]

\end{document}

• Why the starting \protected@edef\arobustcmd, if the replacement text is just some text? – egreg Sep 12 '13 at 20:34
• Because of a copy-pasta error (fixed) – Sam Whited Sep 12 '13 at 20:39