In the TexStudio user mannual I have found the answer of the question. I am sharing it with the hope that it will help others.
- Add a New Macro
- Give some name to the macro
- In
Trigger
write (?highlighted-as:numbers,math-delimiter,math-keyword)_
Type
is Normal
Latex Content
is _{%|}
Although replacing ^
with ^{%|}
is little bit more tricky, because ^
has special meanings in a regular expression. For that change the third bullet above by
- In
Trigger
write (?highlighted-as:numbers,math-delimiter,math-keyword)\^
And change fifth bullet above by
The only drawback of this method is that the macro will also activated by pressing the trigger if the cursor is touching any math code even if it is outside the math code. For example: A$ some math code $B
here you can trigger the macro even if the cursor is in between A
and $
or in between B
and $
because $
is a math code and the cursor is touching $
. However, if the cursor is not touching any math code the macro will not be executed by trigger.