Skip to content

Fix a number of line-breaking problems in tables and elsewhere. (mathjax/MathJax#3416) #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

dpvc
Copy link
Member

@dpvc dpvc commented Aug 18, 2025

This PR addresses a number of problems with line breaking, particular in tables. Issue mathjax/MathJax#3416 pointed out a bad bounding box for SVG output, but this also affects CHTML output. For example:

<div style="width:20em; border: 2px solid red">
$$
\bbox[orange]{
\begin{aligned}
x+y+z+x+y+z &= A+B+C+D+E+F+G+H+I+J
\end{aligned}
}
$$
</div>

produces

image

in SVG output and

image

in CHTML. The vertical positioning is not correct in either, due to improperly handling the change in height/depth of the cells with broken lines.

Worse, if a cell isn't broken, then the width can be set to 0, as in

<div style="width:25em; border: 2px solid red">
$$
\bbox[orange]{
\begin{aligned}
x+y+z+x+y+z &= A+B+C+D+E+F+G+H+I+J
\end{aligned}
}
$$
</div>

which produces

image

for CHTML and

image

in SVG.

When intentshift is specified (as it is by default in the alignment environments), that width may not be taken into account in CHTML (here I'm breaking the one manually):

<div style="width:20em; border: 2px solid red">
$$
\bbox[orange]{
\begin{aligned}
{x+y+z\\+x+y+z} &= {A+B+C\\+D+E+F}
\end{aligned}
}
$$
</div>

generates

image

though it is OK in SVG output.

Finally, when a width is specified via \hsize, tables don't break to the proper width (they use the full container width):

<div style="width:20em; border: 2px solid red">
$$
\bbox[orange]{
\vbox{\hsize=10em
\begin{aligned}
x+y+z+x+y+z &= A+B+C+D+E+F+G+H
\end{aligned}
}}
$$
</div>
image

This PR fixes all these issues, resolving mathjax/MathJax#3416 (and other issues not mentioned there).

Make sure line breaking is enabled when testing this PR.


The change in chtml/Wrappers/mrow.ts fixes the issue with the indentshift in CHTML (the relative positioning doesn't change the box's width, as is needed to get the width correct).

The chtml/Wrappers/mtr.ts change fixes the height of a table with line breaks in CHTML.

The change in common/Wrapper.ts and common/Wrappers/mpadded.ts are the changes needed to get the \hsize to be respected. The \vbox and related boxes are implemented through mpadded elements, and in order to allow tables to know the width they have to work with, the containerWidth getter now looks through the parents and either uses the width from the metrics, or if there is an intervening mpadded with an absolute width attribute and data-overflow set to linebreak (which will only happen for boxes with \hsize specified), the specified width is used.

The changes to common/Wrappers/mrow.ts allow the row's bbox.w to be adjusted if the shifting of lines makes the width wider than when the lines aren't shifted.

The changes to common/Wrappers/mtable.ts are the biggest modifications. It turns out that the row heights and depths weren't being adjusted properly for changes when cells have line breaks, but also the rowalign values weren't being properly handled, either.

The extendHD() function that was supposed to adjust the height and depth of rows according to the alignments was not correct, and has been removed in favor of a new approach, which occurs now within the updateHDW() method. The updateHDW() function used to compute H and D as though the row always had baseline alignment, but now it uses a new adjustHD array of functions to properly adjust the H and D values according to the row alignment attributes. We no longer need the M value.

The new updateHDW() function (at the bottom of the file) is now used in the breakColumn() function to get the correct alignments (the old version didn't take the alignment attributes into account), and now includes the scaling factor, like updateHWD() does. Both breakColumn() and getTableData() now update the row bbox height and depth to match the computed values.

@dpvc dpvc added this to the v4.0.1 milestone Aug 18, 2025
@dpvc dpvc requested a review from zorkow August 18, 2025 18:58
@dpvc dpvc changed the title Fix a number of line-breaking problems in tables and elsewhere. (mathjax/MathJax#3415) Fix a number of line-breaking problems in tables and elsewhere. (mathjax/MathJax#3416) Aug 18, 2025
Copy link

codecov bot commented Aug 18, 2025

Codecov Report

❌ Patch coverage is 61.70213% with 36 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.69%. Comparing base (efcdd46) to head (6176066).

Files with missing lines Patch % Lines
ts/output/common/Wrappers/mtable.ts 73.23% 19 Missing ⚠️
ts/output/common/Wrappers/mpadded.ts 22.22% 7 Missing ⚠️
ts/output/common/Wrappers/mrow.ts 30.00% 7 Missing ⚠️
ts/output/chtml/Wrappers/mtr.ts 0.00% 2 Missing ⚠️
ts/output/chtml/Wrappers/mrow.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1338      +/-   ##
===========================================
- Coverage    86.72%   86.69%   -0.03%     
===========================================
  Files          337      337              
  Lines        84145    84186      +41     
  Branches      4769     3142    -1627     
===========================================
+ Hits         72971    72988      +17     
- Misses       11151    11198      +47     
+ Partials        23        0      -23     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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