3

My objective is to replace math delimiters path $...$ by \(...\)

\documentclass[12pt,a4paper]{article}
\begin{document}
    This is a sample text $ \sin\alpha $ and $ \cos\alpha. $
    \begin{enumerate}
        \item $ \lim\limits_{x\to 0} \frac{\sin x}{x} $
        \item $ \tan\alpha=\frac{\sin\alpha}{\cos\alpha} $
    \end{enumerate}
\end{document}

Expected output:

\documentclass[12pt,a4paper]{article}
\begin{document}
    This is a sample text  \( \sin\alpha \)  and  \( \cos\alpha. \) 
    \begin{enumerate}
        \item  \( \lim\limits_{x\to 0} \frac{\sin x}{x} \) 
        \item  \( \tan\alpha=\frac{\sin\alpha}{\cos\alpha} \) 
    \end{enumerate}
\end{document}

How can we do this in TeXstudio TeX editor?

2 Answers 2

6

You can create a macro for that to make the replacement in one click or with a press of the shortcut (Ctrl+Alt+1 in the example).

%SCRIPT
regexParsed = editor.text().replace(/([^\$\\])\$([^\$]+)\$/gm, '$1\\($2\\)').replace(/\r/g, '');
editor.setText(regexParsed)

This approach also takes into account $$ display math and dollar signs in a text \$, so that your project won't break in case there are some of them. enter image description here

4
  • It doesn't work if the two $'s are not on the same line (my solution too) This is a feature missing from TeXStudio regular expressions.
    – gigiair
    Feb 28 at 11:47
  • See this minimal example on pastebin ix.io/3QZW . You can have surprising results. You should use an interactive way, as I do.
    – gigiair
    Feb 28 at 13:38
  • If needed, one can always test their regular expressions in regexr.com. About double $$, if it's not desired, one can always substitute all $$ in sequence for one $ and then the order substitution. Indeed, it is as easy as writing both editor.replace commands in sequence. I've faced some problems which breaking it into pieces made the regular expression a lot more easier.
    – FHZ
    Feb 28 at 18:29
  • @gigiair thank you for the issue report. I edited the code to take into account this situation as well.
    – antshar
    Mar 1 at 10:11
3

Search and replace (Ctrl R) (^|[^$\\])\$([^\$]+)\$ with \1\(\2\)

6
  • 2
    This won't work as expected if the OP has $$ display math, so the document will be corrupted. You need to add an additional check for this situation.
    – antshar
    Feb 28 at 10:22
  • 1
    This shouldn't be the case because $$ is TeX and its use is deprecated in LaTeX. It must first replace $$..$$ with \[..\]
    – gigiair
    Feb 28 at 10:41
  • 1
    Note that the main downside is that it doesn't work if the two $s aren't on the same line. It's possible with Emacs, but in TeXStudio I'm afraid it's not possible. Emacs regular expressions are much more powerful than TeXStudio regular expressions.
    – gigiair
    Feb 28 at 11:06
  • 1
    @gigiair I have already tested with TeXstudio 4.2.2 and as have mentioned, it doesn't works when the pair $,$ is not in the same line. Your answer is helpful to the community and I have learned something new form it.
    – Say OL
    Feb 28 at 11:20
  • 1
    @antshar I fixed the regex for the bug you reported. I think it's not a good idea to make a macro because you have to manually check if the pair of $ is not on the same line and TS regexps are not able to do that. Looks at my MWE upward.
    – gigiair
    Feb 28 at 18:22

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.