Skip to content

engine: a maths macro's optional argument - #166

Merged
tannevaled merged 1 commit into
mainfrom
math-optional-arg
Aug 31, 2026
Merged

engine: a maths macro's optional argument#166
tannevaled merged 1 commit into
mainfrom
math-optional-arg

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

\newcommand{\cmd}[n][default] makes the first of the n parameters optional: \cmd is \@protected@testopt, which supplies {default} when no [ follows (latex.ltx:1187-1199, \@xargdef), and the remaining n−1 are grabbed as usual. The token path already does this (optArg/optDefault in engine.go); the maths source path read all n as mandatory.

That is worse than not expanding at all. With the optional argument written out the macro still "matched", by taking the brackets themselves as arguments:

\qbin [p]{N}{k}  ->  \genfrac []{0pt}{}{p}{]}_{[}{N}{k}
\qbin {N}{k}+1   ->  \genfrac []{0pt}{}{k}{+}_{N}1

— the formula came out silently wrong, the + of an expression becoming an argument. Without the optional argument nothing matched and the formula was dropped instead, which is how a paper's own

\newcommand{\qbinom}[3][q]{\genfrac[]{0pt}{}{#2}{#3}_{#1}}

lost 12 equations.

Measured — 200 arXiv papers, 200 beamer talks

5 documents change for +1234 glyphs. One repaginates, 362 → 363 against tectonic's 333 — a document already 29 pages clear of it — and the page error against the 157 renders moves 669 → 670. beamer does not move by one glyph.

Test

mathoptarg_test.go asserts the expansions themselves, since that is where the corruption is visible; all three fail on main with the stolen-bracket strings above. A second test takes it end to end: the formula must reach the page with the text around it.

\newcommand{\cmd}[n][default] makes the FIRST of the n parameters optional: \cmd is
\@Protected@testopt, which supplies {default} when no [ follows (latex.ltx:1187-1199,
\@xargdef), and the remaining n-1 are grabbed as usual. The token path already does
this (engine.go's optArg/optDefault); the maths source path read all n as MANDATORY.

That is worse than not expanding at all. With the optional argument written out the
macro still "matched", by taking the brackets themselves as arguments:

	\qbin [p]{N}{k}  ->  \genfrac []{0pt}{}{p}{]}_{[}{N}{k}
	\qbin {N}{k}+1   ->  \genfrac []{0pt}{}{k}{+}_{N}1

so the formula came out silently wrong, the + of an expression becoming an argument.
Without it, nothing matched and the formula was dropped — which is how a paper's own
\newcommand{\qbinom}[3][q]{\genfrac[]{0pt}{}{#2}{#3}_{#1}} lost 12 equations.

Measured over 200 arXiv papers and 200 beamer talks: 5 documents change for +1234
glyphs. One repaginates, 362 -> 363 against tectonic's 333 — a document already 29
pages long of it — and the page error against the 157 renders moves 669 -> 670.
beamer does not move by one glyph.
@tannevaled
tannevaled merged commit afb663c into main Aug 31, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant