6

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.

Martin
  • 511
  • 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 02 '19 at 04: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 02 '19 at 09: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 02 '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 02 '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 02 '19 at 22:00
  • If I write the commands without -sh I get (for a very brief time) the message python2: can't open file 'sage': [Errno 2] No such file or directory (and the sage title is not visible in the console so it seems sage does not even get started). – Martin Feb 03 '19 at 19:20
  • Been ragging away at this off and on it is a nightmare of onion skins since windows is wrapped around Cygwin is wrapped around python is wrapped around sage is running on emulated posix variables. It should be do able but having to work through so many differences as ;=: =/ c: = drive/c and links are handled different etc etc however although not yet converted the sage file via CLI have on occasion got sage to run via a hybrid posix/win terminal as half expected, not much help I know but im not giving up yet –  Feb 15 '19 at 04:46
  • If you can't get an answer, consider posting here – DJP Feb 15 '19 at 18:08
  • @DJP you are correct to give that as the location for an answer and this same question has got the same answer since 2011 i.e. run pdfLaTeX in windows (e.g. from commandline or TeXstudio, then invoke a command line in minty/sage to do the manual translation, then switch back to windows for 2nd pdflatex run) that is as I understand it the "official answer" and what many people finaly accept as the only way. However there are a few that have found it is possible to either from a windows command line OR a windows editor be able to pass a source tex location into posix path, sh into sage & return. –  Feb 16 '19 at 16:07
  • Martin I seem to be able to get the command line working to a degree by storing and referencing specific locations and have built a testing and logging wrapper to be able to trace failings in different user solutions but am still not getting a valid output due to version mismatch errors (need to have same version of .sty and sage components so now working on that angle) however it would help to see your full path to mintty and which tex (MiKTeX / live) are you using and is it "normal" or customised (e.g. portable). –  Feb 17 '19 at 05:44
  • @KJO: I am using MiKTeX (not customized), the full path to mintty is D:\Programme\SageMath 8.6\runtime\bin\mintty.exe – Martin Feb 17 '19 at 21:32
  • Ok have sorted what,s happening with versions but next wrinkle Is results in wrong location I think I messed up my installation with thousands of tests moving to different locations so am going to reset to similar to yours, any reason progs on D: rather than more normal C: ? and is sagemath package loaded in MiKTeX (that is where the version mismatch occurs 8.6=3.0 MiKTeX=3.2) –  Feb 17 '19 at 22:39
  • Just to explain why its difficult What should work is D:\Programme\SageMath 8.6 bin\minty /bin/bash -l -c /opt/sagemath-8.6/sage "/cygdrive/c/path to folder/testme.sagetex.sage" however scripting around that builds the output files in my user directory even after a reinstall –  Feb 18 '19 at 05:20
  • @KJO I use ´D´ for all programs except of windows. And yes, the sagemath package is loaded in MikTeX with the correct version (I think otherwise the compilation using pdflatex, opening the sage console and using pdflatex again should not work). – Martin Feb 18 '19 at 18:20

1 Answers1

7

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

example.sagetex.scmd
example.sagetex.sout
example.sagetex.sage.py
E2.sobj
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.

  • 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 –  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) –  Feb 20 '19 at 20:46
  • sorry had to remove .sagetex.sage from the extra sagerunner.cmd since I forgot it should already be allowed for DUH! –  Feb 20 '19 at 21:09
  • You are excellent user with the editors and the errors. :-) – Sebastiano Feb 20 '19 at 21:12
  • 1
    @Sebastiano decades of practice :-) and millions of errors, many of my making :-) –  Feb 20 '19 at 21:13
  • Great solution! Worked for me even with a path containing spaces using double quotes in the custom user command: "sage.cmd" ""?am"" – Darío Oct 13 '21 at 11:24