在组内移动参数

在组内移动参数

我有一个类似于下面的 MWE 的设置,其中我有一个需要在文档中调用的宏,并且还会定期成为移动参数(在本例中,插入到索引中,尽管它实际上可以是任何东西)。 这个宏(\arobustcmd在示例中)只是对另一个隐藏在最终用户之外的宏执行某些操作(\someothercmd在此示例中为大写或小写)。 它有时需要以作用域方式进行更改(例如在 内部\begin{@empty})。 在打印命令时,此作用域按预期工作(在环境内部它是新文本,在环境外部它是旧文本),但是,由于\arobustcmd传递给索引并在稍后扩展,因此索引条目始终是旧文本。

我想要两个索引条目,一个用于旧文本,一个用于 内设置的文本\begin{@empty}。有没有办法\arobustcmd在写入索引文件之前强制扩展?

我总是可以重新设计它来声明两个命令(例如,\arobustcmd并在命令内部\anotherrobustcmd重新定义它们,而不是像我在这里所做的那样,但我很好奇是否有办法让我的设置工作。\protected@edef\setthetext

\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}

答案1

因为这可能是不可能的;我已经修改了我的示例,使其看起来像这样,它适用于我的用例(即使它不完全相同):

\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}

相关内容