You need to use the \noexpandarg
mode of xstring
, so it won't try expanding its arguments:
\documentclass{article}
\usepackage{xstring}
%\noexpandarg % set \noexpandarg globally
\newcommand{\TRANSFORM}[1]{%
\saveexpandmode\noexpandarg % set \noexpandarg locally
\StrSubstitute{#1}{\textbackslash{}}{\\}%
\restoreexpandmode % restore the previous mode
}
\begin{document}
\TRANSFORM{Hello\textbackslash{}Bye}
\end{document}
Depending on your usage of xstring
, you might also set \noexpandarg
globally (by commenting/uncommenting the relevant lines).
Of course, in this case, redefining a macro is better:
\newcommand\TRANSFORM[1]{{% open a group
\renewcommand{\textbackslash}[1]{\\}%
#1%
}}
Why \renewcommand
and not \let
? Because this redefinition also swallows {}
.