Kolaru / MathTeXEngine.jl

A latex math mode engine in pure Julia.
MIT License
97 stars 19 forks source link

Initial layout for matrices #111

Open adamslc opened 10 months ago

adamslc commented 10 months ago

This builds on the work in #56 (which itself builds on #50). All those commits have been rebased onto master for convience.

I implemented a simple layout algorithm for matrices (only supports matrix right now, but extending to include pmatrix and bmatrix should be possible). It works quite well for simple entries, although perhaps the entries should be centered horizontally and vertically. test.pdf

However, you can see that the layout is very poor when taller notation (e.g. fractions) are used. I think this results from a misunderstanding on my part of how they layout is done. In the second attached pdf, you can see pairs of horizontal lines where I think the symbols should be, which disagrees quite dramatically with where they are. test_vert_extent.pdf

codecov-commenter commented 10 months ago

Codecov Report

Attention: 88 lines in your changes are missing coverage. Please review.

Comparison is base (91aefd0) 81.02% compared to head (18a03e5) 70.96%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #111 +/- ## =========================================== - Coverage 81.02% 70.96% -10.07% =========================================== Files 10 10 Lines 585 675 +90 =========================================== + Hits 474 479 +5 - Misses 111 196 +85 ``` | [Files](https://app.codecov.io/gh/Kolaru/MathTeXEngine.jl/pull/111?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Beno%C3%AEt+Richard) | Coverage Δ | | |---|---|---| | [src/parser/commands\_registration.jl](https://app.codecov.io/gh/Kolaru/MathTeXEngine.jl/pull/111?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Beno%C3%AEt+Richard#diff-c3JjL3BhcnNlci9jb21tYW5kc19yZWdpc3RyYXRpb24uamw=) | `87.50% <0.00%> (-1.01%)` | :arrow_down: | | [src/engine/layout.jl](https://app.codecov.io/gh/Kolaru/MathTeXEngine.jl/pull/111?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Beno%C3%AEt+Richard#diff-c3JjL2VuZ2luZS9sYXlvdXQuamw=) | `76.43% <0.00%> (-13.80%)` | :arrow_down: | | [src/parser/texexpr.jl](https://app.codecov.io/gh/Kolaru/MathTeXEngine.jl/pull/111?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Beno%C3%AEt+Richard#diff-c3JjL3BhcnNlci90ZXhleHByLmps) | `55.55% <0.00%> (-24.81%)` | :arrow_down: | | [src/parser/parser.jl](https://app.codecov.io/gh/Kolaru/MathTeXEngine.jl/pull/111?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Beno%C3%AEt+Richard#diff-c3JjL3BhcnNlci9wYXJzZXIuamw=) | `63.18% <29.41%> (-12.60%)` | :arrow_down: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Kolaru commented 10 months ago

Thanks a lot for the PR !

The alignment problem is there because of where the origin of glyphs (and TeXElements) is. Basically if you draw a TeXElement at position (0, 0) it should sit correctly on the text baseline, at the leftmost reasonable point.

This graph has been quite useful to me (xMin, xMax, yMin and yMax are ink bounds, leftinkbound, rightinkbound, bottominkbound and topinkbound respectively in term of MathTeXEngine functions) image

As a consequence, the vertical offset[*] for a pair of row (top, bot) should be something like bottominkbound(top) - row_gap - topinkbound(bot). The minus signs are there because the offset goes negative as we go down the page.

Then an additional global offset is needed to set the origin of the matrix itself correctly.

[*] The same principle apply for horizontal, but it doesn't matter as much, because the horizontal origin in always roughly at the left edge.

Regarding the other matrix types, I believe we can just translate them to be surrounded by adaptive parentheses, like \left( \begin{matrix} ... \end{matrix} \right) and it should work fine. It can be a separate PR too.

Also in the prototype folder there is a script that can draw the bounding box of all characters, you may find it useful.