0

I am using TeXStudio as my editor.

When creating macros for myself, it is sometimes convenient to know whether the cursor is inside delimiters (e.g., $...$, \[...\]).

Is there a built-in function in TeXStudio that does this job? I assume the editor knows this because codes inside delimiters are highlighted differently.

1 Answer 1

1

Assume that there is no line break between the two delimiters $. The following script will check if the cursor is in the math mode and select the math code.

%SCRIPT
var linenum = cursor.lineNumber();
var colnum = cursor.columnNumber();
var strLine = editor.text(linenum); // get current line
var left = strLine.indexOf("$");
while (left != -1) {
  var n = strLine.substring(left + 1).indexOf("$");
  if (n !== -1){
    var right = left + n + 1;
    if (left < colnum && colnum <= right){
      cursor.select(linenum, left, linenum, right + 1);
      break;
    }
    var n1 = strLine.substring(right + 1).indexOf("$");
    left = (n1 === -1) ? -1 : right + n1 + 1;
  } else {
    break;
  }
}

You must log in to answer this question.

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