Skip to content
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

Long equations from sympy do not wrap #2208

Open
gabemorris12 opened this issue Feb 21, 2025 · 6 comments
Open

Long equations from sympy do not wrap #2208

gabemorris12 opened this issue Feb 21, 2025 · 6 comments

Comments

@gabemorris12
Copy link

gabemorris12 commented Feb 21, 2025

When converting to a PDF, long equations from cell outputs do not wrap correctly:

Image

It does wrap the equation but it still gets clipped off and extends into the margins. This also happens for big matrices:

Image

A similar issue is seen in #275, but I don't think this is quite the same thing.

Steps to Reproduce
Make a notebook that contains the following cells and run it.

import sympy as sp

t, v0 = sp.symbols('t v0')
x = sp.Function('x')(t)

eq = sp.Eq(2*x.diff(t, 4) + 14*x.diff(t, 3) + sp.Rational(93, 2)*x.diff(t, 2) + 127*x.diff(t) + 148*x, 200*sp.Heaviside(t))
eq

# Producing a really long equation
sol = sp.dsolve(eq, ics={
    x.subs(t, 0): 0,
    x.diff(t, 1).subs(t, 0): v0,
    x.diff(t, 2).subs(t, 0): 0,
    x.diff(t, 3).subs(t, 0): 0
})
sol
# Large expression with shorter terms
terms = [sp.sin(i*t) for i in range(100)]
expr = sum(terms)
expr
# Producing a large matrix expression
x = sp.symbols('x0:10')
x_list = [x[i] for i in range(10)]
x_mat = sp.Matrix(x_list)

y = sp.symbols('y0:10')
y_list = [y[i] for i in range(10)]
y_mat = sp.Matrix(y_list)

A = sp.Matrix(10, 10, lambda i, j: sp.sin(i + j))
expr = sp.MatMul(A, x_mat)
for _ in range(10):
    expr = sp.Add(expr, y_mat)
    expr = sp.Add(expr, x_mat)
expr

Run jupyter nbconvert --to pdf <notebook_name.ipynb>

Then you will see this:

Image

It wraps ok with the long equation that has shorter terms, but in that first cell, there is a really long term that should be brought to the next line. The same is true for the large matrix term in the last output.

@t-makaro
Copy link
Contributor

This is definitely a duplicate of #275. NBconvert simply takes the latex that sympy outputs (which struggles with large subexpressions), and I'm not sure there is a way to fix this given the differences between XeLaTeX and MathJax.

@mgeier
Copy link
Contributor

mgeier commented Feb 23, 2025

@gabemorris12 For reproducibility, can you please provide a full example of (copyable) code that produces the problematic equations?

@gabemorris12
Copy link
Author

@gabemorris12 For reproducibility, can you please provide a full example of (copyable) code that produces the problematic equations?

I made edits to reproduce this issue.

@mgeier
Copy link
Contributor

mgeier commented Mar 2, 2025

I made edits to reproduce this issue.

Thanks!

@t-makaro

I'm not sure there is a way to fix this given the differences between XeLaTeX and MathJax.

There are definitely limits, but I think it can still be improved by what I suggested back then in #275 (comment).

@gabemorris12 You can try it with https://github.com/spatialaudio/nbsphinx, which does a slightly better job at breaking equations (but it also has its limits!):

Image

Image

@gabemorris12
Copy link
Author

I made edits to reproduce this issue.

Thanks!

@t-makaro

I'm not sure there is a way to fix this given the differences between XeLaTeX and MathJax.

There are definitely limits, but I think it can still be improved by what I suggested back then in #275 (comment).

@gabemorris12 You can try it with https://github.com/spatialaudio/nbsphinx, which does a slightly better job at breaking equations (but it also has its limits!):

Image

Image

That output is better. I like that it breaks up terms, but I'm wondering how it should handle the case where a single term can be longer than the width of the page. Thanks, and I'll look into using this.

@mgeier
Copy link
Contributor

mgeier commented Mar 3, 2025

I'm wondering how it should handle the case where a single term can be longer than the width of the page

As I mentioned in #275 (comment), it looks like \left( (which SymPy generates by default) inhibits line breaks. I guess a similar thing happens with the brackets of matrices, and obviously breaking a long fraction will not work.

There might be ways to change the line breaking behavior, but this is beyond my TeX/LaTeX skills. Maybe you can ask at a place like https://tex.stackexchange.com/? I've gotten very helpful answers there in the past.

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

No branches or pull requests

3 participants