21

I've got a string like this: aXYbXYc, and need to replace the XY substring with a right arrow: a $\to$ b $\to$ c.

Is there any function (also provided by external packages) to help me do that?

  • Like sed? E.g., sed 's/\([a-z]\)XY/\1 $\\to$ /g' myfile.tex... – jon Mar 30 '17 at 1:51
23
\documentclass{article}
\usepackage{xstring}
\def\ReplaceStr#1{%
  \IfSubStr{#1}{XY}{%
    \StrSubstitute{#1}{XY}{$\to$}}{#1}}

\begin{document}

\ReplaceStr{aXYbXYc}

\ReplaceStr{abc}

\end{document}
  • Please don't use minimal for examples. – cfr Mar 29 '17 at 22:43
  • What is the point in using \IfSubStr? – Solomon Ucko Jan 5 at 2:36
  • I cannot remember, so I must say it is without any sense ... – user2478 Jan 5 at 15:11
0
\documentclass{article}
\usepackage{listofitems}
\newcommand\substr[3]{%
  \setsepchar{#2}%
  \readlist\parsedinput{#1}%
  \foreachitem\x\in\parsedinput{%
    \ifnum\xcnt=1\else#3\fi\x%
  }%
}
\begin{document}
\substr{aXYbXYc}{XY}{$\to$}
\end{document}

enter image description here

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.