I have encountered another expansion issue and have narrowed it down to this simple test case of a macro that does nothing, well at least it tries to do nothing, but is not all that successful.
If I declare this macro as:
\newcommand*{\DoNothingA}[1]{#1}%
This works great, no surprises there.
However, if this macros is defined as follows:
\newcommand*{\DoNothingB}[1]{%
\def\Temp{#1}%
\Temp%
}%
then, this has problems where there is an attempt to use this in an \edef.
Output:
Note that the MWE below yields a compile error for the last step, so you need to hit <RETURN> to skip past it to see the output (or comment out the last \edef, but then this won't show Question 2 below).
The MWE below yields the following:

The last line being especially curious as it produced foo 4, and was the result of:
\edef\NewTemp{\DoNothingA{foo 5}}
\NewTemp
\color{red}% Highlight problem output
\edef\NewTemp{\DoNothingB{foo 6}}
\NewTemp
\color{black}
~-- used \verb|\edef| to store output of macro
Questions:
- What is the explanation of the expansion issue here?
- How does the final output become
foo 4(foo 5would have been easier to accept), if we skip past the compile error? - Since David Carlisle has been quoted as saying that "a few extra
\expandaftercure all known ills" :-), what is the sequence of\expandafters needed in the last\edefto use macro\DoNothinBas defined here. - How can I adjust the
\DoNothingB{}macro to use a temp variable and still work in the three cases in the MWE (preferrably without having to use any\expandaftertrickery.
Code:
\documentclass{article}
\usepackage{xcolor}% To highlight problem output
\newcommand*{\DoNothingA}[1]{#1}% Does everything I want in all cases
\newcommand*{\DoNothingB}[1]{% Works mostly, except if used in a \edef
\def\Temp{#1}%
\Temp%
}%
\begin{document}
% These two work great.
\DoNothingA{foo 1}
\DoNothingB{foo 2}
-- used output of macro directly
% These two also work great.
\def\NewTemp{\DoNothingA{foo 3}}
\NewTemp
\def\NewTemp{\DoNothingB{foo 4}}
\NewTemp
~-- used \verb|\def| to store output of macro
\edef\NewTemp{\DoNothingA{foo 5}}
\NewTemp
\color{red}% Highlight problem output
\edef\NewTemp{\DoNothingB{foo 6}}
%\show\NewTemp% Excellent suggestion by David Carlisle yields: \def foo 4{foo 6}foo 4
\NewTemp
\color{black}
~-- used \verb|\edef| to store output of macro
% Why is the last output "foo 4" (after ignoring error)??
\end{document}