The problem you are facing with is not related to pgfkeys
, but comes from the combination of \edef
and \protect
.
Let me consider the following MWE.
\documentclass{article}
\usepackage{amstext}
\def\foo{
\displaystyle \text{D},
\textstyle \text{T},
\scriptstyle \text{S},
\scriptscriptstyle \text{SS},
}
\edef\bar{\foo}
\begin{document}
$\foo \quad \bar$
\end{document}
The result is like this and you will find that \text
s do not respect the preceding \...style
s in \bar
.

This is because of the fact that the \text
command is defined in amstext.sty
as
\DeclareRobustCommand{\text}{%
\ifmmode\expandafter\text@\else\expandafter\mbox\fi}
This means \text
is a self-\protect
ed command and thus safe in most contexts, but \protect
won't survive inside an \edef
because \edef
is a TeX primitive.
Robust commands inside an \edef
sometime work. For instance,
\documentclass{article}
\usepackage{amstext}
\makeatletter
\def\foo{
\displaystyle \text{D},
\textstyle \text{T},
\scriptstyle \text{S},
\scriptscriptstyle \text{SS},
}
\let\@@protect\protect
\let\protect\@unexpandable@protect
\edef\bar{\foo}
\let\protect\@@protect
\makeatother
\begin{document}
$\foo \quad \bar$
\end{document}
will give the expected result (and this is what \protected@edef
and \protectded@xdef
do). However, this depends on the situation.

You can also benefit from the e-TeX's \protected
(possibly using the etoolbox
package):
\documentclass{article}
\usepackage{amstext}
\usepackage{etoolbox}
\makeatletter
\robustify{\text} % this utilizes the e-TeX's low-level feature instead of LaTeX's high-level protection.
\def\foo{
\displaystyle \text{D},
\textstyle \text{T},
\scriptstyle \text{S},
\scriptscriptstyle \text{SS},
}
\edef\bar{\foo}
\makeatother
\begin{document}
$\foo \quad \bar$
\end{document}
That being said, my recommendation is to avoid using the .estore in
handler unless absolutely needed.