connorferster / handcalcs

Python library for converting Python calculations into rendered latex.
Apache License 2.0
5.68k stars 435 forks source link

Long lines from cell output are truncated instead of being wrapped in PDF #199

Open DonCammne opened 8 months ago

DonCammne commented 8 months ago

Problem: If the output of a cell created with handcalcs is too long, the pdf just truncates it.

I tried to pinpoint where the problem originates, in order to plan a solution, but I don't understand if I can set up correctly handcals, because the creation of the pdf (performed by nbconvert) seems to be limited by the source (thus the NOTEBOOK.ipynb).

I'm sure that I'm not the first person to encounter a truncated line in the pdf, so how do you solve this problem?

More details: I'm on linux (Arch) and I use a simple command line to create the html (then printed in pdf in the browser) or directly the pdf:

jupyter nbconvert --to html/pdf --no-input NOTEBOOK.ipynb

This procedure should be the same of using JupyterLab.

Here are some images to ease the explanation:

2024-03-02_20-46 Image 1. The cell in vscode with a long line

2024-03-02_20-53 Image 2. The html file (converted from the notebook)

2024-03-02_20-53_1 Image 3. The pdf converted from the html using "Print to pdf" (NB: some lines are not complete)

2024-03-02_20-54 Image 4. The pdf file (converted from the notebook) (NB: some lines are not complete)

Code: And here's the code of the example for replicating the problem: Cell 1:

import handcalcs.render
import forallpeople as si
si.environment('structural', top_level=False)

def sN(a):
    return '{:.3e}'.format(a) # to see the result in the scientific notation

Cell 2:

%%render params 2

d = 200*si.mm
b_f = 100*si.mm
t_w = 5.6*si.mm
t_f = 8.5*si.mm
r = 12*si.mm

Cell 3:

%%render

I_y_IShape = (b_f*d**3-(b_f-t_w)*(d-2.0*t_f)**3)/12.0+0.8584*r**2*(0.5*d-t_f-0.4467*r/2.0)**2  # With fillets
I_y_scientificNotation = sN(I_y_IShape)
connorferster commented 8 months ago

Yeah, this has been something that has bothered me for a long time.

The short answer is that it is a problem with the render to PDF engine and I am unsure how to fix it.

It seems to happen both when rendering to HTML in the browser and apparently in LaTeX, as shown by your example.

I have two strategies for managing this in my own calcs:

  1. I break up the calculation into pieces (if there are obvious "components" separated by + or - operators)
  2. I use the browser Print dialog to scale the page until the calculation fits, switching to landscape if it makes sense to.

In an earlier version of handcalcs, I used the /gather environment for the long calcs. However, because this environment was not supported by Katex (at the time, anyway), I switched away from it. The gather environment gives a liiiitle bit of extra room on the line.

For HTML rendering this is an upstream issue for Katex and MathJax to address. In LaTeX, additional layout code or macros would need to be added to the document in order to correct it (I think...).

DonCammne commented 8 months ago

Thank you very much for the exhaustive response, very appreciated.

I will probably divide in parts the long calculation (which are a few, so not a big deal), it is a solution nonetheless, so thank you again.