Skip to content

Fix underbrace, subscript '+' and '-', and 'nitalic dn_i' - #304

Merged
jgm merged 1 commit into
jgm:masterfrom
jdpipe:fix/arrhenius-underbrace
Nov 6, 2026
Merged

jgm merged 1 commit into
jgm:masterfrom
jdpipe:fix/arrhenius-underbrace

Conversation

@jdpipe

@jdpipe jdpipe commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

I found that the current starmath conversion of the following equation was causing bad starmath syntax to be output. The result was bizarre LibreOffice behaviour, nearly a crash but not quite -- basically no active rendering in the main Impress pane any more. The issue was related to the implementation of the \underbrace translation in texmath, which is fixed in this PR. Hopefully I updated to the main repository status first, please advise if not.

$$
\text{rate contribution}
=\underbrace{A_j\exp\!\left(-\frac{E_j}{R_{\mathrm u}T}\right)}_{k_j(T):\ \text{Arrhenius coefficient}}
\times\underbrace{\text{gas factor}}_{\text{local hydrogen/steam}}
\times\underbrace{\text{solid factor}}_{\text{inventory, presence and access}}.
$$

Correct rendering in LO after applying the patch:
image

Edit I've corrected a second issue here also now:

$$
r_j^0 = r_{j,+}^{0}-r_{j,-}^{0}
$$

This incorrectly converted to

r_j^0 = r_{{j, + }}^0 - r_{{j, -}}^0

which renders with an error. Corrected, it now becomes:

r_j^0 = r_{{j, + {}}}^0 - r_{{j, - {}}}^0

Edit 2

Another glitch comes from this equation fragment:

\frac{\mathrm d n_W}{\mathrm d t}

which rendered incorrectly as

{nitalic dn_i} over {nitalic dt}

This needed to be changed to

{nitalic d n_i} over {nitalic d t}}

@jdpipe jdpipe changed the title Fix/arrhenius underbrace Fix/arrhenius underbrace and subscript '+' and '-' Oct 29, 2026
@jdpipe jdpipe changed the title Fix/arrhenius underbrace and subscript '+' and '-' Fix underbrace, subscript '+' and '-', and 'nitalic dn_i' Oct 29, 2026

@jgm jgm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked cases like \overbrace{a}+b or \underbrace{x}_{a}^2?
Also \frac{a}{-}?

Comment thread src/Text/TeXMath/Writers/StarMath.hs Outdated
isPlusMinusSign :: Exp -> Bool
isPlusMinusSign e =
case e of
ESymbol _ s -> s `elem` ["+", "-", "−"]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s == "+" || s == "-" || etc.

is better because it avoids construction of a list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed as suggested!

Comment thread src/Text/TeXMath/Writers/StarMath.hs Outdated
"↑" -> " uparrow "
"↓" -> " downarrow "
"↦" -> " mapsto "
"\8230 " -> " dotslow "

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this here (with extra space)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no good reason. removed the extra space after \8230 (…)

Comment thread src/Text/TeXMath/Writers/StarMath.hs Outdated
@@ -217,9 +217,10 @@ renderExpsIn dt ctx exps = do
if startsWithInfixNeedingLhs exps
then "{} " <> T.stripStart merged

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for symmetry should the space after {} be removed? (cf. what happens on rhs)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i made this change too.

jdpipe added a commit to jdpipe/texmath that referenced this pull request Nov 2, 2026
Supply empty labels for bare braces, preserve centered scripts around
annotated braces, and add missing operands to standalone operators in
fractions.

Use explicit sign comparisons, make empty-operand spacing consistent,
and remove the trailing-space ellipsis mapping.

Add nine regression tests. All 3,180 tests pass; visually checked
16 LibreOffice renderings against LaTeX.
@jdpipe

jdpipe commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

here's a comparison PDF showing how LO renders a few curly cases

comparison.pdf

@jdpipe

jdpipe commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Have you checked cases like \overbrace{a}+b or \underbrace{x}_{a}^2? Also \frac{a}{-}?

I also added some more test cases for these things. The \underbrace{x}_{a}^2 one is particularly fruity, but it's rendering consistently with Latex now.

Comment thread src/Text/TeXMath/Writers/StarMath.hs Outdated
Comment on lines +659 to +657
pure $ "{" <> base' <> " " <> under' <> "}"
under' <- renderScriptArg dt ctx under
pure $ base' <> " csub " <> centerScriptArg under'

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we lose the {..} that the original put around this expression. Might this cause problems?

@jdpipe jdpipe Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not. LibreOffice parses csub’s argument as a single term, and {a} explicitly terminates that term. Super/subscripts bind more tightly than product/sum operators, so subsequent terms cannot be absorbed into the label. Relevant code in the starmath parser: https://github.com/LibreOffice/core/blob/libreoffice-26.2.6.3/starmath/source/parse5.cxx#L1347-L1449

@jdpipe

jdpipe commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

while looking into this i found a deeper error/bug, which goes beyond the Starmath writer. I wanted to bring it specifically to your attention:

Latex input:

x_12

Current incorrect Starmath output:

x_{12}

Correct Starmath output:

x_{1}2

This is actually a reader bug, so it's going to affect all other output formats as well. Similar things happen for powers. Latex doesn't automatically group sub/superscript digits together; only the first token, if ungrouped, ends up as the sub/superscript in Latex.

@jgm

jgm commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Good -- can you split the TeX reader fix into a separate PR?

@jgm

jgm commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Can you rebase against current master, so we just have one clean commit?

Group annotated braces to preserve expression boundaries, supply labels
for bare braces, and retain scripts on the opposite side of a brace.
Supply missing operands for trailing signs and standalone operators in
fractions, and separate upright letters from following identifiers.

Reuse existing groups in roots, scripts, accents, and brace labels to
avoid redundant braces. Add regression coverage and update golden tests
for display and inline output.

Consolidates the StarMath changes from jgm#304 on current master; the TeX
numeric-script reader fix is already included upstream via jgm#306.
@jdpipe
jdpipe force-pushed the fix/arrhenius-underbrace branch from 1e4df7e to 4b128f4 Compare November 5, 2026 09:00
@jdpipe

jdpipe commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

hopefully i did that right...

@jgm
jgm merged commit b0d74f4 into jgm:master Nov 6, 2026
6 checks passed
@jgm

jgm commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Great!

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.

2 participants