Repository navigation
[6227] LaTeX: keep the optional [Name] of \begin{theorem} after \newtheorem - #4755
Merged
Merged
Conversation
…heorem
\newtheorem and \declaretheorem re-registered the begin command of the
environment with arity 0, which overrode the built-in "optional argument
allowed" arity. The parser then no longer consumed [Name] after
\begin{theorem}, so it stayed as literal text in the body instead of
becoming a <dueto|Name>. Register the begin command with arity -1
(optional argument, no required ones).
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…m v22) Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Co-Authored-By: Claude Code <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4754
What happened
Importing
\begin{theorem}[Foo] ... \end{theorem}from a document that declares the theorem with\newtheorem{theorem}{Theorem}left[Foo]as literal text at the start of the theorem body instead ofmaking it the theorem's name (
<dueto|Foo>). Without the\newtheoremline the name was recognised.Background: how the importer decides whether an environment has an optional argument
parsetex.cppparses LaTeX into a tuple tree. For every command or environment it looks up a type and anarity (
latex_type,latex_arity; defaults come from the DRD inconvert-latex-command-drd.scm, anddocuments can change them while parsing). When the parser reaches the arguments of a command it does
so a negative arity means "an optional
[...]argument may follow, then-1 - arityrequired{...}arguments"; arity -1 is therefore "one optional argument, no required ones". If an option is found it is
stored as an argument and the name of the command is renamed with a trailing
*(theorem->theorem*).fromtex_post.cpp(finalize_layout) later turnstheorem*with its argument into<theorem>followed by<dueto|option>. The standard enunciations (theorem,lemma,proof, ...) areregistered with an optional argument by default (
latex-enunciation%implieslatex-environment-0*%).Cause
Declaring a theorem in the preamble also registers it in the parser. For
\newtheorem{name}{Title}and\declaretheorem[...]{name}the parser executesfor
\begin-name(and the same for\end-name). This is meant to make unknown, user-defined names behaveas enunciations, but it also runs for names that are already known. For
theorem, it replaces arity -1(optional argument) by arity 0 (no arguments). From then on
optionis false for every\begin{theorem}in the file, the parser never looks at the[, and[Foo]stays as ordinary charactersin the body. The post-processing that creates
<dueto|...>is correct; it is just never given an option.Since almost every document that uses theorems declares them with
\newtheorem, the named-theorem formwas effectively unavailable in real documents.
The evidence: the parse tree of the failing document has
(tuple "\begin-theorem")followed by the text"[","F","o","o","]", while the same document without the\newtheoremline yields<dueto|Foo>(the latter checked on the fixed build, whose change does not touch that path).Change
src/Plugins/Tex/parsetex.cpp, two one-token edits: in the\newtheorem/\newtheorem*branch and in the\declaretheorem/\declaretheorem*branch, the\begin-<name>registration uses arity -1 instead of 0,so a declared theorem still accepts an optional argument, which is what LaTeX itself does (
\newtheoremenvironments take
[...]). The\end-<name>registrations stay at arity 0: an end marker takes noarguments.
Effects:
\begin{theorem}[Foo]after\newtheorem{theorem}{Theorem}: the option is parsed, the marker is renamedtheorem*, and the existing post-processing produces<dueto|Foo>.\newtheorem{claim}{Claim}) get the same treatment; in the file converter\begin{claim}[Bar]converts to<dueto|Bar>.\begin{theorem}without an option is unaffected: with arity -1 the parser only consumes a bracket if oneactually follows.
theoremwith\newtheorem, a bodythat begins with
[immediately after\begin{theorem}is now read as an option, exactly as LaTeX does.New integration test
TeXmacs/tests/7004.scm(5 checks): a named theorem imports with adueto, with thename, and without the literal
[Foo]in the text; an unnamed theorem imports with nodueto.Verified (Windows, release build, headless)
TeXmacs/tests/7004.scm: 2 of 5 checks fail on unmodifiedupstream/main, 5 of 5 pass with thechange (console output below). It covers a named theorem and an unnamed one (which must not
get a
dueto).theorem.tex(shown below): before[Foo] Statement., after<dueto|Foo>Statement..\newtheorem{claim}{Claim}converts to<dueto|Bar>in the fileconverter as well.
Input and outputs
Input (
theorem.tex):Output of conversion on unmodified
upstream/main:Output with this change:
Test output, unmodified
upstream/main(the two failing checks) and with this change:Notes
\begin{theorem}is now read as an option;this matches LaTeX's own behaviour.
Not checked
\newtheoremwith a counter argument (\newtheorem{lemma}[theorem]{Lemma},\newtheorem{x}{Name}[section]) and the starred\newtheorem*/\declaretheorem*forms were notexercised separately.
latex->texmacsalone, a theorem name declared only by a user\newtheorem(e.g.
claim) comes out as(claim "Bar" (document ...))rather than withdueto, unlike the fileconverter. I did not investigate that difference and the test does not assert it.
How this fix was made
Developed through human-aided debugging: I reported the problem and ran and checked the builds, and the
cause was worked out interactively with an AI assistant. The code was written using Claude Sonnet 5.5
(Anthropic); I reviewed and tested it before submitting. The commit carries a
Co-Authored-Bytrailer tothe same effect.