Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

My handy scripts / macros

Anonymous
2012-06-09
2012-10-17

  • Anonymous
    2012-06-09

    As I mentioned before, i think the option to use scripts / macros in texstudio
    is great.
    I wrote several macros, and the most useful 3 i would like to share:

    1. Makes a suggestion for a reference based on found keywords:
      I suggest \ref as trigger. It should be easy to modify the script for his
      language.

      %SCRIPT
      // autoref: searches in the near of the cursor for a given
      // keyword makes a proper suggestion for and a ref based
      // on the content
      // suggest trigger: \ref
      // version 0.5

      var search_range_columns = 15;
      var ref = new Array();
      var tag = new Array();

      // array with keywords. The script searches for this keywords.
      // regular expression;
      // list is extentable
      ref[0] = /figure|graph|bild|grafik/i;
      ref[1] = /chapter|kapitel/i;
      ref[2] = /section|abschnitt/i;
      ref[3] = /chart|table|tabelle/i;
      ref[4] = /equation|gleichung/i;
      //ref[5] = /paragraph/i;

      // related tags, should fit to your used tags
      tag[0] = "fig:";
      tag[1] = "ch";
      tag[2] = "s";
      tag[3] = "tab:";
      tag[4] = "eq:";
      //tag[5] = "par:";

      // elements where \ref is replaced with \eqref
      // in this case: the 4th element = equationes
      // customizable
      var eq_ref = [4];

      // program
      var columnnumber = cursor.columnNumber();
      var linenumber = cursor.lineNumber();

      var texttemp = editor.text(linenumber);
      texttemp = texttemp.substring(Math.max(columnnumber -search_range_columns,0), columnnumber);

      var pos_max = -1;
      var method = -1;
      for (var i = 0; i < ref.length ; i++){
      var pos = texttemp.search(ref[i]);
      pos_max = Math.max(pos_max,pos);
      if (pos == pos_max) {
      method = i};
      };

      if (pos_max != -1){
      var texttemp2 = texttemp.substring(texttemp.length-1,texttemp.length);
      if (texttemp2.search(/ |~/) != -1){
      cursor.deletePreviousChar();
      };
      editor.insertText("~");
      if (eq_ref.indexOf(method) != -1){
      editor.insertText("\eqref{"+tag[method]+"}");
      }
      else {
      editor.insertText("\ref{"+tag[method]+"}");
      };
      var columnnumbernew = cursor.columnNumber();
      cursor.select(linenumber, columnnumbernew-tag[method].length-1, linenumber, columnnumbernew-1);
      }
      else {
      editor.insertText("\ref");
      };

    2. Makes a suggestion for a label based on found keywords:
      I use it with the trigger: \label

      %SCRIPT
      // autolabel: searches in the near of the cursor for
      // captions and environments and makes a proper suggestion
      // for and a label based on the content
      // suggest trigger: \label
      // version 1.6

      // search range; customizable
      var line_search_range_hea_env = 5;
      var line_search_range_caption = 2;

      // regular expression with words, which are removed in the suggestion
      var shortstring = /\bthe \b|\bof \b|\bder \b|\die \b|\bdas \b|\bdes \b/ig;
      // turns this function on/off
      var shorten = 1;

      var head = new Array();
      var tagh = new Array();

      // array with headings, that are search for. e.g. chapters..
      // extendable
      head[0] = "chapter";
      head[1] = "section";
      head[2] = "subsection";
      //head[3] = "paragraph";

      tagh[0] = "cha:"
      tagh[1] = "sec:"
      tagh[2] = "ssec:"
      //tagh[3] = "par:"

      // environments
      var env = new Array();
      var tage = new Array();
      var caption = new Array();

      // array with environments
      // extendable
      env[0] = "table";
      env[1] = "figure";
      env[2] = "eqnarray";
      env[3] = "align";
      env[4] = "equation";

      tage[0] = "tab:";
      tage[1] = "fig:";
      tage[2] = "eq:";
      tage[3] = "eq:";
      tage[4] = "eq:";

      // in this environments (table and figure) is searched for a
      // caption to autolabel; customizable
      var caption_env = [0,1];

      // program
      var columnnumber = cursor.columnNumber();
      var linenumber = cursor.lineNumber();

      var found = -1;
      var found_ne = 1;
      var method_num = -1;
      var method_length = 0;

      // search lines
      for (var i_line= 0;i_line <= line_search_range_hea_env && found == -1;i_line++){
      var texttemp_ah = editor.text(linenumber-i_line);
      var texttemp_ne = editor.text(linenumber+i_line);
      if (i_line == 0){
      texttemp_ah = texttemp_ah.substring(0, columnnumber+1);
      texttemp_ne = texttemp_ne.substring(columnnumber+1,texttemp_ne.length);};
      //texttemp_ah = escape(texttemp_ah);

      // chapter,section,subsection search only upwards

      for (var i_hea = 0; i_hea < head.length ; i_hea++){

      var pos = texttemp_ah.lastIndexOf("\"+head[i_hea]+"{");
      found = Math.max(found,pos);
      // select nearest match
      if (pos == found && pos !=-1) {
      method_num = i_hea
      pos_l = linenumber-i_line;
      method_length = head[i_hea].length};
      };
      // environments: figures...: search up and downward
      for (var i_env = 0; i_env < env.length ; i_env++){
      var pos_ah1 = texttemp_ah.lastIndexOf("\begin{"+env[i_env]+"}");
      var pos_ah2 = texttemp_ah.lastIndexOf("\end{"+env[i_env]+"}");
      var pos_ne = texttemp_ne.indexOf("\end{"+env[i_env]+"}");
      var pos_ah = Math.max(pos_ah1,pos_ah2);
      found = Math.max(found,pos_ah);
      // select nearest match of forward search
      if (pos_ah == found && pos_ah !=-1) {
      method_num = i_env + head.length;
      method_length = env[i_env].length;
      };
      // look if backward search matches are closer to cursor
      if (pos_ne !=-1) {
      found_ne = Math.min(found_ne,-1pos_ne);
      if (pos_ne == -1
      found_ne) {
      var method_ne = i_env + head.length;
      };
      if (method_num == -1 || -1found_ne < texttemp_ah.length-found-method_length){
      found = -1
      found_ne;
      method_num = method_ne;
      };
      };
      };
      };

      if (found != -1) {
      if (method_num >= head.length) {
      var tag = tage[method_num-head.length];
      var pos1_c = -1;
      // search selected environments for captions
      if (caption_env.indexOf(method_num-head.length) != -1){
      for (var i_l = linenumber; i_l >= linenumber-line_search_range_caption && pos1_c ==-1 ;i_l--){
      texttemp = editor.text(i_l);
      pos1_c = texttemp.search(/\caption{/);
      if (pos1_c != -1){
      pos1_c = pos1_c + 9;
      pos_l = i_l;
      };
      };
      };
      }
      else {
      // select headings
      var tag = tagh[method_num];
      pos1_c = found + head[method_num].length+2;
      };
      if (pos1_c != -1) {
      var texttemp = editor.text(pos_l);
      texttemp = texttemp.substring(pos1_c,texttemp.length);
      pos2_c = texttemp.search("}");
      if (pos_l == linenumber && pos2_c == -1){
      pos2_c = columnnumber-pos1_c;
      }

      var textrecord = texttemp.substring(0, pos2_c);
      // replace space
      if (shorten) {
      textrecord = textrecord.replace(shortstring,"");};
      textrecord = textrecord.replace(/\s/g,"_");
      editor.insertText("\label{"+tag+textrecord + "}");
      //cursor select autolabel
      var columnnumbernew = cursor.columnNumber();
      cursor.select(linenumber, columnnumbernew-textrecord.length-1, linenumber, columnnumbernew-1)
      }
      else {
      editor.insertText("\label{"+tag+"}");
      cursor.movePosition(1,cursorEnums.Left);
      };
      }
      else {
      editor.insertText("\label");
      };

    3. The last one is smaller and extend the autocompletion for $. Works only with \$ as trigger

      %SCRIPT
      // mathinline: autoreplace $ with$$ expect for $
      // like the autocompletion of txs for brackets
      // makes only sense with \$ as trigger
      // version 1.0
      texttemp = editor.text(cursor.lineNumber());
      if (texttemp[cursor.columnNumber()] =="$"){
      cursor.movePosition(1,cursorEnums.Right);}
      else if (texttemp[cursor.columnNumber()-1] == "\" ) {
      editor.insertText("$");
      }
      else {
      editor.insertText("$$");
      cursor.movePosition(1,cursorEnums.Left);}

    I hope some of you find the macros a little bit handy. Because the trigger
    mode in texstudio is disabled, when it is in completion mode, my suggested
    triggers doesn't work, when "automatically start completer" is turned on.
    Of course the scripts could show unintended behavior. Please report bugs.