KenyC / ReX

ReX - typesetting mathematics
MIT License
7 stars 0 forks source link

Aligned Environment #14

Closed JohnPiwinski closed 5 months ago

JohnPiwinski commented 12 months ago

I wish to thank you first for writing such an excellent library. I am planning on creating an interactive presentation framework for mathematics that will allow embedding OpenGl simulations, which can be manipulated by the users. For this end, I wished to render long multiline equations equations that are common in mathematical presentations. Is the aligned environment currently supported?

I work primarily in analysis, so the equations are typically chains of inequalities that resemble the following.

image

If the aligned environment is not currently supported, do you think that I could use a matrix environment in the meantime? It would probably have to be modified so that the two parenthesis of the matrix are not shown. Under this hypothetical model, the \leq, \geq, or = signs would be in the middle column while the subformulae would be in the first and third columns.

Instead of

\begin{align}
f(x) &= x^2 \\
       &\leq |x|^2
\end{align}

the code

\begin{matrix}
f(x) & = & \\
       & \leq & |x|^2 \\
\end{matrix}

could suffice so long as the boundaries of the matrix were removed.

Thank you again for your efforts. I truly appreciate them.

Best regards, John Piwinski

KenyC commented 10 months ago

Thanks for your interest! I'm truly sorry I didn't see your issue earlier ; I never saw a notification for it. The best approximation currently to what you want to do is:

\begin{array}{lll}
f(x) & = & \ldots \\
       & \leq & |x|^2 \\
\end{array}

The matrix environment is in fact nothing but an array environment with specified alignment and delimiters

The only problem is that the column spacing creates quite jarring gaps between columns:

image

The next best option is to construct the desired layout manually. A very rough example

    // -- Parse formula
    let cells = [
        [rex::parser::parse("f(x)").unwrap(), rex::parser::parse("=").unwrap(),     rex::parser::parse("\\ldots").unwrap(),],
        [rex::parser::parse("").unwrap(),     rex::parser::parse("\\leq").unwrap(), rex::parser::parse("y").unwrap(),],
    ];

    // -- Lay out nodes in grid pattern
    // TODO: expose VBox and HBox
    let mut grid = Grid::new();
    grid.insert(0, 0, layout(&cells[0][0], layout_settings).unwrap().as_node());
    grid.insert(0, 1, layout(&cells[0][1], layout_settings).unwrap().as_node());
    grid.insert(0, 2, layout(&cells[0][2], layout_settings).unwrap().as_node());
    grid.insert(1, 0, LayoutNode { node: rex::layout::LayoutVariant::Kern, width: Unit::ZERO, height: Unit::new(3.), depth: Unit::ZERO });
    grid.insert(2, 0, layout(&cells[1][0], layout_settings).unwrap().as_node());
    grid.insert(2, 1, layout(&cells[1][1], layout_settings).unwrap().as_node());
    grid.insert(2, 2, layout(&cells[1][2], layout_settings).unwrap().as_node());
    let mut layout = Layout::new();
    layout.add_node(grid.build());

image

I plan on implementing the align environment at some point so I'll leave the issue open

KenyC commented 5 months ago

With eae2394f1227970013c54b6255677d49303eb614, there is now support for \begin{aligned}..\end{aligned} (which is the version of align that can be used directly within a formula). So you can do:

\begin{aligned}
f(x) &= x^2 \\
       &\leq |x|^2
\end{aligned}

Which renders as:

aligned example

I haven't checked it extensively so one might still run into display errors now and then.