Say I have a variable that contains some non-plain text, for instance \def\var{Caro \textit{et al.}}, and I want to make a substring substitution of Caro for \underline{Caro}. If I try to follow the recipe from Replacing a substring, this will fail:
\documentclass{minimal}
\usepackage{xstring}
\def\ReplaceStr#1{%
\IfSubStr{#1}{Caro}{%
\StrSubstitute{#1}{Caro}{\underline{Caro}}}{#1}}
\begin{document}
\def\var{Caro \textit{et al.}}
\ReplaceStr{\var}
\end{document}
If I remove the formatting from the variable definition, i.e. \def\var{Caro et al.}, then it works:
\documentclass{minimal}
\usepackage{xstring}
\def\ReplaceStr#1{%
\IfSubStr{#1}{Caro}{%
\StrSubstitute{#1}{Caro}{\underline{Caro}}}{#1}}
\begin{document}
\def\var{Caro et al.}
\ReplaceStr{\var}
\end{document}
Unfortunately, in my actual case I have no way to directly modify the formatting that \var comes with since it's generated through a series of commands. How can I either:
- Remove all formatting from
\var(i.e. make it "plain text") before I do the substring replacement - Make the string replacement act correctly on formatted text
For the curious, my actual \var comes from a biblatex call: \def\var{\citename{somebibitem}{author}}.

\varis actually\newcommand{\var}{\citename{caro_2015}{author}}andcaro_2015is a bibitem whereM. A. Carois the outcome of\citename{caro_2015}{author}. I will accept your answer but help on this more complicated scenario would be appreciated. – Miguel Sep 22 '15 at 11:31biblatexseem more effective. – egreg Sep 22 '15 at 11:42sedcommand to generate a temporary bib file from my master bib file with the correct formatting... – Miguel Sep 22 '15 at 13:23