3-manifolds / Sage_macOS

SageMath as a macOS application bundle.
152 stars 15 forks source link

File > Download as > PDF via LaTeX #51

Closed GMS103 closed 1 year ago

GMS103 commented 1 year ago

In a SageMath 9.7 Notebook File > Download as > PDF via LaTeX gives 500 : Internal Server Error The error was: nbconvert failed: xelatex not found on PATH, if you have not installed xelatex you may need to do so. Find further instructions at https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex.

What I have done (perhaps needlessly): brew install pandoc

I think the problem is here:

import os
print(os.environ['PATH'])

gives

/Applications/SageMath/SageMath-9-7.app/Contents/Frameworks/Sage.framework/Versions/9.7/build/bin:/private/var/tmp/sage-9.7-current/local/var/lib/sage/venv-python3.10.5/bin:/var/tmp/sage-9.7-current/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

where I would need to add /Library/TeX/texbin (in a permanent way if possible).

herbs commented 1 year ago

On Jan 11, 2023, at 4:44 AM, Guillermo Moreno-Socías @.***> wrote:

In a SageMath 9.7 Notebook File > Download as > PDF via LaTeX gives 500 : Internal Server Error The error was: nbconvert failed: xelatex not found on PATH, if you have not installed xelatex you may need to do so. Find further instructions at https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex. What I have done (perhaps needlessly): brew install pandoc I think the problem is here: import os print(os.environ['PATH'])

gives /Applications/SageMath/SageMath-9-7.app/Contents/Frameworks/Sage.framework/Versions/9.7/build/bin:/private/var/tmp/sage-9.7-current/local/var/lib/sage/venv-python3.10.5/bin:/var/tmp/sage-9.7-current/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

where I would need to add /Library/TeX/texbin (in a permanent way if possible). — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

Howdy,

How did you install TeX? Did you install TeX Live via MacTeX? What is your PATH variable in Terminal?

Good Luck,

Herb Schulz @.***

GMS103 commented 1 year ago

Hi Herb. Yes, I installed MacTeX and it is up to date. echo $PATH gives (replacing : with \n)

/opt/homebrew/bin
/usr/bin
/opt/homebrew/sbin
/opt/homebrew/opt/texinfo/bin
/opt/homebrew/opt/openssl@3/bin
/Users/gms/opt/miniconda3/bin
/usr/local/bin
/bin
/usr/sbin
/sbin
/Library/TeX/texbin

and which xelatex gives /Library/TeX/texbin/xelatex which works (I use it all the time). My question is how to modify the PATH used by SageMath and everything it contains (in /Applications/SageMath-9-7.app).

culler commented 1 year ago

This has been addressed in the SageMath 9.8 binary. When you start the app you can select the SageMath->Preferences (or SageMath->Settings) menu to open a dialog which allows specifying arbitrary environment variables for the sage session which is about to be launched.

The PATH environment variable will be appended with a minimal path containing what sage needs in order to run. Other variables are not modified (so be careful!).

GMS103 commented 1 year ago

Thanks for looking into this! Unfortunately, I get the same as before. version() gives SageMath version 9.8, Release Date: 2023-02-11 and import os; print(os.environ['PATH']) gives /Library/TeX/texbin:/var/tmp/sage-9.8-current/local/bin:/var/tmp/sage-9.8-current/venv/bin:/bin:/usr/bin:/usr/local/bin but File > Download as > PDF via LaTeX (.pdf) opens a new window with

500 : Internal Server Error
The error was:
nbconvert failed: xelatex not found on PATH, if you have not installed xelatex you may need to do so. Find further instructions at https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex.

Let me know if I can be of any help.

GMS103 commented 1 year ago

I have noticed that without setting anything for PATH, /Library/TeX/texbin is already there:

:/var/tmp/sage-9.8-current/local/bin:/var/tmp/sage-9.8-current/venv/bin:/bin:/usr/bin:/usr/local/bin:/Library/TeX/texbin

(with an empty element at the beginning).

herbs commented 1 year ago

On Feb 22, 2023, at 5:58 PM, Guillermo Moreno-Socías @.***> wrote:

I have noticed that without setting anything for PATH, /Library/TeX/texbin is already there: :/var/tmp/sage-9.8-current/local/bin:/var/tmp/sage-9.8-current/venv/bin:/bin:/usr/bin:/usr/local/bin:/Library/TeX/texbin

(with an empty element at the beginning). — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

Howdy,

That is appended to the path automatically when you install TeX Live using the MacTeX installer.

Good Luck,

Herb Schulz @.***

GMS103 commented 1 year ago

Thanks Herb. But I was talking about SageMath PATH, as given by import os; print(os.environ['PATH']) Sorry if I was unclear.

herbs commented 1 year ago

On Feb 22, 2023, at 7:05 PM, Guillermo Moreno-Socías @.***> wrote:

Thanks Herb. But I was talking about SageMath PATH, as given by import os; print(os.environ['PATH']) Sorry if I was unclear.

Howdy,

But doesn't it inherit the base PATH from the system (i.e., before changes made in your login shell)?

Good Luck,

Herb Schulz @.***

GMS103 commented 1 year ago

I do not think so, that is why Marc had to change something. Otherwise I would not have had this problem, I think.

culler commented 1 year ago

I actually did test to make sure that the download as -> pdf via latex works correctly before I did the release. After this report I did a lot more testing. The situation is that sometimes it works and sometimes it doesn't. That fact is independent of how the app is launched, but the failures are more frequent when the app is launched by clicking the icon versus using the "open" command.

This is going to be very unpleasant to work around.

The actual exception that leads to the 500 error is raised in nbconvert.py when shutil.which('xelatex') returns None. If you run that command at the sage prompt it always returns the correct path. But somehow, when the jupyter kernel runs that command it often, but not always, fails.

GMS103 commented 1 year ago

Thanks again for looking into this. As you say, it does not seem easy to debug. In any case, I am available for testing.

culler commented 1 year ago

At least the leading ':' in the PATH is a bug that can be easily fixed.

culler commented 1 year ago

@GMS103: Can you please test the disk image in the v1.6.1 pre-release?

GMS103 commented 1 year ago

I have tried several times, and it works! This seems independent of the addition of any component to PATH in Settings. The order in which the elements appears changes. import os; print(os.environ['PATH']) gives (replacing : with \n)

/Applications/SageMath/SageMath-9-8.app/Contents/Frameworks/Sage.framework/Versions/9.8/build/bin
/private/var/tmp/sage-9.8-current/local/var/lib/sage/venv-python3.11.1/bin
/var/tmp/sage-9.8-current/local/bin
/Applications/SageMath/SageMath-9-8.app/Contents/Frameworks/Sage.framework/Versions/9.8/build/bin
/private/var/tmp/sage-9.8-current/local/var/lib/sage/venv-python3.11.1/bin
/var/tmp/sage-9.8-current/local/bin
/Library/TeX/texbin
/var/tmp/sage-9.8-current/local/bin
/var/tmp/sage-9.8-current/venv/bin
/bin
/usr/bin
/usr/local/bin

or

/Applications/SageMath/SageMath-9-8.app/Contents/Frameworks/Sage.framework/Versions/9.8/build/bin
/private/var/tmp/sage-9.8-current/local/var/lib/sage/venv-python3.11.1/bin
/var/tmp/sage-9.8-current/local/bin
/Applications/SageMath/SageMath-9-8.app/Contents/Frameworks/Sage.framework/Versions/9.8/build/bin
/private/var/tmp/sage-9.8-current/local/var/lib/sage/venv-python3.11.1/bin
/var/tmp/sage-9.8-current/local/bin
/var/tmp/sage-9.8-current/local/bin
/var/tmp/sage-9.8-current/venv/bin
/bin
/usr/bin
/usr/local/bin
/Library/TeX/texbin

and there are duplicate paths in the list.

culler commented 1 year ago

I'm glad to hear that it worked!

It should not be necessary (for most users) to add anything to the PATH in order to get LaTeX to work, because I added /Library/TeX/texbin to the default path.

I think the reason it was failing sporadically before was that I was resetting the path after the Jupyter server was launched. That created a race. If the Jupyter server finished launching before the PATH was changed it would not have the correct PATH. Now I am doing it much earlier. That has the side effect that I do not have to modify any of the Sage startup scripts, which is a good thing. But another side effect is that the standard Sage startup prepends its own paths to the PATH. Some of those are duplicates of paths that are already there. But duplicate paths are not a big problem since the search stops as soon as an executable is found. The duplicates are only checked when the executable does not exist.

I think I can close this now and upgrade the pre-release to a release

GMS103 commented 1 year ago

Good detective work! Thanks again.

herbs commented 1 year ago

On Feb 23, 2023, at 7:57 PM, Marc Culler @.***> wrote:

It should not be necessary (for most users) to add anything to the PATH in order to get LaTeX to work, because I added /Library/TeX/texbin to the default path.

Howdy,

Just to note: while TeX Live installed via the MacTeX installer package is the most popular TeX Distribution on the Mac there are others that may not use /Library/TeX/texbin to (indirectly) point to the active Distribution's binaries.

Good Luck,

Herb Schulz @.***

culler commented 1 year ago

That was the purpose of all this extra work. Now those people can add their own weird path to their Sage environment.