The LaTeX editor I use, TeXStudio, allows for custom script-macros. I have little experience with JAVASCRIPT, but I do have one script I managed to create after reading the TeXstudio : user manual providing me with a list of JAVASCRIPT commands.

My example (primitive) script using only the commands in the user manual

txt = cursor.selectedText()
ln = cursor.lineNumber()
cn = cursor.columnNumber()

The above script presupposes the cursor selected some string. Then it surrounds it with "{" "}", and leaves the cursor in the desired location.

I am interested in a similar (inverse) script where the cursor is between $ $, and is able to select everything in between $ and $.

Much like how Ctrl+W selects an entire word, I want to create a script (which will be mapped to some shortcut) that selects an entire math environment between $ and $.

1 Answer 1


This script assumes (i) your cursor is lying inside the math environment $..$ already, and that (ii) the whole $...$ content lies within a single line (this condition can be relaxed, but would make for a more fragile code, I think).

NOTE: It fails (doesn't check for) the following situation:

There are $x$ apples| and $y$ oranges.

where your cursor sits on |, the result is apples and being selected. This could be improved upon, but would take a little bit more work. Last I checked, there is no function to 'check if the cursor is within a math environment'. So one probably needs to count the number of $ from the beginning of the line, check if the count is even/odd etc.etc. (But even this is not foolproof..)


// grab the line the cursor is on
var ln = cursor.lineNumber();
var tl = editor.text(ln);

// get text before and after cursor
var beforeText = tl.substr(0, cursor.columnNumber());
var afterText = tl.substr(cursor.columnNumber(), tl.length);

// search for last $ before cursor, and first $ after cursor
var op = beforeText.lastIndexOf('\$');
var cl = afterText.indexOf('\$');
if (op>=0 && cl>=0){
    // match found, select everything between the $$
    cursor.selectColumns(op+1, cl+cursor.columnNumber())

In the gif, I am activating the macro with the default Shift+F1 when I'm letting the cursor sit for a while.

enter image description here

Test case:

% Test 1: Standard test
There are $x$ apples and $y$ oranges.
A longer expression might be $ \alpha + \beta = \gamma $

% Test 2: $$ across lines (fails) -- condition can be relaxed
x + y = \sqrt{2}

  • I noticed (and expected) that you used commands not listed in the usermanual. Do you have a recommended beginnner / intermediate / advanced reference for learning what commands are predefined? I am quite amazed by the gif you posted. Commented Sep 22, 2018 at 22:03
  • 1
    @AlbertoTakase Only cursor.selectColumns() is not listed in the user manual. The rest are either ecmascript standard functions (string manipulation) or already present in the user manual. As for reference, I don't have one, sorry. (read the source code of Texstudio?)
    – Troy
    Commented Sep 22, 2018 at 22:06

You must log in to answer this question.

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