Here's a problem I've encountered when I tried make a script in TeXstudio with several replace queries. Suppose I have some text:

Here goes some text, more text and some more text.

And when I apply this macro

editor.replace(/\s(text)([\.,\s])/g, '\mbox{\1}\2')

to it, the resulting text is messed up:

Here goes somembox{} morembox{}and some morembox{}

(in a text field it looks like this) Here goes somembox{} morembox{}and some morembox{}

In the meantime everything works fine when the same regexp is called from GUI Replace menu:

Here goes some \mbox{text}, more \mbox{text} and some more \mbox{text}.

The text in UTF-8 encoding. Is that behaviour a bug, or my script lacks something important?

  • maybe you have to mask the \ in the replace pattern as \\ (which would be very uncommon). Or it doesn't use \1 \2 and instead $1 $2 etc. check the manual for the replacement command – musicman Oct 13 '15 at 7:15
  • @musicman The problem is - numbered matches aren't even mentioned in manual:texstudio.sourceforge.net/manual/current/… . And I've tried $1 (since these are supposed to be js-macros), but it puts '$1' literally both in macro and UI, but \1 does the substitution in UI command. – The_Keeper Oct 13 '15 at 8:39
  • @musicman hm, changing \ to \\ seems to work, thanks – The_Keeper Oct 13 '15 at 8:48

The current TeXstudio manual doesn't seem to specify the usage of numbered matches in macros. But thanks to @musicman's suggestion (changing single slash \ to double slash \\) I was able to make the script work:

editor.replace(/\s(text)([\.,\s])/g, ' \\mbox{\\1}\\2')

The result is correct now:

Here goes some \mbox{text}, more \mbox{text} and some more \mbox{text}.

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.