Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/latex-envs.ts
Views: 687
/*1Limited support for some latex environments, only for rendering (not editing).2*/34export default function latexEnvs(value: string): string {5value = transformFigures(value);6value = transformItemEnvironments(value);7return value;8}910/*11transformFigures -- dumb parser to turn this:1213---1415...1617\begin{figure}18\centering19\centerline{\includegraphics[width=WIDTH]{URL}}20\caption{\label{foo}CAPTION}21\end{figure}2223...2425---2627into this:2829---3031...3233<div style="text-align:center"><img src="URL" style="width:WIDTH"/><br/><br/>\figlabel{foo}CAPTION</div>343536...3738---3940There can be lots of figures.41*/4243function transformFigures(content: string): string {44while (true) {45const i = content.indexOf("\\begin{figure}");46if (i == -1) {47return content;48}49const j = content.indexOf("\\end{figure}");50if (j == -1) {51return content;52}53const k = content.indexOf("\\includegraphics");54if (k == -1) {55return content;56}57const c = content.indexOf("\\caption{");58if (c == -1) {59return content;60}61const c2 = content.lastIndexOf("}", j);62if (c2 == -1) {63return content;64}6566const w = content.indexOf("width=");67const w2 = content.indexOf("{", k);68const w3 = content.indexOf("}", k);69if (w2 == -1 || w3 == -1) {70return content;71}72let style = "";73if (w != -1) {74style = `width:${content.slice(w + "width=".length, w2 - 1)}`;75}76const url = content.slice(w2 + 1, w3);77let caption = content.slice(c + "\\caption{".length, c2);78const x = caption.indexOf("\\label{");79let figlabel;80if (x != -1) {81const y = caption.indexOf("}", x);82figlabel = `\\figlabel{${caption.slice(x + "\\label{".length, y)}}`;83caption = caption.slice(0, x) + caption.slice(y + 1);84} else {85figlabel = "";86}8788const md = `\n\n<div style="text-align:center;margin:20px auto;max-width:750px"><img src="${url}" style="${style}"/><br/><br/><b>Figure${figlabel}:</b> ${caption}</div>\n\n`;89content =90content.slice(0, i) + md + content.slice(j + "\\end{figure}".length);91}92}9394/*95transformEnumerate -- dumb parser to turn this:9697---9899...100101\begin{enumerate}102\item ITEM1103\item ITEM2104...105\end{enumerate}106107...108109---110111into this:112113---114115...1161171. ITEM11181191. ITEM2120121...122123---124125and126127---128129\begin{itemize}130\item ITEM1131\item ITEM2132...133\end{itemize}134135into136137- ITEM1138139- ITEM2140141*/142143function transformItemEnvironments(content: string) {144for (const type of ["itemize", "enumerate"]) {145content = transformItemsType(content, type as "itemize" | "enumerate");146}147return content;148}149150function transformItemsType(151content: string,152type: "itemize" | "enumerate",153): string {154while (true) {155const BEGIN = `\\begin{${type}}`;156const i = content.indexOf(BEGIN);157if (i == -1) {158return content;159}160const END = `\\end{${type}}`;161const j = content.indexOf(END);162if (j == -1) {163return content;164}165166const body = content.slice(i + BEGIN.length + 1, j);167const items = body168.split("\\item")169.filter((x) => x.trim())170.map((x) => (type == "itemize" ? "- " : "1. ") + x)171.join("\n\n");172content =173content.slice(0, i) +174"\n\n" +175items +176"\n\n" +177content.slice(j + END.length + 1);178}179return content;180}181182183