I need to create a function that will transform a string such as:


into this:

Hello \\ Bye

I have tried using StrSubstitute (from the xstring package) without success. I have attempted things along these lines:

\newcommand{\TRANSFORM}[1]{\StrSubstitute{#1}{\textbackslash{}}{ \noexpand\\ }}


You could just temporarily redefine the meaning of \textbackslash, letting it to \\:

enter image description here


\setlength{\parindent}{0pt}% Just for this example





You need to use the \noexpandarg mode of xstring, so it won't try expanding its arguments:


%\noexpandarg % set \noexpandarg globally

  \saveexpandmode\noexpandarg % set \noexpandarg locally
  \restoreexpandmode % restore the previous mode




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

Why \renewcommand and not \let? Because this redefinition also swallows {}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.