Comparing version 0.2.3 to 0.2.6
import { Tex2TypstOptions } from "./types"; | ||
import { symbolMap } from "./map"; | ||
export declare function tex2typst(tex: string, options?: Tex2TypstOptions): string; | ||
export { Tex2TypstOptions }; | ||
export { symbolMap, Tex2TypstOptions }; |
@@ -192,13 +192,11 @@ // src/parser.ts | ||
const firstTwoChars = latex.slice(pos, pos + 2); | ||
if (firstTwoChars === "\\\\") { | ||
token = { type: "control", value: "\\\\" }; | ||
pos += 2; | ||
if (["\\\\", "\\,"].includes(firstTwoChars)) { | ||
token = { type: "control", value: firstTwoChars }; | ||
} else if (["\\{", "\\}", "\\%", "\\$", "\\&", "\\#", "\\_"].includes(firstTwoChars)) { | ||
token = { type: "element", value: firstTwoChars }; | ||
pos += 2; | ||
} else { | ||
const command = eat_command_name(latex, pos + 1); | ||
token = { type: "command", value: "\\" + command }; | ||
pos += 1 + command.length; | ||
} | ||
pos += token.value.length; | ||
break; | ||
@@ -263,6 +261,2 @@ } | ||
"text", | ||
"arccos", | ||
"arcsin", | ||
"arctan", | ||
"arg", | ||
"bar", | ||
@@ -272,19 +266,13 @@ "bold", | ||
"ddot", | ||
"det", | ||
"dim", | ||
"dot", | ||
"exp", | ||
"gcd", | ||
"hat", | ||
"ker", | ||
"mathbb", | ||
"mathbf", | ||
"mathcal", | ||
"mathfrak", | ||
"mathit", | ||
"mathrm", | ||
"mathscr", | ||
"mathsf", | ||
"mathtt", | ||
"mathrm", | ||
"max", | ||
"min", | ||
"mod", | ||
"operatorname", | ||
@@ -294,3 +282,2 @@ "overbrace", | ||
"pmb", | ||
"sup", | ||
"rm", | ||
@@ -456,2 +443,4 @@ "tilde", | ||
return [{ type: "control", content: "\\\\" }, start + 1]; | ||
case "\\,": | ||
return [{ type: "control", content: "\\," }, start + 1]; | ||
case "_": { | ||
@@ -623,3 +612,2 @@ let [sub, pos] = this.parseNextExpr(tokens, start + 1); | ||
var symbolMap = new Map([ | ||
["gets", "arrow.l"], | ||
["nonumber", ""], | ||
@@ -649,9 +637,10 @@ ["vec", "arrow"], | ||
["boldsymbol", "bold"], | ||
["mathbb", "bb"], | ||
["mathbf", "bold"], | ||
["mathbb", "bb"], | ||
["mathcal", "cal"], | ||
["mathit", "italic"], | ||
["mathfrak", "frak"], | ||
["mathrm", "upright"], | ||
["mathsf", "sans"], | ||
["mathtt", "mono"], | ||
["mathrm", "upright"], | ||
["rm", "upright"], | ||
@@ -672,9 +661,2 @@ ["pmb", "bold"], | ||
["propto", "prop"], | ||
["implies", "arrow.r.double.long"], | ||
["Longrightarrow", "arrow.r.double.long"], | ||
["iff", "arrow.l.r.double.long"], | ||
["Longleftrightarrow", "arrow.l.r.double.long"], | ||
["leftrightarrow", "arrow.l.r"], | ||
["longleftrightarrow", "arrow.l.r.long"], | ||
["rightrightarrows", "arrows.rr"], | ||
["lfloor", "\u230A"], | ||
@@ -684,2 +666,32 @@ ["rfloor", "\u230B"], | ||
["rceil", "\u2309"], | ||
["gets", "arrow.l"], | ||
["hookleftarrow", "arrow.l.hook"], | ||
["leftharpoonup", "harpoon.lt"], | ||
["leftharpoondown", "harpoon.lb"], | ||
["rightleftharpoons", "harpoons.rtlb"], | ||
["longleftarrow", "arrow.l.long"], | ||
["longrightarrow", "arrow.r.long"], | ||
["longleftrightarrow", "arrow.l.r.long"], | ||
["Longleftarrow", "arrow.l.double.long"], | ||
["Longrightarrow", "arrow.r.double.long"], | ||
["Longleftrightarrow", "arrow.l.r.double.long"], | ||
["longmapsto", "arrow.r.bar"], | ||
["hookrightarrow", "arrow.r.hook"], | ||
["rightharpoonup", "harpoon.rt"], | ||
["rightharpoondown", "harpoon.rb"], | ||
["iff", "arrow.l.r.double.long"], | ||
["implies", "arrow.r.double.long"], | ||
["uparrow", "arrow.t"], | ||
["downarrow", "arrow.b"], | ||
["updownarrow", "arrow.t.b"], | ||
["Uparrow", "arrow.t.double"], | ||
["Downarrow", "arrow.b.double"], | ||
["Updownarrow", "arrow.t.b.double"], | ||
["nearrow", "arrow.tr"], | ||
["searrow", "arrow.br"], | ||
["swarrow", "arrow.bl"], | ||
["nwarrow", "arrow.tl"], | ||
["leadsto", "arrow.squiggly"], | ||
["leftleftarrows", "arrows.ll"], | ||
["rightrightarrows", "arrows.rr"], | ||
["Cap", "sect.double"], | ||
@@ -691,8 +703,12 @@ ["Cup", "union.double"], | ||
["Lambda", "Lambda"], | ||
["Leftarrow", "arrow.l.double"], | ||
["Leftrightarrow", "arrow.l.r.double"], | ||
["Longrightarrow", "arrow.r.double.long"], | ||
["Omega", "Omega"], | ||
["P", "pilcrow"], | ||
["Phi", "Phi"], | ||
["Pi", "Pi"], | ||
["Psi", "Psi"], | ||
["Rightarrow", "arrow.double"], | ||
["Rightarrow", "arrow.r.double"], | ||
["S", "section"], | ||
["Sigma", "Sigma"], | ||
@@ -729,2 +745,3 @@ ["Theta", "Theta"], | ||
["coprod", "product.co"], | ||
["copyright", "copyright"], | ||
["cup", "union"], | ||
@@ -782,3 +799,3 @@ ["curlyvee", "or.curly"], | ||
["ll", "lt.double"], | ||
["longmapsto", "arrow.long.bar"], | ||
["longmapsto", "arrow.bar.long"], | ||
["longrightarrow", "arrow.long"], | ||
@@ -1156,2 +1173,4 @@ ["lor", "or"], | ||
this.queue.push({ type: "symbol", content: node.content }); | ||
} else if (node.content === "\\,") { | ||
this.queue.push({ type: "symbol", content: "thin" }); | ||
} else { | ||
@@ -1257,3 +1276,4 @@ throw new TypstWriterError(`Unknown control sequence: ${node.content}`, node); | ||
export { | ||
tex2typst | ||
tex2typst, | ||
symbolMap | ||
}; |
@@ -1,1 +0,1 @@ | ||
function G(J,Z=""){if(!J)throw new v(Z)}function c(J){if(E.includes(J))return 1;else if(m.includes(J))return 2;else return 0}function x(J,Z){G(H(J[Z],D));let V=1,$=Z+1;while(V>0){if($>=J.length)throw new v("Unmatched curly brackets");if(H(J[$],D))V+=1;else if(H(J[$],S))V-=1;$+=1}return $-1}function l(J,Z){G(H(J[Z],I));let V=1,$=Z+1;while(V>0){if($>=J.length)throw new v("Unmatched square brackets");if(H(J[$],I))V+=1;else if(H(J[$],i))V-=1;$+=1}return $-1}function C(J){return"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".includes(J)}function w(J){return"0123456789".includes(J)}function N(J,Z){let V=Z;while(V<J.length&&["whitespace","newline"].includes(J[V].type))V++;return J.slice(Z,V)}function R(J,Z){const V=J[Z];if(V.type==="element"&&["(",")","[","]","|","\\{","\\}"].includes(V.value))return V;else if(V.type==="command"&&["lfloor","rfloor","lceil","rceil","langle","rangle"].includes(V.value.slice(1)))return V;else return null}function K(J,Z){let V=Z;while(V<J.length&&H(J[V],{type:"element",value:"'"}))V+=1;return V-Z}function _(J,Z){let V=Z;while(V<J.length&&C(J[V]))V+=1;return J.substring(Z,V)}function p(J,Z){let V=1,$=Z;while(V>0){if($>=J.length)return-1;if(H(J[$],q))V+=1;else if(H(J[$],d))V-=1;$+=1}return $-1}function a(J,Z){let V=1,$=Z;while(V>0){if($>=J.length)return-1;if(H(J[$],M))V+=1;else if(H(J[$],k))V-=1;$+=1}return $-1}function r(J,Z){G(J[Z]==="{");let V=1,$=Z+1;while(V>0){if($>=J.length)throw new v("Unmatched curly brackets");if($+1<J.length&&["\\{","\\}"].includes(J.substring($,$+2))){$+=2;continue}if(J[$]==="{")V+=1;else if(J[$]==="}")V-=1;$+=1}return $-1}function g(J){const Z=[];let V=0;while(V<J.length){const $=J[V];let X;switch($){case"%":{let z=V+1;while(z<J.length&&J[z]!=="\n")z+=1;X={type:"comment",value:J.slice(V+1,z)},V=z;break}case"{":case"}":case"_":case"^":case"&":X={type:"control",value:$},V++;break;case"\n":X={type:"newline",value:$},V++;break;case"\r":{if(V+1<J.length&&J[V+1]==="\n")X={type:"newline",value:"\n"},V+=2;else X={type:"newline",value:"\n"},V++;break}case" ":{let z=V;while(z<J.length&&J[z]===" ")z+=1;X={type:"whitespace",value:J.slice(V,z)},V=z;break}case"\\":{if(V+1>=J.length)throw new v("Expecting command name after \\");const z=J.slice(V,V+2);if(z==="\\\\")X={type:"control",value:"\\\\"},V+=2;else if(["\\{","\\}","\\%","\\$","\\&","\\#","\\_"].includes(z))X={type:"element",value:z},V+=2;else{const j=_(J,V+1);X={type:"command",value:"\\"+j},V+=1+j.length}break}default:{if(w($)){let z=V;while(z<J.length&&w(J[z]))z+=1;X={type:"element",value:J.slice(V,z)}}else if(C($))X={type:"element",value:$};else if("+-*/=\'<>!.,;?()[]|".includes($))X={type:"element",value:$};else X={type:"unknown",value:$};V+=X.value.length}}if(Z.push(X),X.type==="command"&&["\\text","\\begin","\\end"].includes(X.value)){if(V>=J.length||J[V]!=="{")throw new v(`No content for ${X.value} command`);Z.push({type:"control",value:"{"});const z=r(J,V);V++;let j=J.slice(V,z);const Q=["{","}","\\","$","&","#","_","%"];for(let W of Q)j=j.replaceAll("\\"+W,W);Z.push({type:"text",value:j}),Z.push({type:"control",value:"}"}),V=z+1}}return Z}function H(J,Z){return J.type==Z.type&&J.value==Z.value}function b(J,Z){const V=new T,$=g(J);let X=[];for(let z of $)if(z.type==="command"&&Z[z.value]){const j=g(Z[z.value]);X=X.concat(j)}else X.push(z);return V.parse(X)}var E=["sqrt","text","arccos","arcsin","arctan","arg","bar","bold","boldsymbol","ddot","det","dim","dot","exp","gcd","hat","ker","mathbb","mathbf","mathcal","mathscr","mathsf","mathtt","mathrm","max","min","mod","operatorname","overbrace","overline","pmb","sup","rm","tilde","underbrace","underline","vec","widehat","widetilde"],m=["frac","tfrac","binom","dbinom","dfrac","tbinom"],Y={type:"empty",content:""},D={type:"control",value:"{"},S={type:"control",value:"}"},I={type:"element",value:"["},i={type:"element",value:"]"},q={type:"command",value:"\\left"},d={type:"command",value:"\\right"},M={type:"command",value:"\\begin"},k={type:"command",value:"\\end"};class v extends Error{constructor(J){super(J);this.name="LatexParserError"}}var y={type:"control",value:"_"},A={type:"control",value:"^"};class T{space_sensitive;newline_sensitive;constructor(J=!1,Z=!0){this.space_sensitive=J,this.newline_sensitive=Z}parse(J){const Z=[];let V=0;while(V<J.length){const $=[];let X=0;while(X<J.length){const[z,j]=this.parseNextExpr(J,X);if(X=j,!this.space_sensitive&&z.type==="whitespace")continue;if(!this.newline_sensitive&&z.type==="newline")continue;if(z.type==="control"&&z.content==="&")throw new v("Unexpected & outside of an alignment");$.push(z)}if($.length===0)return Y;else if($.length===1)return $[0];else return{type:"ordgroup",content:"",args:$}}if(Z.length===0)return Y;else if(Z.length===1)return Z[0];else return{type:"ordgroup",content:"",args:Z}}parseNextExpr(J,Z){let[V,$]=this.parseNextExprWithoutSupSub(J,Z),X=null,z=null,j=0;if(j+=K(J,$),$+=j,$<J.length&&H(J[$],y)){if([X,$]=this.parseNextExprWithoutSupSub(J,$+1),j+=K(J,$),$+=j,$<J.length&&H(J[$],A)){if([z,$]=this.parseNextExprWithoutSupSub(J,$+1),K(J,$)>0)throw new v("Double superscript")}}else if($<J.length&&H(J[$],A)){if([z,$]=this.parseNextExprWithoutSupSub(J,$+1),K(J,$)>0)throw new v("Double superscript");if($<J.length&&H(J[$],y)){if([X,$]=this.parseNextExprWithoutSupSub(J,$+1),K(J,$)>0)throw new v("Double superscript")}}if(X!==null||z!==null||j>0){const Q={base:V};if(X)Q.sub=X;if(j>0){Q.sup={type:"ordgroup",content:"",args:[]};for(let W=0;W<j;W++)Q.sup.args.push({type:"symbol",content:"\\prime"});if(z)Q.sup.args.push(z);if(Q.sup.args.length===1)Q.sup=Q.sup.args[0]}else if(z)Q.sup=z;return[{type:"supsub",content:"",data:Q},$]}else return[V,$]}parseNextExprWithoutSupSub(J,Z){const V=J[Z],$=V.type;switch($){case"element":case"text":case"comment":case"whitespace":case"newline":return[{type:$,content:V.value},Z+1];case"command":if(H(V,M))return this.parseBeginEndExpr(J,Z);else if(H(V,q))return this.parseLeftRightExpr(J,Z);else return this.parseCommandExpr(J,Z);case"control":switch(V.value){case"{":const z=x(J,Z),j=J.slice(Z+1,z);return[this.parse(j),z+1];case"}":throw new v("Unmatched '}'");case"\\\\":return[{type:"control",content:"\\\\"},Z+1];case"_":{let[Q,W]=this.parseNextExpr(J,Z+1),F=void 0;if(W<J.length&&H(J[W],A))[F,W]=this.parseNextExpr(J,W+1);return[{type:"supsub",content:"",data:{base:Y,sub:Q,sup:F}},W]}case"^":{let[Q,W]=this.parseNextExpr(J,Z+1),F=void 0;if(W<J.length&&H(J[W],y))[F,W]=this.parseNextExpr(J,W+1);return[{type:"supsub",content:"",data:{base:Y,sub:F,sup:Q}},W]}case"&":return[{type:"control",content:"&"},Z+1];default:throw new v("Unknown control sequence")}default:throw new v("Unknown token type")}}parseCommandExpr(J,Z){G(J[Z].type==="command");const V=J[Z].value;let $=Z+1;if(["left","right","begin","end"].includes(V.slice(1)))throw new v("Unexpected command: "+V);const X=c(V.slice(1));if(X===0)return[{type:"symbol",content:V},$];else if(X===1){if(V==="\\sqrt"&&$<J.length&&H(J[$],I)){const Q=$,W=l(J,$),F=J.slice(Q+1,W),U=this.parse(F),[f,u]=this.parseNextExprWithoutSupSub(J,W+1);return[{type:"unaryFunc",content:V,args:[f],data:U},u]}else if(V==="\\text"){if($+2>=J.length)throw new v("Expecting content for \\text command");return G(H(J[$],D)),G(J[$+1].type==="text"),G(H(J[$+2],S)),[{type:"text",content:J[$+1].value},$+3]}let[z,j]=this.parseNextExprWithoutSupSub(J,$);return[{type:"unaryFunc",content:V,args:[z]},j]}else if(X===2){const[z,j]=this.parseNextExprWithoutSupSub(J,$),[Q,W]=this.parseNextExprWithoutSupSub(J,j);return[{type:"binaryFunc",content:V,args:[z,Q]},W]}else throw new Error("Invalid number of parameters")}parseLeftRightExpr(J,Z){G(H(J[Z],q));let V=Z+1;if(V+=N(J,V).length,V>=J.length)throw new v("Expecting delimiter after \\left");const $=R(J,V);if($===null)throw new v("Invalid delimiter after \\left");V++;const X=V,z=p(J,V);if(z===-1)throw new v("No matching \\right");const j=z;if(V=z+1,V+=N(J,V).length,V>=J.length)throw new v("Expecting \\right after \\left");const Q=R(J,V);if(Q===null)throw new v("Invalid delimiter after \\right");V++;const W=J.slice(X,j),F=this.parse(W);return[{type:"leftright",content:"",args:[{type:"element",content:$.value},F,{type:"element",content:Q.value}]},V]}parseBeginEndExpr(J,Z){G(H(J[Z],M));let V=Z+1;G(H(J[V],D)),G(J[V+1].type==="text"),G(H(J[V+2],S));const $=J[V+1].value;V+=3,V+=N(J,V).length;const X=V,z=a(J,V);if(z===-1)throw new v("No matching \\end");const j=z;if(V=z+1,G(H(J[V],D)),G(J[V+1].type==="text"),G(H(J[V+2],S)),J[V+1].value!==$)throw new v("Mismatched \\begin and \\end environments");V+=3;const Q=J.slice(X,j);while(Q.length>0&&["whitespace","newline"].includes(Q[Q.length-1].type))Q.pop();const W=this.parseAligned(Q);return[{type:"beginend",content:$,data:W},V]}parseAligned(J){let Z=0;const V=[];let $=[];V.push($);let X={type:"ordgroup",content:"",args:[]};$.push(X);while(Z<J.length){const[z,j]=this.parseNextExpr(J,Z);if(Z=j,z.type==="whitespace")continue;else if(z.type==="newline"&&!this.newline_sensitive)continue;else if(z.type==="control"&&z.content==="\\\\")$=[],X={type:"ordgroup",content:"",args:[]},$.push(X),V.push($);else if(z.type==="control"&&z.content==="&")X={type:"ordgroup",content:"",args:[]},$.push(X);else X.args.push(z)}return V}}var B=new Map([["gets","arrow.l"],["nonumber",""],["vec","arrow"],["neq","eq.not"],["dot","dot"],["ddot","dot.double"],["doteq","dot(eq)"],["dots","dots.h"],["ldots","dots.h"],["vdots","dots.v"],["ddots","dots.down"],["widehat","hat"],["widetilde","tilde"],["quad","quad"],["qquad","wide"],["overbrace","overbrace"],["underbrace","underbrace"],["overline","overline"],["underline","underline"],["bar","macron"],["dbinom","binom"],["tbinom","binom"],["dfrac","frac"],["tfrac","frac"],["boldsymbol","bold"],["mathbf","bold"],["mathbb","bb"],["mathcal","cal"],["mathfrak","frak"],["mathsf","sans"],["mathtt","mono"],["mathrm","upright"],["rm","upright"],["pmb","bold"],["pm","plus.minus"],["mp","minus.plus"],["oplus","xor"],["boxplus","plus.square"],["otimes","times.circle"],["boxtimes","times.square"],["sim","tilde"],["approx","approx"],["cong","tilde.equiv"],["simeq","tilde.eq"],["asymp","\u224D"],["equiv","equiv"],["propto","prop"],["implies","arrow.r.double.long"],["Longrightarrow","arrow.r.double.long"],["iff","arrow.l.r.double.long"],["Longleftrightarrow","arrow.l.r.double.long"],["leftrightarrow","arrow.l.r"],["longleftrightarrow","arrow.l.r.long"],["rightrightarrows","arrows.rr"],["lfloor","\u230A"],["rfloor","\u230B"],["lceil","\u2308"],["rceil","\u2309"],["Cap","sect.double"],["Cup","union.double"],["Delta","Delta"],["Gamma","Gamma"],["Join","join"],["Lambda","Lambda"],["Longrightarrow","arrow.r.double.long"],["Omega","Omega"],["Phi","Phi"],["Pi","Pi"],["Psi","Psi"],["Rightarrow","arrow.double"],["Sigma","Sigma"],["Theta","Theta"],["aleph","alef"],["alpha","alpha"],["angle","angle"],["approx","approx"],["approxeq","approx.eq"],["ast","ast"],["beta","beta"],["bigcap","sect.big"],["bigcirc","circle.big"],["bigcup","union.big"],["bigodot","dot.circle.big"],["bigoplus","xor.big"],["bigotimes","times.circle.big"],["bigsqcup","union.sq.big"],["bigtriangledown","triangle.b"],["bigtriangleup","triangle.t"],["biguplus","union.plus.big"],["bigvee","or.big"],["bigwedge","and.big"],["bullet","bullet"],["cap","sect"],["cdot","dot.op"],["cdots","dots.c"],["checkmark","checkmark"],["chi","chi"],["circ","circle.small"],["colon","colon"],["cong","tilde.equiv"],["coprod","product.co"],["cup","union"],["curlyvee","or.curly"],["curlywedge","and.curly"],["dagger","dagger"],["dashv","tack.l"],["ddagger","dagger.double"],["delta","delta"],["ddots","dots.down"],["diamond","diamond"],["div","div"],["divideontimes","times.div"],["dotplus","plus.dot"],["downarrow","arrow.b"],["ell","ell"],["emptyset","nothing"],["epsilon","epsilon.alt"],["equiv","equiv"],["eta","eta"],["exists","exists"],["forall","forall"],["gamma","gamma"],["ge","gt.eq"],["geq","gt.eq"],["geqslant","gt.eq.slant"],["gg","gt.double"],["hbar","planck.reduce"],["imath","dotless.i"],["iiiint","intgral.quad"],["iiint","integral.triple"],["iint","integral.double"],["in","in"],["infty","infinity"],["int","integral"],["intercal","top"],["iota","iota"],["jmath","dotless.j"],["kappa","kappa"],["lambda","lambda"],["land","and"],["langle","angle.l"],["lbrace","brace.l"],["lbrack","bracket.l"],["ldots","dots.l"],["le","lt.eq"],["leadsto","arrow.squiggly"],["leftarrow","arrow.l"],["leftthreetimes","times.three.l"],["leftrightarrow","arrow.l.r"],["leq","lt.eq"],["leqslant","lt.eq.slant"],["lhd","triangle.l"],["ll","lt.double"],["longmapsto","arrow.long.bar"],["longrightarrow","arrow.long"],["lor","or"],["ltimes","times.l"],["mapsto","arrow.bar"],["measuredangle","angle.arc"],["mid","divides"],["models","models"],["mp","minus.plus"],["mu","mu"],["nRightarrow","arrow.double.not"],["nabla","nabla"],["ncong","tilde.nequiv"],["ne","eq.not"],["neg","not"],["neq","eq.not"],["nexists","exists.not"],["ni","in.rev"],["nleftarrow","arrow.l.not"],["nleq","lt.eq.not"],["nparallel","parallel.not"],["ngeq","gt.eq.not"],["nmid","divides.not"],["notin","in.not"],["nrightarrow","arrow.not"],["nsim","tilde.not"],["nsubseteq","subset.eq.not"],["nu","nu"],["ntriangleleft","lt.tri.not"],["ntriangleright","gt.tri.not"],["nwarrow","arrow.tl"],["odot","dot.circle"],["oint","integral.cont"],["oiint","integral.surf"],["oiiint","integral.vol"],["omega","omega"],["ominus","minus.circle"],["oplus","xor"],["otimes","times.circle"],["parallel","parallel"],["partial","diff"],["perp","perp"],["phi","phi.alt"],["pi","pi"],["pm","plus.minus"],["pounds","pound"],["prec","prec"],["preceq","prec.eq"],["prime","prime"],["prod","product"],["propto","prop"],["psi","psi"],["rangle","angle.r"],["rbrace","brace.r"],["rbrack","bracket.r"],["rhd","triangle"],["rho","rho"],["rightarrow","arrow.r"],["rightthreetimes","times.three.r"],["rtimes","times.r"],["setminus","without"],["sigma","sigma"],["sim","tilde"],["simeq","tilde.eq"],["slash","slash"],["smallsetminus","without"],["spadesuit","suit.spade"],["sqcap","sect.sq"],["sqcup","union.sq"],["sqsubseteq","subset.eq.sq"],["sqsupseteq","supset.eq.sq"],["star","star"],["subset","subset"],["subseteq","subset.eq"],["subsetneq","subset.neq"],["succ","succ"],["succeq","succ.eq"],["sum","sum"],["supset","supset"],["supseteq","supset.eq"],["supsetneq","supset.neq"],["swarrow","arrow.bl"],["tau","tau"],["theta","theta"],["times","times"],["to","arrow.r"],["top","top"],["triangle","triangle.t"],["triangledown","triangle.b.small"],["triangleleft","triangle.l.small"],["triangleright","triangle.r.small"],["twoheadrightarrow","arrow.r.twohead"],["uparrow","arrow.t"],["updownarrow","arrow.t.b"],["upharpoonright","harpoon.tr"],["uplus","union.plus"],["upsilon","upsilon"],["varepsilon","epsilon"],["varnothing","diameter"],["varphi","phi"],["varpi","pi.alt"],["varrho","rho.alt"],["varsigma","sigma.alt"],["vartheta","theta.alt"],["vdash","tack.r"],["vdots","dots.v"],["vee","or"],["wedge","and"],["wr","wreath"],["xi","xi"],["yen","yen"],["zeta","zeta"],["mathscr","scr"],["LaTeX","#LaTeX"],["TeX","#TeX"]]);function L(J){if(/^[a-zA-Z0-9]$/.test(J))return J;else if(J==="\\\\")return"\\";else if(J=="/")return"\\/";else if(["\\$","\\#","\\&","\\_"].includes(J))return J;else if(J.startsWith("\\")){const Z=J.slice(1);if(B.has(Z))return B.get(Z);else return Z}return J}var n=["dim","id","im","mod","Pr","sech","csch"];class O extends Error{node;constructor(J,Z){super(J);this.name="TypstWriterError",this.node=Z}}class h{nonStrict;preferTypstIntrinsic;buffer="";queue=[];needSpaceAfterSingleItemScript=!1;insideFunctionDepth=0;constructor(J,Z){this.nonStrict=J,this.preferTypstIntrinsic=Z}writeBuffer(J){if(this.needSpaceAfterSingleItemScript&&/^[0-9a-zA-Z\(]/.test(J))this.buffer+=" ";else{let Z=!1;if(Z||=/[\(\|]$/.test(this.buffer)&&/^\w/.test(J),Z||=/^[}()_^,;!\|]$/.test(J),Z||=J==="'",Z||=/[0-9]$/.test(this.buffer)&&/^[0-9]/.test(J),Z||=/[\(\[{]\s*(-|\+)$/.test(this.buffer)||this.buffer==="-"||this.buffer==="+",Z||=J.startsWith("\n"),Z||=this.buffer==="",Z||=/[\s"_^{\(]$/.test(this.buffer),!Z)this.buffer+=" "}if(this.needSpaceAfterSingleItemScript)this.needSpaceAfterSingleItemScript=!1;this.buffer+=J}append(J){if(J.type==="empty"||J.type==="whitespace")return;else if(J.type==="ordgroup")J.args.forEach((Z)=>this.append(Z));else if(J.type==="element"){let Z=J.content;if(J.content===","&&this.insideFunctionDepth>0)Z="comma";this.queue.push({type:"symbol",content:Z})}else if(J.type==="symbol")this.queue.push({type:"symbol",content:J.content});else if(J.type==="text")this.queue.push(J);else if(J.type==="supsub"){let{base:Z,sup:V,sub:$}=J.data;if(Z&&Z.type==="unaryFunc"&&Z.content==="\\overbrace"&&V){this.append({type:"binaryFunc",content:"\\overbrace",args:[Z.args[0],V]});return}else if(Z&&Z.type==="unaryFunc"&&Z.content==="\\underbrace"&&$){this.append({type:"binaryFunc",content:"\\underbrace",args:[Z.args[0],$]});return}if(Z.type==="empty")this.queue.push({type:"text",content:""});else this.appendWithBracketsIfNeeded(Z);let X=!1;const z=V&&V.type==="symbol"&&V.content==="\\prime";if(z)this.queue.push({type:"atom",content:"\'"}),X=!1;if($)this.queue.push({type:"atom",content:"_"}),X=this.appendWithBracketsIfNeeded($);if(V&&!z)this.queue.push({type:"atom",content:"^"}),X=this.appendWithBracketsIfNeeded(V);if(X)this.queue.push({type:"softSpace",content:""})}else if(J.type==="leftright"){const[Z,V,$]=J.args;if(["[]","()","\\{\\}","\\lfloor\\rfloor","\\lceil\\rceil"].includes(Z.content+$.content)){this.append(Z),this.append(V),this.append($);return}const X={type:"symbol",content:"lr"};this.queue.push(X),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(Z),this.append(V),this.append($),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--}else if(J.type==="binaryFunc"){const Z={type:"symbol",content:J.content},[V,$]=J.args;this.queue.push(Z),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(V),this.queue.push({type:"atom",content:","}),this.append($),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--}else if(J.type==="unaryFunc"){const Z={type:"symbol",content:J.content},V=J.args[0];if(J.content==="\\sqrt"&&J.data){Z.content="root",this.queue.push(Z),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(J.data),this.queue.push({type:"atom",content:","}),this.append(V),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--;return}else if(J.content==="\\mathbf"){this.append({type:"symbol",content:"upright"}),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.queue.push(Z),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(V),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--,this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--;return}else if(J.content==="\\mathbb"){const $=J.args[0];if($.type==="element"&&/^[A-Z]$/.test($.content)){this.queue.push({type:"symbol",content:$.content+$.content});return}}else if(J.content==="\\operatorname"){let $=J.args;if($.length===1&&$[0].type=="ordgroup")$=$[0].args;const X=$.reduce((z,j)=>{return z+=L(j.content),z},"");if(this.preferTypstIntrinsic&&n.includes(X))this.queue.push({type:"symbol",content:X});else this.queue.push({type:"symbol",content:"op"}),this.queue.push({type:"atom",content:"("}),this.queue.push({type:"text",content:X}),this.queue.push({type:"atom",content:")"});return}this.queue.push(Z),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(V),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--}else if(J.type==="newline"){this.queue.push({type:"newline",content:"\n"});return}else if(J.type==="beginend")if(J.content.startsWith("align")){const Z=J.data;Z.forEach((V,$)=>{if(V.forEach((X,z)=>{if(z>0)this.queue.push({type:"atom",content:"&"});this.append(X)}),$<Z.length-1)this.queue.push({type:"symbol",content:"\\\\"})})}else{const Z=J.data;this.queue.push({type:"symbol",content:"mat"}),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.queue.push({type:"symbol",content:"delim: #none, "}),Z.forEach((V,$)=>{V.forEach((X,z)=>{if(X.type==="ordgroup"&&X.args.length===0){this.queue.push({type:"atom",content:","});return}if(this.append(X),z<V.length-1)this.queue.push({type:"atom",content:","});else if($<Z.length-1)this.queue.push({type:"atom",content:";"})})}),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--}else if(J.type==="matrix");else if(J.type==="unknownMacro")if(this.nonStrict)this.queue.push({type:"symbol",content:J.content});else throw new O(`Unknown macro: ${J.content}`,J);else if(J.type==="control")if(J.content==="\\\\")this.queue.push({type:"symbol",content:J.content});else throw new O(`Unknown control sequence: ${J.content}`,J);else if(J.type==="comment")this.queue.push({type:"comment",content:J.content});else throw new O(`Unimplemented node type to append: ${J.type}`,J)}flushQueue(){this.queue.forEach((J)=>{let Z="";switch(J.type){case"atom":Z=J.content;break;case"symbol":Z=L(J.content);break;case"text":Z=`"${J.content}"`;break;case"softSpace":this.needSpaceAfterSingleItemScript=!0,Z="";break;case"comment":Z=`//${J.content}`;break;case"newline":Z="\n";break;default:throw new O(`Unexpected node type to stringify: ${J.type}`,J)}if(Z!=="")this.writeBuffer(Z)}),this.queue=[]}appendWithBracketsIfNeeded(J){const Z=["symbol","element","unaryFunc","binaryFunc","leftright"].includes(J.type);if(Z)this.append(J);else this.queue.push({type:"atom",content:"("}),this.append(J),this.queue.push({type:"atom",content:")"});return Z}finalize(){this.flushQueue();const J=function(V){let $=V.replace(/⌊\s*(.*?)\s*⌋/g,"floor($1)");return $=$.replace(/floor\(\)/g,'floor("")'),$},Z=function(V){let $=V.replace(/⌈\s*(.*?)\s*⌉/g,"ceil($1)");return $=$.replace(/ceil\(\)/g,'ceil("")'),$};return this.buffer=J(this.buffer),this.buffer=Z(this.buffer),this.buffer}}function P(J,Z){const V={nonStrict:!1,preferTypstIntrinsic:!0,customTexMacros:{}};if(Z){if(Z.nonStrict)V.nonStrict=Z.nonStrict;if(Z.preferTypstIntrinsic)V.preferTypstIntrinsic=Z.preferTypstIntrinsic;if(Z.customTexMacros)V.customTexMacros=Z.customTexMacros}const $=b(J,V.customTexMacros),X=new h(V.nonStrict,V.preferTypstIntrinsic);return X.append($),X.finalize()}if(typeof window!=="undefined")window.tex2typst=P; | ||
function G(J,Z=""){if(!J)throw new v(Z)}function c(J){if(E.includes(J))return 1;else if(m.includes(J))return 2;else return 0}function x(J,Z){G(H(J[Z],D));let V=1,$=Z+1;while(V>0){if($>=J.length)throw new v("Unmatched curly brackets");if(H(J[$],D))V+=1;else if(H(J[$],S))V-=1;$+=1}return $-1}function l(J,Z){G(H(J[Z],M));let V=1,$=Z+1;while(V>0){if($>=J.length)throw new v("Unmatched square brackets");if(H(J[$],M))V+=1;else if(H(J[$],i))V-=1;$+=1}return $-1}function C(J){return"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".includes(J)}function R(J){return"0123456789".includes(J)}function N(J,Z){let V=Z;while(V<J.length&&["whitespace","newline"].includes(J[V].type))V++;return J.slice(Z,V)}function w(J,Z){const V=J[Z];if(V.type==="element"&&["(",")","[","]","|","\\{","\\}"].includes(V.value))return V;else if(V.type==="command"&&["lfloor","rfloor","lceil","rceil","langle","rangle"].includes(V.value.slice(1)))return V;else return null}function K(J,Z){let V=Z;while(V<J.length&&H(J[V],{type:"element",value:"'"}))V+=1;return V-Z}function _(J,Z){let V=Z;while(V<J.length&&C(J[V]))V+=1;return J.substring(Z,V)}function p(J,Z){let V=1,$=Z;while(V>0){if($>=J.length)return-1;if(H(J[$],q))V+=1;else if(H(J[$],d))V-=1;$+=1}return $-1}function a(J,Z){let V=1,$=Z;while(V>0){if($>=J.length)return-1;if(H(J[$],B))V+=1;else if(H(J[$],k))V-=1;$+=1}return $-1}function r(J,Z){G(J[Z]==="{");let V=1,$=Z+1;while(V>0){if($>=J.length)throw new v("Unmatched curly brackets");if($+1<J.length&&["\\{","\\}"].includes(J.substring($,$+2))){$+=2;continue}if(J[$]==="{")V+=1;else if(J[$]==="}")V-=1;$+=1}return $-1}function g(J){const Z=[];let V=0;while(V<J.length){const $=J[V];let X;switch($){case"%":{let z=V+1;while(z<J.length&&J[z]!=="\n")z+=1;X={type:"comment",value:J.slice(V+1,z)},V=z;break}case"{":case"}":case"_":case"^":case"&":X={type:"control",value:$},V++;break;case"\n":X={type:"newline",value:$},V++;break;case"\r":{if(V+1<J.length&&J[V+1]==="\n")X={type:"newline",value:"\n"},V+=2;else X={type:"newline",value:"\n"},V++;break}case" ":{let z=V;while(z<J.length&&J[z]===" ")z+=1;X={type:"whitespace",value:J.slice(V,z)},V=z;break}case"\\":{if(V+1>=J.length)throw new v("Expecting command name after \\");const z=J.slice(V,V+2);if(["\\\\","\\,"].includes(z))X={type:"control",value:z};else if(["\\{","\\}","\\%","\\$","\\&","\\#","\\_"].includes(z))X={type:"element",value:z};else X={type:"command",value:"\\"+_(J,V+1)};V+=X.value.length;break}default:{if(R($)){let z=V;while(z<J.length&&R(J[z]))z+=1;X={type:"element",value:J.slice(V,z)}}else if(C($))X={type:"element",value:$};else if("+-*/=\'<>!.,;?()[]|".includes($))X={type:"element",value:$};else X={type:"unknown",value:$};V+=X.value.length}}if(Z.push(X),X.type==="command"&&["\\text","\\begin","\\end"].includes(X.value)){if(V>=J.length||J[V]!=="{")throw new v(`No content for ${X.value} command`);Z.push({type:"control",value:"{"});const z=r(J,V);V++;let j=J.slice(V,z);const Q=["{","}","\\","$","&","#","_","%"];for(let W of Q)j=j.replaceAll("\\"+W,W);Z.push({type:"text",value:j}),Z.push({type:"control",value:"}"}),V=z+1}}return Z}function H(J,Z){return J.type==Z.type&&J.value==Z.value}function b(J,Z){const V=new T,$=g(J);let X=[];for(let z of $)if(z.type==="command"&&Z[z.value]){const j=g(Z[z.value]);X=X.concat(j)}else X.push(z);return V.parse(X)}var E=["sqrt","text","bar","bold","boldsymbol","ddot","dot","hat","mathbb","mathbf","mathcal","mathfrak","mathit","mathrm","mathscr","mathsf","mathtt","operatorname","overbrace","overline","pmb","rm","tilde","underbrace","underline","vec","widehat","widetilde"],m=["frac","tfrac","binom","dbinom","dfrac","tbinom"],Y={type:"empty",content:""},D={type:"control",value:"{"},S={type:"control",value:"}"},M={type:"element",value:"["},i={type:"element",value:"]"},q={type:"command",value:"\\left"},d={type:"command",value:"\\right"},B={type:"command",value:"\\begin"},k={type:"command",value:"\\end"};class v extends Error{constructor(J){super(J);this.name="LatexParserError"}}var A={type:"control",value:"_"},I={type:"control",value:"^"};class T{space_sensitive;newline_sensitive;constructor(J=!1,Z=!0){this.space_sensitive=J,this.newline_sensitive=Z}parse(J){const Z=[];let V=0;while(V<J.length){const $=[];let X=0;while(X<J.length){const[z,j]=this.parseNextExpr(J,X);if(X=j,!this.space_sensitive&&z.type==="whitespace")continue;if(!this.newline_sensitive&&z.type==="newline")continue;if(z.type==="control"&&z.content==="&")throw new v("Unexpected & outside of an alignment");$.push(z)}if($.length===0)return Y;else if($.length===1)return $[0];else return{type:"ordgroup",content:"",args:$}}if(Z.length===0)return Y;else if(Z.length===1)return Z[0];else return{type:"ordgroup",content:"",args:Z}}parseNextExpr(J,Z){let[V,$]=this.parseNextExprWithoutSupSub(J,Z),X=null,z=null,j=0;if(j+=K(J,$),$+=j,$<J.length&&H(J[$],A)){if([X,$]=this.parseNextExprWithoutSupSub(J,$+1),j+=K(J,$),$+=j,$<J.length&&H(J[$],I)){if([z,$]=this.parseNextExprWithoutSupSub(J,$+1),K(J,$)>0)throw new v("Double superscript")}}else if($<J.length&&H(J[$],I)){if([z,$]=this.parseNextExprWithoutSupSub(J,$+1),K(J,$)>0)throw new v("Double superscript");if($<J.length&&H(J[$],A)){if([X,$]=this.parseNextExprWithoutSupSub(J,$+1),K(J,$)>0)throw new v("Double superscript")}}if(X!==null||z!==null||j>0){const Q={base:V};if(X)Q.sub=X;if(j>0){Q.sup={type:"ordgroup",content:"",args:[]};for(let W=0;W<j;W++)Q.sup.args.push({type:"symbol",content:"\\prime"});if(z)Q.sup.args.push(z);if(Q.sup.args.length===1)Q.sup=Q.sup.args[0]}else if(z)Q.sup=z;return[{type:"supsub",content:"",data:Q},$]}else return[V,$]}parseNextExprWithoutSupSub(J,Z){const V=J[Z],$=V.type;switch($){case"element":case"text":case"comment":case"whitespace":case"newline":return[{type:$,content:V.value},Z+1];case"command":if(H(V,B))return this.parseBeginEndExpr(J,Z);else if(H(V,q))return this.parseLeftRightExpr(J,Z);else return this.parseCommandExpr(J,Z);case"control":switch(V.value){case"{":const z=x(J,Z),j=J.slice(Z+1,z);return[this.parse(j),z+1];case"}":throw new v("Unmatched '}'");case"\\\\":return[{type:"control",content:"\\\\"},Z+1];case"\\,":return[{type:"control",content:"\\,"},Z+1];case"_":{let[Q,W]=this.parseNextExpr(J,Z+1),F=void 0;if(W<J.length&&H(J[W],I))[F,W]=this.parseNextExpr(J,W+1);return[{type:"supsub",content:"",data:{base:Y,sub:Q,sup:F}},W]}case"^":{let[Q,W]=this.parseNextExpr(J,Z+1),F=void 0;if(W<J.length&&H(J[W],A))[F,W]=this.parseNextExpr(J,W+1);return[{type:"supsub",content:"",data:{base:Y,sub:F,sup:Q}},W]}case"&":return[{type:"control",content:"&"},Z+1];default:throw new v("Unknown control sequence")}default:throw new v("Unknown token type")}}parseCommandExpr(J,Z){G(J[Z].type==="command");const V=J[Z].value;let $=Z+1;if(["left","right","begin","end"].includes(V.slice(1)))throw new v("Unexpected command: "+V);const X=c(V.slice(1));if(X===0)return[{type:"symbol",content:V},$];else if(X===1){if(V==="\\sqrt"&&$<J.length&&H(J[$],M)){const Q=$,W=l(J,$),F=J.slice(Q+1,W),U=this.parse(F),[f,u]=this.parseNextExprWithoutSupSub(J,W+1);return[{type:"unaryFunc",content:V,args:[f],data:U},u]}else if(V==="\\text"){if($+2>=J.length)throw new v("Expecting content for \\text command");return G(H(J[$],D)),G(J[$+1].type==="text"),G(H(J[$+2],S)),[{type:"text",content:J[$+1].value},$+3]}let[z,j]=this.parseNextExprWithoutSupSub(J,$);return[{type:"unaryFunc",content:V,args:[z]},j]}else if(X===2){const[z,j]=this.parseNextExprWithoutSupSub(J,$),[Q,W]=this.parseNextExprWithoutSupSub(J,j);return[{type:"binaryFunc",content:V,args:[z,Q]},W]}else throw new Error("Invalid number of parameters")}parseLeftRightExpr(J,Z){G(H(J[Z],q));let V=Z+1;if(V+=N(J,V).length,V>=J.length)throw new v("Expecting delimiter after \\left");const $=w(J,V);if($===null)throw new v("Invalid delimiter after \\left");V++;const X=V,z=p(J,V);if(z===-1)throw new v("No matching \\right");const j=z;if(V=z+1,V+=N(J,V).length,V>=J.length)throw new v("Expecting \\right after \\left");const Q=w(J,V);if(Q===null)throw new v("Invalid delimiter after \\right");V++;const W=J.slice(X,j),F=this.parse(W);return[{type:"leftright",content:"",args:[{type:"element",content:$.value},F,{type:"element",content:Q.value}]},V]}parseBeginEndExpr(J,Z){G(H(J[Z],B));let V=Z+1;G(H(J[V],D)),G(J[V+1].type==="text"),G(H(J[V+2],S));const $=J[V+1].value;V+=3,V+=N(J,V).length;const X=V,z=a(J,V);if(z===-1)throw new v("No matching \\end");const j=z;if(V=z+1,G(H(J[V],D)),G(J[V+1].type==="text"),G(H(J[V+2],S)),J[V+1].value!==$)throw new v("Mismatched \\begin and \\end environments");V+=3;const Q=J.slice(X,j);while(Q.length>0&&["whitespace","newline"].includes(Q[Q.length-1].type))Q.pop();const W=this.parseAligned(Q);return[{type:"beginend",content:$,data:W},V]}parseAligned(J){let Z=0;const V=[];let $=[];V.push($);let X={type:"ordgroup",content:"",args:[]};$.push(X);while(Z<J.length){const[z,j]=this.parseNextExpr(J,Z);if(Z=j,z.type==="whitespace")continue;else if(z.type==="newline"&&!this.newline_sensitive)continue;else if(z.type==="control"&&z.content==="\\\\")$=[],X={type:"ordgroup",content:"",args:[]},$.push(X),V.push($);else if(z.type==="control"&&z.content==="&")X={type:"ordgroup",content:"",args:[]},$.push(X);else X.args.push(z)}return V}}var y=new Map([["nonumber",""],["vec","arrow"],["neq","eq.not"],["dot","dot"],["ddot","dot.double"],["doteq","dot(eq)"],["dots","dots.h"],["ldots","dots.h"],["vdots","dots.v"],["ddots","dots.down"],["widehat","hat"],["widetilde","tilde"],["quad","quad"],["qquad","wide"],["overbrace","overbrace"],["underbrace","underbrace"],["overline","overline"],["underline","underline"],["bar","macron"],["dbinom","binom"],["tbinom","binom"],["dfrac","frac"],["tfrac","frac"],["boldsymbol","bold"],["mathbb","bb"],["mathbf","bold"],["mathcal","cal"],["mathit","italic"],["mathfrak","frak"],["mathrm","upright"],["mathsf","sans"],["mathtt","mono"],["rm","upright"],["pmb","bold"],["pm","plus.minus"],["mp","minus.plus"],["oplus","xor"],["boxplus","plus.square"],["otimes","times.circle"],["boxtimes","times.square"],["sim","tilde"],["approx","approx"],["cong","tilde.equiv"],["simeq","tilde.eq"],["asymp","\u224D"],["equiv","equiv"],["propto","prop"],["lfloor","\u230A"],["rfloor","\u230B"],["lceil","\u2308"],["rceil","\u2309"],["gets","arrow.l"],["hookleftarrow","arrow.l.hook"],["leftharpoonup","harpoon.lt"],["leftharpoondown","harpoon.lb"],["rightleftharpoons","harpoons.rtlb"],["longleftarrow","arrow.l.long"],["longrightarrow","arrow.r.long"],["longleftrightarrow","arrow.l.r.long"],["Longleftarrow","arrow.l.double.long"],["Longrightarrow","arrow.r.double.long"],["Longleftrightarrow","arrow.l.r.double.long"],["longmapsto","arrow.r.bar"],["hookrightarrow","arrow.r.hook"],["rightharpoonup","harpoon.rt"],["rightharpoondown","harpoon.rb"],["iff","arrow.l.r.double.long"],["implies","arrow.r.double.long"],["uparrow","arrow.t"],["downarrow","arrow.b"],["updownarrow","arrow.t.b"],["Uparrow","arrow.t.double"],["Downarrow","arrow.b.double"],["Updownarrow","arrow.t.b.double"],["nearrow","arrow.tr"],["searrow","arrow.br"],["swarrow","arrow.bl"],["nwarrow","arrow.tl"],["leadsto","arrow.squiggly"],["leftleftarrows","arrows.ll"],["rightrightarrows","arrows.rr"],["Cap","sect.double"],["Cup","union.double"],["Delta","Delta"],["Gamma","Gamma"],["Join","join"],["Lambda","Lambda"],["Leftarrow","arrow.l.double"],["Leftrightarrow","arrow.l.r.double"],["Longrightarrow","arrow.r.double.long"],["Omega","Omega"],["P","pilcrow"],["Phi","Phi"],["Pi","Pi"],["Psi","Psi"],["Rightarrow","arrow.r.double"],["S","section"],["Sigma","Sigma"],["Theta","Theta"],["aleph","alef"],["alpha","alpha"],["angle","angle"],["approx","approx"],["approxeq","approx.eq"],["ast","ast"],["beta","beta"],["bigcap","sect.big"],["bigcirc","circle.big"],["bigcup","union.big"],["bigodot","dot.circle.big"],["bigoplus","xor.big"],["bigotimes","times.circle.big"],["bigsqcup","union.sq.big"],["bigtriangledown","triangle.b"],["bigtriangleup","triangle.t"],["biguplus","union.plus.big"],["bigvee","or.big"],["bigwedge","and.big"],["bullet","bullet"],["cap","sect"],["cdot","dot.op"],["cdots","dots.c"],["checkmark","checkmark"],["chi","chi"],["circ","circle.small"],["colon","colon"],["cong","tilde.equiv"],["coprod","product.co"],["copyright","copyright"],["cup","union"],["curlyvee","or.curly"],["curlywedge","and.curly"],["dagger","dagger"],["dashv","tack.l"],["ddagger","dagger.double"],["delta","delta"],["ddots","dots.down"],["diamond","diamond"],["div","div"],["divideontimes","times.div"],["dotplus","plus.dot"],["downarrow","arrow.b"],["ell","ell"],["emptyset","nothing"],["epsilon","epsilon.alt"],["equiv","equiv"],["eta","eta"],["exists","exists"],["forall","forall"],["gamma","gamma"],["ge","gt.eq"],["geq","gt.eq"],["geqslant","gt.eq.slant"],["gg","gt.double"],["hbar","planck.reduce"],["imath","dotless.i"],["iiiint","intgral.quad"],["iiint","integral.triple"],["iint","integral.double"],["in","in"],["infty","infinity"],["int","integral"],["intercal","top"],["iota","iota"],["jmath","dotless.j"],["kappa","kappa"],["lambda","lambda"],["land","and"],["langle","angle.l"],["lbrace","brace.l"],["lbrack","bracket.l"],["ldots","dots.l"],["le","lt.eq"],["leadsto","arrow.squiggly"],["leftarrow","arrow.l"],["leftthreetimes","times.three.l"],["leftrightarrow","arrow.l.r"],["leq","lt.eq"],["leqslant","lt.eq.slant"],["lhd","triangle.l"],["ll","lt.double"],["longmapsto","arrow.bar.long"],["longrightarrow","arrow.long"],["lor","or"],["ltimes","times.l"],["mapsto","arrow.bar"],["measuredangle","angle.arc"],["mid","divides"],["models","models"],["mp","minus.plus"],["mu","mu"],["nRightarrow","arrow.double.not"],["nabla","nabla"],["ncong","tilde.nequiv"],["ne","eq.not"],["neg","not"],["neq","eq.not"],["nexists","exists.not"],["ni","in.rev"],["nleftarrow","arrow.l.not"],["nleq","lt.eq.not"],["nparallel","parallel.not"],["ngeq","gt.eq.not"],["nmid","divides.not"],["notin","in.not"],["nrightarrow","arrow.not"],["nsim","tilde.not"],["nsubseteq","subset.eq.not"],["nu","nu"],["ntriangleleft","lt.tri.not"],["ntriangleright","gt.tri.not"],["nwarrow","arrow.tl"],["odot","dot.circle"],["oint","integral.cont"],["oiint","integral.surf"],["oiiint","integral.vol"],["omega","omega"],["ominus","minus.circle"],["oplus","xor"],["otimes","times.circle"],["parallel","parallel"],["partial","diff"],["perp","perp"],["phi","phi.alt"],["pi","pi"],["pm","plus.minus"],["pounds","pound"],["prec","prec"],["preceq","prec.eq"],["prime","prime"],["prod","product"],["propto","prop"],["psi","psi"],["rangle","angle.r"],["rbrace","brace.r"],["rbrack","bracket.r"],["rhd","triangle"],["rho","rho"],["rightarrow","arrow.r"],["rightthreetimes","times.three.r"],["rtimes","times.r"],["setminus","without"],["sigma","sigma"],["sim","tilde"],["simeq","tilde.eq"],["slash","slash"],["smallsetminus","without"],["spadesuit","suit.spade"],["sqcap","sect.sq"],["sqcup","union.sq"],["sqsubseteq","subset.eq.sq"],["sqsupseteq","supset.eq.sq"],["star","star"],["subset","subset"],["subseteq","subset.eq"],["subsetneq","subset.neq"],["succ","succ"],["succeq","succ.eq"],["sum","sum"],["supset","supset"],["supseteq","supset.eq"],["supsetneq","supset.neq"],["swarrow","arrow.bl"],["tau","tau"],["theta","theta"],["times","times"],["to","arrow.r"],["top","top"],["triangle","triangle.t"],["triangledown","triangle.b.small"],["triangleleft","triangle.l.small"],["triangleright","triangle.r.small"],["twoheadrightarrow","arrow.r.twohead"],["uparrow","arrow.t"],["updownarrow","arrow.t.b"],["upharpoonright","harpoon.tr"],["uplus","union.plus"],["upsilon","upsilon"],["varepsilon","epsilon"],["varnothing","diameter"],["varphi","phi"],["varpi","pi.alt"],["varrho","rho.alt"],["varsigma","sigma.alt"],["vartheta","theta.alt"],["vdash","tack.r"],["vdots","dots.v"],["vee","or"],["wedge","and"],["wr","wreath"],["xi","xi"],["yen","yen"],["zeta","zeta"],["mathscr","scr"],["LaTeX","#LaTeX"],["TeX","#TeX"]]);function L(J){if(/^[a-zA-Z0-9]$/.test(J))return J;else if(J==="\\\\")return"\\";else if(J=="/")return"\\/";else if(["\\$","\\#","\\&","\\_"].includes(J))return J;else if(J.startsWith("\\")){const Z=J.slice(1);if(y.has(Z))return y.get(Z);else return Z}return J}var n=["dim","id","im","mod","Pr","sech","csch"];class O extends Error{node;constructor(J,Z){super(J);this.name="TypstWriterError",this.node=Z}}class h{nonStrict;preferTypstIntrinsic;buffer="";queue=[];needSpaceAfterSingleItemScript=!1;insideFunctionDepth=0;constructor(J,Z){this.nonStrict=J,this.preferTypstIntrinsic=Z}writeBuffer(J){if(this.needSpaceAfterSingleItemScript&&/^[0-9a-zA-Z\(]/.test(J))this.buffer+=" ";else{let Z=!1;if(Z||=/[\(\|]$/.test(this.buffer)&&/^\w/.test(J),Z||=/^[}()_^,;!\|]$/.test(J),Z||=J==="'",Z||=/[0-9]$/.test(this.buffer)&&/^[0-9]/.test(J),Z||=/[\(\[{]\s*(-|\+)$/.test(this.buffer)||this.buffer==="-"||this.buffer==="+",Z||=J.startsWith("\n"),Z||=this.buffer==="",Z||=/[\s"_^{\(]$/.test(this.buffer),!Z)this.buffer+=" "}if(this.needSpaceAfterSingleItemScript)this.needSpaceAfterSingleItemScript=!1;this.buffer+=J}append(J){if(J.type==="empty"||J.type==="whitespace")return;else if(J.type==="ordgroup")J.args.forEach((Z)=>this.append(Z));else if(J.type==="element"){let Z=J.content;if(J.content===","&&this.insideFunctionDepth>0)Z="comma";this.queue.push({type:"symbol",content:Z})}else if(J.type==="symbol")this.queue.push({type:"symbol",content:J.content});else if(J.type==="text")this.queue.push(J);else if(J.type==="supsub"){let{base:Z,sup:V,sub:$}=J.data;if(Z&&Z.type==="unaryFunc"&&Z.content==="\\overbrace"&&V){this.append({type:"binaryFunc",content:"\\overbrace",args:[Z.args[0],V]});return}else if(Z&&Z.type==="unaryFunc"&&Z.content==="\\underbrace"&&$){this.append({type:"binaryFunc",content:"\\underbrace",args:[Z.args[0],$]});return}if(Z.type==="empty")this.queue.push({type:"text",content:""});else this.appendWithBracketsIfNeeded(Z);let X=!1;const z=V&&V.type==="symbol"&&V.content==="\\prime";if(z)this.queue.push({type:"atom",content:"\'"}),X=!1;if($)this.queue.push({type:"atom",content:"_"}),X=this.appendWithBracketsIfNeeded($);if(V&&!z)this.queue.push({type:"atom",content:"^"}),X=this.appendWithBracketsIfNeeded(V);if(X)this.queue.push({type:"softSpace",content:""})}else if(J.type==="leftright"){const[Z,V,$]=J.args;if(["[]","()","\\{\\}","\\lfloor\\rfloor","\\lceil\\rceil"].includes(Z.content+$.content)){this.append(Z),this.append(V),this.append($);return}const X={type:"symbol",content:"lr"};this.queue.push(X),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(Z),this.append(V),this.append($),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--}else if(J.type==="binaryFunc"){const Z={type:"symbol",content:J.content},[V,$]=J.args;this.queue.push(Z),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(V),this.queue.push({type:"atom",content:","}),this.append($),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--}else if(J.type==="unaryFunc"){const Z={type:"symbol",content:J.content},V=J.args[0];if(J.content==="\\sqrt"&&J.data){Z.content="root",this.queue.push(Z),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(J.data),this.queue.push({type:"atom",content:","}),this.append(V),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--;return}else if(J.content==="\\mathbf"){this.append({type:"symbol",content:"upright"}),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.queue.push(Z),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(V),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--,this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--;return}else if(J.content==="\\mathbb"){const $=J.args[0];if($.type==="element"&&/^[A-Z]$/.test($.content)){this.queue.push({type:"symbol",content:$.content+$.content});return}}else if(J.content==="\\operatorname"){let $=J.args;if($.length===1&&$[0].type=="ordgroup")$=$[0].args;const X=$.reduce((z,j)=>{return z+=L(j.content),z},"");if(this.preferTypstIntrinsic&&n.includes(X))this.queue.push({type:"symbol",content:X});else this.queue.push({type:"symbol",content:"op"}),this.queue.push({type:"atom",content:"("}),this.queue.push({type:"text",content:X}),this.queue.push({type:"atom",content:")"});return}this.queue.push(Z),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.append(V),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--}else if(J.type==="newline"){this.queue.push({type:"newline",content:"\n"});return}else if(J.type==="beginend")if(J.content.startsWith("align")){const Z=J.data;Z.forEach((V,$)=>{if(V.forEach((X,z)=>{if(z>0)this.queue.push({type:"atom",content:"&"});this.append(X)}),$<Z.length-1)this.queue.push({type:"symbol",content:"\\\\"})})}else{const Z=J.data;this.queue.push({type:"symbol",content:"mat"}),this.insideFunctionDepth++,this.queue.push({type:"atom",content:"("}),this.queue.push({type:"symbol",content:"delim: #none, "}),Z.forEach((V,$)=>{V.forEach((X,z)=>{if(X.type==="ordgroup"&&X.args.length===0){this.queue.push({type:"atom",content:","});return}if(this.append(X),z<V.length-1)this.queue.push({type:"atom",content:","});else if($<Z.length-1)this.queue.push({type:"atom",content:";"})})}),this.queue.push({type:"atom",content:")"}),this.insideFunctionDepth--}else if(J.type==="matrix");else if(J.type==="unknownMacro")if(this.nonStrict)this.queue.push({type:"symbol",content:J.content});else throw new O(`Unknown macro: ${J.content}`,J);else if(J.type==="control")if(J.content==="\\\\")this.queue.push({type:"symbol",content:J.content});else if(J.content==="\\,")this.queue.push({type:"symbol",content:"thin"});else throw new O(`Unknown control sequence: ${J.content}`,J);else if(J.type==="comment")this.queue.push({type:"comment",content:J.content});else throw new O(`Unimplemented node type to append: ${J.type}`,J)}flushQueue(){this.queue.forEach((J)=>{let Z="";switch(J.type){case"atom":Z=J.content;break;case"symbol":Z=L(J.content);break;case"text":Z=`"${J.content}"`;break;case"softSpace":this.needSpaceAfterSingleItemScript=!0,Z="";break;case"comment":Z=`//${J.content}`;break;case"newline":Z="\n";break;default:throw new O(`Unexpected node type to stringify: ${J.type}`,J)}if(Z!=="")this.writeBuffer(Z)}),this.queue=[]}appendWithBracketsIfNeeded(J){const Z=["symbol","element","unaryFunc","binaryFunc","leftright"].includes(J.type);if(Z)this.append(J);else this.queue.push({type:"atom",content:"("}),this.append(J),this.queue.push({type:"atom",content:")"});return Z}finalize(){this.flushQueue();const J=function(V){let $=V.replace(/⌊\s*(.*?)\s*⌋/g,"floor($1)");return $=$.replace(/floor\(\)/g,'floor("")'),$},Z=function(V){let $=V.replace(/⌈\s*(.*?)\s*⌉/g,"ceil($1)");return $=$.replace(/ceil\(\)/g,'ceil("")'),$};return this.buffer=J(this.buffer),this.buffer=Z(this.buffer),this.buffer}}function P(J,Z){const V={nonStrict:!1,preferTypstIntrinsic:!0,customTexMacros:{}};if(Z){if(Z.nonStrict)V.nonStrict=Z.nonStrict;if(Z.preferTypstIntrinsic)V.preferTypstIntrinsic=Z.preferTypstIntrinsic;if(Z.customTexMacros)V.customTexMacros=Z.customTexMacros}const $=b(J,V.customTexMacros),X=new h(V.nonStrict,V.preferTypstIntrinsic);return X.append($),X.finalize()}if(typeof window!=="undefined")window.tex2typst=P; |
{ | ||
"name": "tex2typst", | ||
"version": "0.2.3", | ||
"version": "0.2.6", | ||
"description": "JavaScript library for converting TeX code to Typst", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -8,6 +8,2 @@ import { TexNode, TexSupsubData } from "./types"; | ||
'arccos', | ||
'arcsin', | ||
'arctan', | ||
'arg', | ||
'bar', | ||
@@ -17,19 +13,13 @@ 'bold', | ||
'ddot', | ||
'det', | ||
'dim', | ||
'dot', | ||
'exp', | ||
'gcd', | ||
'hat', | ||
'ker', | ||
'mathbb', | ||
'mathbf', | ||
'mathcal', | ||
'mathfrak', | ||
'mathit', | ||
'mathrm', | ||
'mathscr', | ||
'mathsf', | ||
'mathtt', | ||
'mathrm', | ||
'max', | ||
'min', | ||
'mod', | ||
'operatorname', | ||
@@ -39,3 +29,2 @@ 'overbrace', | ||
'pmb', | ||
'sup', | ||
'rm', | ||
@@ -301,13 +290,11 @@ 'tilde', | ||
const firstTwoChars = latex.slice(pos, pos + 2); | ||
if (firstTwoChars === '\\\\') { | ||
token = { type: 'control', value: '\\\\' }; | ||
pos += 2; | ||
if (['\\\\', '\\,'].includes(firstTwoChars)) { | ||
token = { type: 'control', value: firstTwoChars }; | ||
} else if (['\\{','\\}', '\\%', '\\$', '\\&', '\\#', '\\_'].includes(firstTwoChars)) { | ||
token = { type: 'element', value: firstTwoChars }; | ||
pos += 2; | ||
} else { | ||
const command = eat_command_name(latex, pos + 1); | ||
token = { type: 'command', value: '\\' + command}; | ||
pos += 1 + command.length; | ||
} | ||
pos += token.value.length; | ||
break; | ||
@@ -509,2 +496,4 @@ } | ||
return [{ type: 'control', content: '\\\\' }, start + 1]; | ||
case '\\,': | ||
return [{ type: 'control', content: '\\,' }, start + 1]; | ||
case '_': { | ||
@@ -511,0 +500,0 @@ let [sub, pos] = this.parseNextExpr(tokens, start + 1); |
@@ -286,2 +286,4 @@ import { symbolMap } from "./map"; | ||
this.queue.push({ type: 'symbol', content: node.content }); | ||
} else if (node.content === '\\,') { | ||
this.queue.push({ type: 'symbol', content: 'thin' }); | ||
} else { | ||
@@ -288,0 +290,0 @@ throw new TypstWriterError(`Unknown control sequence: ${node.content}`, node); |
1061370
30222