Regular expression in texstudio

regular expressionstexstudio

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?

Best Answer

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

Related Question