[Tex/LaTex] Pass a macro (with arguments) as an argument for another macro

macros

In Pass a command as argument for another command, a notion of invoking TeX macros as methods was described. In my case, I'm attempting to use arguments within the call structure. My example below uses two passes of \StrSubstitute to replace characters that I want to modify:

\usepackage{xstring} % for StrSubstitute

\newcommand*{\changeslash}[1] {%
    \StrSubstitute{#1}{/}{5c}%
}
\newcommand*{\changedollars}[1] {%
    \StrSubstitute{#1}{\$}{24}%
}
\newcommand*{\changeboth}[1] {%
    \changeslash{\changedollars{#1}}%
}

I've experimented with adding \expandafter in different places, but this problem has me stumped.

Best Answer

enter image description here

xstring replacements do not work by expansion, but they need the input string to be expanded, thus adding \expandafter doesn't really help. You need to use the optional argument so that the string with the replacement is stored in a temporary macro, then you can use \expandafter to expand that macro to pass to the second call. In addition as it fully expands its input in a way not entirely consistent with LaTeX \protect mechanism I needed to make \$ e-tex protected.

\documentclass{article}

\usepackage{xstring} % for StrSubstitute

\let\olddollar\$
\protected\def\${\olddollar}


\newcommand*{\changeslash}[1] {%
    \StrSubstitute{#1}{/}{5c}[\temp]%
}
\newcommand*{\changedollars}[1] {%
    \StrSubstitute{#1}{\$}{24}[\temp]%
}
\newcommand*{\changeboth}[1] {%
  \changedollars{#1}%
  \expandafter\changeslash\expandafter{\temp}%
}


\begin{document}


\changeslash{aaa/b\$bb}\temp


\changedollars{aaa/b\$bb}\temp


\changeboth{aaa/b\$bb}\temp

\end{document}