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

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;
  }
}

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.