Prelude
Recently, while browsing the
LaTeX blog, I came across an interesting article called
Mathematical Computing and Graphs in LyX using Sage . A thought immediately appeared in my head: “Finally, there was a reason
to throw off the shackles of MS Office to stop using Word for what it is not intended for — not to engage in idiotic copy-paste and re-saving a heap of graphs with the slightest changes, because Sage will do everything for me!”. LyX was immediately installed, the Sage lineup came. But ... as it turned out, the native version of Sage under Windows really does not exist. There is only an abandoned half-working port, which I never managed to get to work, and a terribly raw version of Sage under CygWin, which, it seems, only knows how to run.
A little depressed, I decided to try the option offered on
the download page - use a virtual machine.
Despite the assurances that "it’s unencrypted.", It was unclear how to get LyX to communicate with the Sage installed somewhere, far away. on a virtual machine. But there is a way out ™, and it turned out, oddly enough, in the documentation for SageTex - to use the Sage running in the virtual machine via the web API.
Recipe
Training
First of all, LyX will need some kind of LaTeX distribution. I will not elaborate on this, I note only that I used
MikTeX . How to install and configure it, well described
here .
')
Now, of course, download and install
LyX . We also download an
image of a virtual machine with Sage . To work with the image it is proposed to use
VirtualBox or free
VMWare Player .
The script communicating with Sage is written in Python, which also needs to be
downloaded and installed.
The script itself can be downloaded, for example,
from here .
Action
- Create a folder somewhere on the screw where we’ll drop everything you need - the Sage image and remote-sagetex.py, and also add this folder to the system’s PATH environment variable (this is where LyX will look for the script)
- Processing of the Sage-code inside the LaTeX-document takes place in three stages - after the first pass of latex.exe, a file with the extension .sage is generated from the source .tex file. This file is fed by Sage, which will give us a .sout file containing the actual results of the calculations. At the second launch, latex.exe will pick up these values ​​and paste them into the document.
- Unfortunately, LyX has no idea about Sage. Well, teach him. Create everything in the same folder as the bat-file. Let's call it ps2pdf_sage.bat and give it the following content:
::
latex %~n1.tex
:: .sage, .
:: Sage Notebook
remote-sagetex.py -s localhost:8000 -u admin -p sage %~n1.sage
:: - Sage LaTeX
latex %~n1.tex
:: PDF
dvips %~n1.dvi
ps2pdf %~n1.ps
- Now we need to teach LyX to use our script to generate PDF. To do this, open LyX, go to Tools → Settings ... → File Processing → Converters → and add a new LaTex handler -> PDF (ps2pdf), something like this

Why ps2pdf, not pdflatex? For some unknown reason, the graphs obtained using Sage refuse to be displayed in the resulting PDF in any form. If anyone knows what the problem is, I will be grateful for the decision.
- All is ready. It remains only to add
\usepackage{sagetex}
to the document preamble (MikTex, if necessary, downloads this package from the repository).
A simple example: the
source and the
finished PDF .
PS The topic was written by hacker ZimM and published at his request.