4

I am having a problem substituting string using \StrSubstitute. The way I understand the function, as according to the documentation (page 7/8), it should work with three arguments. It should search the text for the first argument, and the replace the second argument within this first argument with the third argument.

\documentclass[12pt]{scrartcl}
\usepackage{xstring}

\begin{document}
\StrSubstitute{abcde}{d}{D}

blablabla
abcde

\input{mwe2}

cdeba

\end{document}

and the input file mwe2.tex is just

abcde

abcde

12345

However, the result of this in the PDF file is just:

abcDe

blablabla

abcde

abcde

abcde

12345

cdeba

i.e. the software places the string abcde at the location of the command \StrSubstitute, and then replaces d with D here, but ignoring the string abcde anywhere else in the document.

Can anyone help? Best, Marie

5
  • StrSubstitute does not work on all upcoming strings, only on the argument abcde, regardless what's inside mwe2.tex
    – user31729
    Jan 12, 2016 at 10:05
  • but if it works only on the argument, and not on further occurences of the argument elsewhere in the text, then what will be the use of this function? Then I will manually have to work through the whole file and place StrSubstitute everywhere where I have an abcde, but then I can just replace abcde directly, if that's what you're saying.
    – Marie. P.
    Jan 12, 2016 at 10:14
  • Yes, of course, that's what Search And Replace for in a editor. Perhaps it works with the l3reg package
    – user31729
    Jan 12, 2016 at 10:16
  • I'm sorry, I could not find anything on the web regarding an l3reg latex package. I'm aware of the use of search and replace in a document, but this does not work on input files that are generated by another software. And if it only works in the text, then my point is: what is the point in programming the \StrSubstitute command at all if \StrSubstitute{abcde}{d}{D} needs to replace abcde everyhwere, and hence does nothing more than any editor's function replace all abcde with abcDe? Then this command would have no right to exist at all, so I'm sure there must be something to it?
    – Marie. P.
    Jan 12, 2016 at 10:43
  • 1
    I meant l3regex, sorry. You're misunderstanding StrSubstitute. If the abcde is hidden in a macro, say, \foo, then StrSubstitute is useful!. \foo can be anything (well, to some extent only, of course)
    – user31729
    Jan 12, 2016 at 10:52

1 Answer 1

3

\StrSubstitute does only act on the first argument content, i.e. it is a one - time - replacement. Following occurences of the source string are disregarded unless they are in the 1st argument. The usability of \StrSubstitute is improved if the source string is contained in a macro, say, \newcommand{\foo}{abcde}.

However, \StrSubstitute{\input{mwe2}{...}{...} would not work, due to expansion issues.

I tried to use the l3regex package (for the very first time) and it's quite easy for simple regular expressions (abcde is simple ;-)

The \SearchAndReplace macro opens the file mwe2.tex and replaces the occurences of abcde with abcDe (however, I don't think it works if there's a line break between abc andde` for example.

\documentclass[12pt]{scrartcl}
\usepackage{xstring}

\usepackage{xparse}
\usepackage{l3regex}

\ExplSyntaxOn
\ior_new:N \l_marie_input_stream 
\NewDocumentCommand{\SearchAndReplace}{mmm}{%
  \ior_open:Nn \l_marie_input_stream {#1}% Open the file
  \ior_map_inline:Nn \l_marie_input_stream {% Read line by line
    \tl_set:Nn \l_tmpa_tl {##1}% Store the line 
    \regex_replace_all:nnN {#2} {#3} \l_tmpa_tl % Replace #2 by #3
    \tl_use:N \l_tmpa_tl % Display the (modified) line content
  }
  \ior_close:N \l_marie_input_stream % close the file
}
\ExplSyntaxOff


\begin{document}

With \verb!\StrSubstitute!

\StrSubstitute{abcde}{d}{D}

blablabla
abcde

Now with \verb!\SearchAndReplace!

\SearchAndReplace{mew2.tex}{abcde}{abcDe}

cdeba

\end{document}

Here is mew2.tex

abcde abcde

abcde

12345

And the output:

enter image description here

5
  • Would you consider \tl_set_from_file:Nnn?
    – egreg
    Jan 12, 2016 at 11:25
  • @egreg: Ah, again some addition to l3file which is hidden in the backyard of the manual ;-). I'll try to change later on
    – user31729
    Jan 12, 2016 at 11:30
  • this is great, but what if I need to do several replacements in the input file, such as replacing abcde with abcDe and xyz with xYZ?
    – Marie. P.
    Jan 12, 2016 at 12:35
  • @Marie.P. You're changing the question!
    – user31729
    Jan 12, 2016 at 12:42
  • yes, I am afraid that your solution might not work anymore. I'm sorry, when providing a minimum working example, I did not think of the specific case that there could be a solution that helps in the MWE, but not in my case with several strings. You provided a solution that works if I only want to substitute one string, but the general case where someone would like to change several strings is just one step away. But does anyone perhaps have any more ideas?
    – Marie. P.
    Jan 12, 2016 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .