How to call Sage from PowerShell in Windows?

asked 2018-02-25 00:09:02 +0200

Dillon Ethier gravatar image

updated 2018-02-26 16:35:56 +0200

I just installed Sage on Windows 10 directly (without using VM), and I'm wondering how I can run sage on a .sage file without typing commands into the sage shell, or just run it in PowerShell. I would like to automate sagetex as much as possible, and I find it's time-consuming to have to go over to the sage shell and type in commands that way. Is there any way to do this from PowerShell, or possibly to automate sending these commands to the sage shell?

Edit: I figured out the following clunky solution and tried to submit it as an answer, but whenever I click submit, nothing happens. I'd rather have it be available to anyone else who might be wondering the same thing.

I managed to get it to work good enough for my purposes- I used the following in a .bat file to run sage on a .sagetex.sage file:

cmd /c C:\Progra~1\SageMath-8.1\runtime\bin\mintty.exe -t SageTeX /bin/bash --login -c "cd %cygpath% && /opt/sagemath-8.1/sage %2.sagetex.sage | tee %2.sagetex.log" | more

where cygpath is a variable representing the absolute path to the current directory as cygwin would represent it (i.e. with forward slashes and beginning with /cygdrive/drive/ instead of drive:\ - this required some light pre-processing), and %2 is the basename of the .tex file (without the extension). I tried calling sage on the absolute path of the file, but it never worked, which is why I cd into the directory first before running sage. The pipe to tee just makes it so I get a log of sage's processing, since the sage shell window auto-closes whenever it's done.

One major thing is that I absolutely could not get this to work without renaming my root Sage directory to SageMath-8.1 since by default it installs in "SageMath 8.1" with a space which was causing a lot of problems. Essentially cmd can't handle having more than one quoted string, and I absolutely needed to have the last part quoted. This doesn't seem to have broken anything with Sage, though it did require me to reroute all my existing shortcuts.

The pipe to "more" at the end just ensures that the script won't continue until sage is done processing the .sagetex.sage file. It won't help to run pdflatex again until that time.

Now I can automatically run sagetex directly from Notepad++ with the help of nppexec. I'd upload the .bat file, but apparently I'm not allowed to attach anything. I mostly just adapted the script found at with a few extra lines after the intial run of pdflatex.

To call from Powershell, you only have to modify this by replacing "cmd /c" with just an ampersand "&"

If anyone knows of a less clunky way to do this, please feel free to share it.

edit retag flag offensive close merge delete