Continuing with my struggle to deal with expansion issues, I am stuck on how to use an \edef properly. The code below with the switches set as:
%\def\ApplyColorToTitle{}
%\def\UseMathrmInTitle{}
\def\UseEdefInsteasOfDef{}
produces:

but as soon as I attempt to apply color formatting of the title, or even use \mathrm{} (surprisingly using \text{} does not exhibit this problem) in the title I get compile errors if I attempt to use the \edef. That is, the following settings work:
\def\ApplyColorToTitle{}
\def\UseMathrmInTitle{}
%\def\UseEdefInsteasOfDef{}
and produce the desired result:

However, I would like to be able to use this code with all the switches enabled:
\def\ApplyColorToTitle{}
\def\UseMathrmInTitle{}
\def\UseEdefInsteasOfDef{}
In this case I thought I needed to use a \noexpand on the formatting macro in the \edef:
\edef\FormattedTitle{\noexpand\FormatTitle{\Title}}
This \noexpand does work for the case of:
\def\ApplyColorToTitle{}
%\def\UseMathrmInTitle{}
\def\UseEdefInsteasOfDef{}
and hence I thought I was on the right track, but still fails with the use \mathrm{}.
Question:
How do I use the \edef instead of a \def and allow for use of \mathrm{} along with the color formatting?
Note:
I have to admit I am not sure why I have the
\edefhere in the first place, but there must have been a case that was not working that got fixed when I switched to using an\edef. I realize that it is not a very good reason, but that is why I would prefer to continue to use the\edef. If the expansion gurus here see no reason to be using an\edefhere and recommend that I just use the\defI will do that until I am able to provide a MWE that actually requires a\edef.I do need to use
\mathrm{}instead of\text{}to ensure that thethis in roman font even if the title is wrapped in another formatting macro..
Code:
%\def\ApplyColorToTitle{}
%\def\UseMathrmInTitle{}
\def\UseEdefInsteasOfDef{}
\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\newcommand{\Title}{}%
\newcommand{\SetTitle}[1]{%
\renewcommand*{\Title}{#1}%
}%
\ifdefined\ApplyColorToTitle
\newcommand{\FormatTitle}[1]{\textbf{\textcolor{blue}{\boldmath#1}}}%
\else
\newcommand{\FormatTitle}[1]{#1}%
\fi
\begin{document}
\ifdefined\UseMathrmInTitle
\SetTitle{$n^{\mathrm{th}}$ Root}%
\else
\SetTitle{$n^{\text{th}}$ Root}%
\fi
\ifdefined\UseEdefInsteasOfDef% Would prefer to use an edef
\edef\FormattedTitle{\FormatTitle{\Title}}%
% Thought that adding this \noexpand would do the trick as
% it seems to alow for \ApplyColorToTitle being defined.
%\edef\FormattedTitle{\noexpand\FormatTitle{\Title}}%
\else
\def\FormattedTitle{\FormatTitle{\Title}}%
\fi
Title is: \FormattedTitle
\end{document}
\edefon tokens like\mathrm: they do assignments which are not performed in\edef. Rather use\protected@edef. (And never use superscripted "th", please.) – egreg Jul 04 '12 at 21:56\protected@edefall the time? – Peter Grill Jul 04 '12 at 22:00\edef(or variants thereof) in this case. But the answer is no:\protected@edefand\edefare different. – egreg Jul 04 '12 at 22:01\textup. – Mico Jul 05 '12 at 01:05\def, but thanks for the suggestion. – Peter Grill Jul 05 '12 at 01:12