Skip to content

Commit 6810f2c

Browse files
authored
render: support Pandoc's --math-method option
Pandoc 3.11 added --math-method=METHOD[:URL] and deprecated --katex, --mathjax, --mathml, --webtex and --gladtex, whose deprecation warning now points users to the new option. Pandoc honours it, but Quarto only looked at the old flags to learn which math method the command line chose, so with --math-method it kept acting on html-math-method (MathJax by default for Bootstrap HTML): - with --embed-resources, the template patch swapped Pandoc's $math$ for Quarto's MathJax loader while Pandoc's KaTeX render call stayed, so the KaTeX library was never embedded and math showed as raw TeX; - the KaTeX post-processor (the require.js define guard) didn't run; - llms.md output lost the math delimiters and doubled backslashes. parseRenderFlags now records the method and optional URL, the HTML math resolver returns it as a command line choice, and the template patch asks the resolver instead of checking the five legacy flags. The rest of mathConfig is left as is, so html-math-method behaves as before. The argument still goes to Pandoc unchanged. Output with --math-method=X is now byte-identical to output with the matching legacy --X flag.
1 parent 84c4916 commit 6810f2c

10 files changed

Lines changed: 116 additions & 7 deletions

File tree

‎news/changelog-1.12.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ All changes included in 1.12:
2626
- ([#9606](https://github.com/quarto-dev/quarto-cli/issues/9606)): Fix `format-links` entries written as `{format, text}` adding a second, default link for the same format instead of renaming its link.
2727
- ([#15269](https://github.com/quarto-dev/quarto-cli/pull/15269)): Fix `link-external-newwindow` overriding a `target` set explicitly on a link. (author: @mcanouil)
2828
- ([#15297](https://github.com/quarto-dev/quarto-cli/issues/15297)): Fix the table of contents highlight not following the scroll position when a heading id contains a `%` or a `:`. (author: @chenyangsu)
29+
- ([#PRNUMBER](https://github.com/quarto-dev/quarto-cli/pull/PRNUMBER)): Support Pandoc's `--math-method` option in `quarto render`, so that `--math-method=katex` works with `--embed-resources` and keeps math intact in `llms.md` output, like `--katex` does.
2930

3031
### `pdf`
3132

‎src/command/render/flags.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
kDate,
1515
kEmbedResources,
1616
kListings,
17+
kMathMethod,
1718
kNumberOffset,
1819
kNumberSections,
1920
kReferenceLocation,
@@ -204,6 +205,18 @@ export async function parseRenderFlags(args: string[]) {
204205
arg = argsStack.shift();
205206
break;
206207

208+
case `--${kMathMethod}`: {
209+
// METHOD[:URL]; the URL itself contains ':', so split on the first one
210+
arg = argsStack.shift();
211+
if (arg) {
212+
const colon = arg.indexOf(":");
213+
flags[kMathMethod] = colon === -1
214+
? { method: arg }
215+
: { method: arg.slice(0, colon), url: arg.slice(colon + 1) };
216+
}
217+
break;
218+
}
219+
207220
case "--execute":
208221
flags.execute = true;
209222
arg = argsStack.shift();

‎src/command/render/template.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { normalizePath } from "../../core/path.ts";
2626
import { isGlob } from "../../core/lib/glob.ts";
2727
import { ProjectContext } from "../../project/types.ts";
2828
import { isWindows } from "../../deno_ral/platform.ts";
29+
import { resolveHtmlMathMethod } from "../../format/html/format-html-math.ts";
2930

3031
export const kPatchedTemplateExt = ".patched";
3132
export const kTemplatePartials = "template-partials";
@@ -214,11 +215,8 @@ function applyTemplatePatches(
214215
}
215216

216217
function mathConfig(format: Format, flags?: RenderFlags) {
217-
// if any command line math flags were passed then bail
218-
if (
219-
flags?.mathjax || flags?.katex || flags?.webtex || flags?.gladtex ||
220-
flags?.mathml
221-
) {
218+
// if a command line math flag chose the method, Pandoc handles it
219+
if (resolveHtmlMathMethod(format, flags)?.fromFlag) {
222220
return undefined;
223221
}
224222

‎src/config/constants.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ export const kKatex = "katex";
609609
export const kMathml = "mathml";
610610
export const kGladtex = "gladtex";
611611
export const kWebtex = "webtex";
612+
export const kMathMethod = "math-method";
612613

613614
// metadata fields
614615
export const kQuartoInternal = "quarto-internal";

‎src/config/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ import {
154154
kManuscriptMecaBundle,
155155
kMarkdownHeadings,
156156
kMathjax,
157+
kMathMethod,
157158
kMathml,
158159
kMergeIncludes,
159160
kMermaidFormat,
@@ -645,6 +646,7 @@ export interface PandocFlags {
645646
[kMathml]?: boolean;
646647
[kGladtex]?: boolean;
647648
[kWebtex]?: boolean;
649+
[kMathMethod]?: { method: string; url?: string };
648650
}
649651

650652
// the requested pdf engine, it's options, and the bib engine

‎src/format/html/format-html-math.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
import { kHtmlEmptyPostProcessResult } from "../../command/render/constants.ts";
99
import { Document } from "../../core/deno-dom.ts";
10-
import { kHtmlMathMethod } from "../../config/constants.ts";
10+
import { kHtmlMathMethod, kMathMethod } from "../../config/constants.ts";
1111
import { Format, PandocFlags } from "../../config/types.ts";
1212
import { MathMethods } from "../../resources/types/schema-types.ts";
1313

1414
export interface HtmlMathMethod {
1515
method: MathMethods;
1616
url?: string;
17-
// true when a command line flag (--katex, --mathjax, ...) chose the method
17+
// true when a command line flag (--math-method, --katex, ...) chose the method
1818
fromFlag: boolean;
1919
}
2020

@@ -29,6 +29,14 @@ export function resolveHtmlMathMethod(
2929
format: Format,
3030
flags?: PandocFlags,
3131
): HtmlMathMethod | undefined {
32+
const mathMethod = flags?.[kMathMethod];
33+
if (mathMethod) {
34+
return {
35+
method: mathMethod.method as MathMethods,
36+
url: mathMethod.url,
37+
fromFlag: true,
38+
};
39+
}
3240
const flagMethod = kMathFlags.find((method) => flags?.[method]);
3341
if (flagMethod) {
3442
return { method: flagMethod, fromFlag: true };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: "Math method chosen with the --math-method command line option"
3+
format: html
4+
---
5+
6+
Inline math $x^2$.
7+
8+
$$
9+
E = mc^2
10+
$$ {#eq-energy}
11+
12+
See @eq-energy.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* render-math-method-flag.test.ts
3+
*
4+
* Pandoc 3.11 replaced --katex, --mathjax, ... with --math-method=METHOD[:URL].
5+
* Quarto must see the method chosen that way, as it does for the older flags.
6+
*
7+
* Copyright (C) 2026 Posit Software, PBC
8+
*/
9+
10+
import { docs, outputForInput } from "../../utils.ts";
11+
import { ensureFileRegexMatches, noErrorsOrWarnings } from "../../verify.ts";
12+
import { testRender } from "./render.ts";
13+
14+
const input = docs("render/math-method-flag.qmd");
15+
const output = outputForInput(input, "html");
16+
17+
// With embed-resources, Pandoc embeds the KaTeX library itself (its error
18+
// strings are in the page); Quarto must not swap in its MathJax loader.
19+
testRender(input, "html", true, [
20+
noErrorsOrWarnings,
21+
ensureFileRegexMatches(output.outputPath, [/KaTeX parse error/], [
22+
/mathjax@3\/es5\/tex-chtml-full\.js/,
23+
]),
24+
], {}, ["--math-method=katex", "--embed-resources"]);
25+
26+
// Without embed-resources, Quarto's KaTeX post-processor runs
27+
testRender(input, "html", false, [
28+
noErrorsOrWarnings,
29+
ensureFileRegexMatches(output.outputPath, [
30+
/window\.backupDefine = window\.define/,
31+
]),
32+
], {}, ["--math-method", "katex"]);

‎tests/unit/format-html-math.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,20 @@ async () => {
6161
assertEquals(resolveHtmlMathMethod(createMockFormat()), undefined);
6262
assertEquals(resolveHtmlMathMethod(createMockFormat(), {}), undefined);
6363
});
64+
65+
unitTest("resolveHtmlMathMethod - --math-method flag", //deno-lint-ignore require-await
66+
async () => {
67+
const format = createMockFormat({
68+
pandoc: { "html-math-method": "mathjax" },
69+
});
70+
assertEquals(
71+
resolveHtmlMathMethod(format, {
72+
"math-method": { method: "katex", url: "https://example.com/katex/" },
73+
}),
74+
{ method: "katex", url: "https://example.com/katex/", fromFlag: true },
75+
);
76+
assertEquals(
77+
resolveHtmlMathMethod(format, { "math-method": { method: "plain" } }),
78+
{ method: "plain", url: undefined, fromFlag: true },
79+
);
80+
});

‎tests/unit/render-flags.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* render-flags.test.ts
3+
*
4+
* Copyright (C) 2026 Posit Software, PBC
5+
*/
6+
7+
import { assertEquals } from "../asserts.ts";
8+
import { parseRenderFlags } from "../../src/command/render/flags.ts";
9+
import { unitTest } from "../test.ts";
10+
11+
unitTest("parseRenderFlags - --math-method=METHOD:URL", async () => {
12+
const flags = await parseRenderFlags([
13+
"--math-method=katex:https://example.com/katex/",
14+
]);
15+
assertEquals(flags["math-method"], {
16+
method: "katex",
17+
url: "https://example.com/katex/",
18+
});
19+
});
20+
21+
unitTest("parseRenderFlags - --math-method METHOD", async () => {
22+
const flags = await parseRenderFlags(["--math-method", "katex", "--toc"]);
23+
assertEquals(flags["math-method"], { method: "katex" });
24+
assertEquals(flags.toc, true);
25+
});

0 commit comments

Comments
 (0)