3

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

Hello\textbackslash{}Bye

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\\ }}

\TRANSFORM{Hello\textbackslash{}Bye}
5

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

enter image description here

\documentclass{article}

\newcommand{\TRANSFORM}[1]{{\let\textbackslash\\#1}}
\setlength{\parindent}{0pt}% Just for this example

\begin{document}

Hello\textbackslash{}Bye

\TRANSFORM{Hello\textbackslash{}Bye}

\end{document}
2

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 {}.

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.