I have installed and tried SageTeX (using pdflatex, opening the sage console and using pdflatex again). This works fine. I want to get a user command in TeXstudio that calls sage without the need to open the console. I have found the question here that answers this question for Linux and Texmaker. This states that I have to plug in the complete path of sage in the command line: <path>/sage %.sagetex.sage. I was not able to adapt this answer to Windows since in my Sage-folder there are only links to executables and I dont know which .exe I should take. Can someone help?

EDIT: Thanks to Marijn's comments, I managed to get the sage console started with the command "<path>\SageMath 8.6\runtime\bin\mintty.exe" /bin/bash --login -c '/opt/sagemath-8.6/sage -sh 'sage %.sagetex.sage (either with or without --login and/or -sh), but nothing more happens.

  • Which executables are there in the folder? Presumably something called sage.exe should work. Also, how do you 'open the sage console' exactly? The command you use for that should also work for the TeXStudio compile command sequence. As in the linked question, you could also write a batch (.bat) file that performs the sequence of steps and call that file from TeXStudio. – Marijn Feb 2 '19 at 4:39
  • There is a .lnk file named "Sage Math Shell" that I use to open the console. This links to "<path>\SageMath 8.6\runtime\bin\mintty.exe" -t 'SageMath 8.6 Shell' -i sagemath.ico /bin/bash --login -c '/opt/sagemath-8.6/sage -sh' and type sage there. I tried using the executable mintty but this did not work. Also I did not find any executable named sage. Unfortunately I don't know enough to write a batch file and call that, so I would also need help for this (and would still be interested how to get that to work without batch file). – Martin Feb 2 '19 at 9:18
  • You could try that full line (without the -t 'SageMath 8.6 Shell' and the i- sagemath.ico which set the title and the icon for the terminal window) with or without the --login and probably without the -sh and add sage %.sagetex.sage at the end - maybe you need to play around a bit to find the correct configuration options. – Marijn Feb 2 '19 at 16:39
  • Using "<path>\SageMath 8.6\runtime\bin\mintty.exe" /bin/bash --login -c '/opt/sagemath-8.6/sage -sh 'sage %.sagetex.sage opens sage in the console, but nothing happens (if I omit --login and/or -sh the console just opens for a short time and closes again). – Martin Feb 2 '19 at 17:16
  • Opening and closing directly is what you want - as long as Sage is executed in between. Maybe you can add ;sleep 5 after the sage command to pause the console for 5 seconds so you can see what is happening before it closes? – Marijn Feb 2 '19 at 22:00

As explained below we can work around most issues raised. And here is the customising to run in ubiquitous TeXworks (TeXstudio in detail below). enter image description here

SageMaths is developed to run with a customised version of Python currently undergoing changes from version 2.7 to 3.x.

SageTex is not provided as a package in Tex Live but is supplied by MiKTeX. Normally if a package is available via the TeX package manager you are encouraged to use it in preference to download from CTAN.

SageTex recommends (in fact requires) that the compatible version dependant .sty is obtained from within SageMaths for that reason you will need to find and copy D:\Programme\SageMath 8.6\runtime\opt\sagemath-8.6\local\share\texmf\tex\latex\sagetex\sagetex.sty to your working folder. This should prevent MiKTeX from using its incompatible version. (alternatively uninstall the MiKTeX version and add the TDS compliant folder D:\Programme\SageMath 8.6\runtime\opt\sagemath-8.6\local\share\texmf in MiKTeX or copy the texmf\tex folder contents to either your local or system texmf\tex)

This is just step one of many pitfalls I encountered. So now you can compile the example or one of your own files with the version that will work with "SageMath 8.6" (note this issue has dogged SageTeX with every version update and is aggravated by having a space in between)

Whilst mentioning spaces POSIX will accept escaped spaces such as '\ ' however in the hybrid calls between windows and POSIX through Cygwin using "/program\ files/" would cause windows to choke so it would be easier to use DOS style short filenames such as c:\progra~1\sagema~1\ etc. however that's not necessary in this case since the application is in D:\path\with\no\spaces.

That raises the next issue which is that Cygwin expects when crossing drives to have a prefix /cygdrive/c for drive c: and /cygdrive/d for d: However by trimming the commands to the bone I eventually dropped all such problems

For all the above reasons, and more, the simple task of calling
sage example.sagetex.sage requires scripting behind the scenes, it would be oh so easy if there was simply a sage.exe to transmute all those problems. so what have I learnt over the weeks of investigation.

1) SageMath is wrapped in layers of wrappers and a law unto itself
2) Prior to installing make a c:\sage\work\ and when asked where your user HOME is then point to that folder.
3) Install sagemath into a directory with no version name (thus no spaces) e.g. c:\sage\app\

Here is your answer, In your case first ensure D:\Programme\SageMath 8.6\runtime\bin is on your path e.g. if running from a temporary command line or in a .cmd file add

set path=D:\Programme\SageMath 8.6\runtime\bin;%path%  

do the first pdfLaTeX run (with the correct sty in the folder)

either copy or move example.sagetex.sage to your "HOME" directory probably in your case c:\users\yourname then you can simply run

bash -l /opt/sagemath-8.6/sage 'example.sagetex.sage'

IT SHOULD WORK ! and you will find

and a folder called
sage-plots-for-example.tex (with 15 items)

all these need to be moved to wherever you wish to second run pdfLaTeX i.e. where your original aux and pdf files are after second run you get the expected results.

Final Cut

To avoid all that messing about we need a minimal script as below, call it any name you like but I called it sage.cmd and placed it in the bin directory D:\Programme\SageMath 8.6\runtime\bin.

The PATH is the ONLY exception to the rule of NO spaces The following script will not work if your .tex is down a path with spaces, Be Warned

Save as Sage.cmd

:: IF this directory is already on path you don't need the next line
@set path=D:\Programme\SageMath 8.6\runtime\bin;%path%
:: This next line is essential to temporarily correct the directory misdirection
:: but has the side effect of rebuilding a temporary dot sage subdirectory
@set HOME=%~dp1
:: This next line does the donkey work
bash -l /opt/sagemath-8.6/sage '%~n1.sagetex.sage'
:: the following command is optional if you want to delete .sage then remove the ::
:: rmdir /S /Q %~dp1.sage

Inside TeXstudio add a user command to the file (you could also add the pdfLaTeX run before and after)

"D:/Programme/SageMath 8.6/runtime/bin/sage.cmd" "?am" 

enter image description here

I forgot to mention that if you now have sage.cmd as suggested within sage...\bin on system path then the command for the editor can be simply sage "?am" (at worst sage.cmd "?am")

Also you could have a desktop shortcut pointing to the sage.cmd with a "%1" at the end it will allow you to drag and drop a filename.sagetex.sage file for processing

If you want a .cmd or .bat (they are basically both the same) file to automatically run a tex file through all compiles you can have for example one called say SageRunner.cmd (or.bat)

PdfLaTeX your usual options (e.g. synctex) "%1"
Sage "%~dpn1"
PdfLaTeX your usual options "%1"

That should work if you simply drag and drop the filename.tex onto it

I have used "..." in most commands to allow for spaces in the "file name" however there are some places that the call via BaSh (Bourne Shell) would stumble so to make it the simplest (dirty) solution I had to forgo scripting for all drive switching and associated filename syntax checking.

| improve this answer | |
  • thanks a lot for your hard work. I am still not understanding some of the details but I hope that I will do if I have more time to look at it. First of all: The solution works. Is there any possibility to get it to work if the file path contains spaces? That would be nice, but is not a must have. Maybe only partially related: what is the difference in calling .cmd files or calling .bat files from TeXStudio? Could you explain some more what the .cmd file does (if thats possible without having to much knowledge) – Martin Feb 20 '19 at 20:03
  • Glad to be of service , and I learnt a lot attempting hundreds of lines of convoluted workarounds just to find Keep It Simple is best – user170109 Feb 20 '19 at 20:05
  • I edited my comment (but I think you were too fast). If you have time it would be really great if you could answer the questions. – Martin Feb 20 '19 at 20:31
  • The cmd bypasses minimal terminal telli type (MinTTY) and calls the Cygwin version of the Bourne Shell which uses the POSIX environment to find the *nix version sage command which runs within the dedicated Python (version 2.7) – user170109 Feb 20 '19 at 20:46
  • 1
    @Sebastiano decades of practice :-) and millions of errors, many of my making :-) – user170109 Feb 20 '19 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.