Skip to content

Commit c444133

Browse files
authored
Honor Pandoc's --math-method option in quarto render (#15321)
Quarto read only the legacy math flags (--katex, --mathjax, --mathml, --webtex, --gladtex) to learn which math method the command line chose, so --math-method=METHOD[:URL] was ignored and html-math-method applied instead. With --embed-resources this left Quarto's MathJax loader in place while Pandoc still emitted the KaTeX render call, so KaTeX was never embedded and math showed as raw TeX; llms.md output also lost its math delimiters. The flag is now parsed and treated as a command line choice, as the legacy flags are. It still goes to Pandoc unchanged, and html-math-method is unaffected when the flag is absent. - Parse --math-method in parseRenderFlags, splitting METHOD and URL. - Return it from resolveHtmlMathMethod as a command line choice. - Have the template patch ask the resolver instead of the legacy flags.
1 parent 84c4916 commit c444133

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
@@ -13,6 +13,7 @@ All changes included in 1.12:
1313
- ([jgm/pandoc#11831](https://github.com/jgm/pandoc/pull/11831)): HTML documents using `theme: pandoc` pick up Pandoc 3.13's style updates. Links now inherit the surrounding text color unless `linkcolor` is set, horizontal rules and table borders follow the text color, and printed output drops backgrounds and the fixed 12pt font size, rendering at the 11pt print size used by other Quarto HTML themes. Pandoc 3.13's automatic dark mode is not enabled for `theme: pandoc`.
1414
- ([jgm/pandoc#11845](https://github.com/jgm/pandoc/issues/11845)): In Word output, cross-reference targets are now hidden bookmarks with generated names instead of their Quarto id (`fig-plot`). Cross-reference links inside the document still work, but a link or script that looks a bookmark up by its Quarto id from outside the document, such as `report.docx#fig-plot` or `officer`, VBA or `python-docx` code, no longer finds it.
1515
- ([jgm/pandoc 3.13](https://github.com/jgm/pandoc/releases/tag/3.13)): A YAML bibliography file with a duplicated key, such as two `title` fields in one reference, now stops the render with an error giving the file, line and column. Before, the last value was kept silently. Remove the duplicate key to render again.
16+
- ([jgm/pandoc@c73a35a](https://github.com/jgm/pandoc/commit/c73a35a2e7ed1144132f1e533b4f0ae4c3648143)): 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.
1617

1718
## Accessibility
1819

‎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)