From ef2cd8c306969c4e92c54f83d88dc9577255e03d Mon Sep 17 00:00:00 2001 From: Oscar Levin Date: Mon, 10 Feb 2025 17:46:44 -0700 Subject: [PATCH] Support `\ref` and related commands (#131) * support \ref and related xrefs; plus \latex, etc. --- .../libs/pre-conversion-subs/macro-subs.ts | 54 +++++++++++++++++++ .../tests/unified-latex-to-pretext.test.ts | 27 ++++++++++ 2 files changed, 81 insertions(+) diff --git a/packages/unified-latex-to-pretext/libs/pre-conversion-subs/macro-subs.ts b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/macro-subs.ts index 0d53f85..b0bf7b9 100644 --- a/packages/unified-latex-to-pretext/libs/pre-conversion-subs/macro-subs.ts +++ b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/macro-subs.ts @@ -128,6 +128,47 @@ export const macroReplacements: Record< content: args[1] || [], }); }, + ref: (node) => { + const args = getArgsContent(node); + const ref = printRaw(args[1] || ""); + return htmlLike({ + tag: "xref", + attributes: { + ref: ref || "", + text: "global", + }, + }); + }, + cref: (node) => { + const args = getArgsContent(node); + const ref = printRaw(args[1] || ""); + return htmlLike({ + tag: "xref", + attributes: { + ref: ref || "", + }, + }); + }, + Cref: (node) => { + const args = getArgsContent(node); + const ref = printRaw(args[1] || ""); + return htmlLike({ + tag: "xref", + attributes: { + ref: ref || "", + }, + }); + }, + cite: (node) => { + const args = getArgsContent(node); + const ref = printRaw(args[1] || ""); + return htmlLike({ + tag: "xref", + attributes: { + ref: ref || "", + }, + }); + }, "\\": emptyStringWithWarningFactory( `Warning: There is no equivalent tag for \"\\\", an empty Ast.String was used as a replacement.` ), @@ -150,6 +191,19 @@ export const macroReplacements: Record< noindent: emptyStringWithWarningFactory( `Warning: There is no equivalent tag for \"noindent\", an empty Ast.String was used as a replacement.` ), + latex: (node) => { + return htmlLike({tag: "latex"}) + }, + latexe: (node) => { + return htmlLike({tag: "latex"}) + }, + today: (node) => { + return htmlLike({tag: "today"}) + }, + tex: (node) => { + return htmlLike({tag: "tex"}) + }, + //tex: factory("tex"), includegraphics: (node) => { const args = getArgsContent(node); const source = printRaw(args[args.length - 1] || []); diff --git a/packages/unified-latex-to-pretext/tests/unified-latex-to-pretext.test.ts b/packages/unified-latex-to-pretext/tests/unified-latex-to-pretext.test.ts index db3b6db..470a786 100644 --- a/packages/unified-latex-to-pretext/tests/unified-latex-to-pretext.test.ts +++ b/packages/unified-latex-to-pretext/tests/unified-latex-to-pretext.test.ts @@ -387,4 +387,31 @@ describe("unified-latex-to-pretext:unified-latex-to-pretext", () => { await normalizeHtml(`My remark

a

`) ); }); + it("Replaces \\ref with a xref", async () => { + html = process(`Exercise \\ref{foo} is important`); + expect(await normalizeHtml(html)).toEqual( + await normalizeHtml(`Exercise is important`) + ); + }); + it("Replaces \\cref and \\Cref with a bare xref", async () => { + html = process(`As we saw in \\cref{foo}, we can do this.`); + expect(await normalizeHtml(html)).toEqual( + await normalizeHtml(`As we saw in , we can do this.`) + ); + + html = process(`As we saw in \\Cref{foo}, we can do this.`); + expect(await normalizeHtml(html)).toEqual( + await normalizeHtml(`As we saw in , we can do this.`) + ); + }); + it("Replaces \\cite with a xref", async () => { + html = process(`See \\cite{foo} for more`); + expect(await normalizeHtml(html)).toEqual( + await normalizeHtml(`See for more`) + ); + }); + it("Replaces \\latex with etc.", async () => { + html = process(`We can write in \\latex or \\tex and do so \\today.`); + expect(await normalizeHtml(html)).toEqual(await normalizeHtml(`We can write in or and do so .`)); + }) });