TexWorks JavaScript 转换为 SI 单位

TexWorks JavaScript 转换为 SI 单位

我尝试为 TexWorks 编辑器编写一个脚本。我希望能够将易于写入的物理单位转换为 SI-Unit latex 包的语法。

例如:

我写的:12,45A

标记它并点击我的快捷方式将其转换为(通过脚本)

\SI{12.45}{\安培}

我设法整理了以下脚本:

// TeXworksScript
// Title: convert SI
// Shortcut: Ctrl+Shift+K
// Description: Converts into SI 
// (...)
// Script-Type: standalone
// Context: TeXDocument



function addOrRemove(prefixA, suffixA, prefixB, suffixB) {
  var txt = TW.target.selection; //ausgewählter text
  //var komplett = txt.match(/\d+\,\d+[a-zA-Z]+/);
  //txt=txt.replace(",",".");
  var zahl = txt.match(/\d*\,?\d*/);
  zahl=zahl.toString().replace(",",".");
  var einheit = txt.match(/[a-zA-Z]+/);
  einheit=einheit.toString().replace("V","\\volt ");
  einheit=einheit.toString().replace("kmh","\\kilo\\meter\\per\\hour ");
  einheit=einheit.toString().replace("W","\\watt ");
  einheit=einheit.toString().replace("A","\\ampere ");
  einheit=einheit.toString().replace("m","\\meter ");
  einheit=einheit.toString().replace("g","\\gram ");
  einheit=einheit.toString().replace("kg","\\kilo\\gram ");
  einheit=einheit.toString().replace("ms","\\meter\\per\\second ");
  einheit=einheit.toString().replace("s","\\second ");
  var wrapped = prefixA + zahl + suffixA + prefixB + einheit + suffixB;
  var len = txt.length;
  var pos = TW.target.selectionStart;
  if (pos >= prefixA.length) {
    TW.target.selectRange(pos - prefixA.length, wrapped.length);
    TW.target.selectRange(pos, len);
  }
  TW.target.insertText(wrapped);
  TW.target.selectRange(pos + prefixA.length + 5 + 2*len, 0);
  return;
};

addOrRemove("\\SI{", "}", "{", "}");

这种方法还算有效。第一个问题是替换。它迭代进行,因此我得到了两次替换。 2公里每小时变成\SI{2}{\千米/小时 }

如果我能解决单个正则表达式部分并使用它们,那将会非常有帮助。

非常感谢您的帮助。

答案1

看来,我找到了答案。

// TeXworksScript
// Title: convert SI
// Shortcut: Ctrl+Shift+L
// Description: Converts into SI Syntax
// (...)
// Script-Type: standalone
// Context: TeXDocument



function replace() {
  var txt = TW.target.selection; //ausgewählter text //save selected text in txt
  var posA = TW.target.selectionStart;
  var posi = txt.search(/(\d+)(\,?\.?)(\d*)([a-zA-Z]+\/?[a-zA-Z]?\^?\d?)/g); //Position des ersten Elements //position of first element
  var item = txt.match(/(\d+)(\,?\.?)(\d*)([a-zA-Z]+\/?[a-zA-Z]?\^?\d?)/g); //Inhalt des ersten Elements //content of first element
  if (item != null){ //Prüfe, ob ein Element vorhanden ist //check if there is an element
    TW.target.selectRange(posi+posA,item[0].length); //erstes Element markieren //select first element
    var wrapped = TW.target.selection;

//Ersetzen mit SI und {} //replace with SI and brakets 
    wrapped = wrapped.replace(/(\d+)(\,?\.?)(\d*)([a-zA-Z]+\/?[a-zA-Z]?\^?\d?)/g, "\\SI{$1$2$3}{$4}");
    wrapped = wrapped.replace(",",".");


//Ersetzungen der Einheiten //replacement of the units


//Volumina //volumes

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{km\^3}/g, "\\SI{$1$2$3}{\\kilo\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{m\^3}/g, "\\SI{$1$2$3}{\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{dm\^3}/g, "\\SI{$1$2$3}{\\deci\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{cm\^3}/g, "\\SI{$1$2$3}{\\centi\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mm\^3}/g, "\\SI{$1$2$3}{\\milli\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{ml}/g, "\\SI{$1$2$3}{\\milli\\liter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{l}/g, "\\SI{$1$2$3}{\\liter}");



//Flächen //areas

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{km\^2}/g, "\\SI{$1$2$3}{\\kilo\\meter\\squared}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{m\^2}/g, "\\SI{$1$2$3}{\\meter\\squared}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{dm\^2}/g, "\\SI{$1$2$3}{\\deci\\meter\\squared}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{cm\^2}/g, "\\SI{$1$2$3}{\\centi\\meter\\squared}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mm\^2}/g, "\\SI{$1$2$3}{\\milli\\meter\\squared}");

//Längen //lengths

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{km}/g, "\\SI{$1$2$3}{\\kilo\\meter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{m}/g, "\\SI{$1$2$3}{\\meter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{dm}/g, "\\SI{$1$2$3}{\\deci\\meter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{cm}/g, "\\SI{$1$2$3}{\\centi\\meter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mm}/g, "\\SI{$1$2$3}{\\milli\\meter}");





//Elektrizität //electricity

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kA}/g, "\\SI{$1$2$3}{\\kilo\\ampere}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{A}/g, "\\SI{$1$2$3}{\\ampere}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mA}/g, "\\SI{$1$2$3}{\milli\\ampere}");

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kV}/g, "\\SI{$1$2$3}{\\kilo\\volt}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{V}/g, "\\SI{$1$2$3}{\\volt}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mV}/g, "\\SI{$1$2$3}{\milli\\volt}");

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kC}/g, "\\SI{$1$2$3}{\\kilo\\coulomb}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{C}/g, "\\SI{$1$2$3}{\\coulomb}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mC}/g, "\\SI{$1$2$3}{\\milli\\coulomb}");

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kW}/g, "\\SI{$1$2$3}{\\kilo\\watt}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{W}/g, "\\SI{$1$2$3}{\\watt}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mW}/g, "\\SI{$1$2$3}{\\milli\\watt}");

    
//Masse //mass
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mg}/g, "\\SI{$1$2$3}{\\milli\\gram}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{g}/g, "\\SI{$1$2$3}{\\gram}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kg}/g, "\\SI{$1$2$3}{\\kilo\\gram}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{t}/g, "\\SI{$1$2$3}{\\tonne}");


//Zeit //time
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{d}/g, "\\SI{$1$2$3}{\\day}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{h}/g, "\\SI{$1$2$3}{\\hour}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{min}/g, "\\SI{$1$2$3}{\\minute}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{s}/g, "\\SI{$1$2$3}{\\second}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{ms}/g, "\\SI{$1$2$3}{\\milli\\second}");



    TW.target.insertText(wrapped);
    TW.target.selectRange(posA,txt.length+wrapped.length-item[0].length); //ursprüngliche Auswahl markieren //select original slelected text
  }
  return;
};

replace();

相关内容