📜 ⬆️ ⬇️

Mathematical calculations and graphs in LyX using Sage

LyX is a WYSIWYM document processor that works seamlessly with LaTeX. That is, the user creates * .lyx files in this processor, from which * .tex is then created and compiled.
Sage is a computer algebra system that works transparently with other math packages. The main interface is the command line, using all the variety of python capabilities. Also available is a web-based interface that can be tested on the developers website.
The joint use of these two developments opens up interesting possibilities. For example, you can create reports in which calculations, graphs and analytical calculations will be performed automatically, almost as if in some kind of, but with a clear LyX structure, python flexibility and LaTeX layout capabilities.



Installation


LyX is in almost any modern distribution, so it installs in the usual way. It is written in Qt, there is a port under Windows. LyX will pull LaTeX.
With Sage, it's not that simple. He is now only under linux, in distributions, if presented, then in a not particularly efficient form. Developers recommend running under Windows using a virtual machine.
Sage developers suggest downloading the build for your architecture (386 or amd64) and distribution (Ubuntu and Fedora, but I think it will work in the rest). The assembly contains, in addition to sage-modules, python, maxima, gap, and much more. You download the archive, unpack it into a folder and immediately start sage or sage-notebook - the server for the web interface from it. It also includes the style for LaTeX, which can always be downloaded separately.
Let all this is not particularly compatible with package managers, but quite convenient.
I will assume that you unpacked the archive into the /usr/local/sage directory.

Training


Now that LyX and Sage are installed, you can begin to combine them.
This will require:
  1. Customize LaTeX.
  2. Write a script.
  3. Customize LyX.

1. Configure LaTeX

The sagetex style mentioned above should be visible to LaTeX. In the style manual it is advised to put it in the ~/texmf/tex/genetic folder. Under ubuntu, apparently, you need to put in ~/.local/share/texmf/tex/genetic
It worked for me (so far without LyX) like this:
 $ sudo ln -s /usr/local/sage/local/share/texmf/tex/generic/sagetex/ /usr/local/share/texmf/tex/generic/sagetex/ 
and then
 $ sudo texhash /usr/local/share/texmf/tex/generic/sagetex/ # ,  ,       

By the way, speaking, TexLive, which is set by default in ubunt, contains the sagetex style. But there is a problem with versions - sage and sagetex of different versions are not friendly with each other, so it is important that LaTeX deal with the new sagetex.sty.
You can find out which file LaTeX will take if you say \usepackage{sagetex} to it, like this:
 $ kpsewhich sagetex.sty 

This style implies the following sequence of assembling documents that use it:
 $ latex tex_with_sage.tex #     sage tex_with_sage.sage $ sage tex_with_sage.sage #      #     ,    $ latex tex_with_sage.tex #   
$ latex tex_with_sage.tex # sage tex_with_sage.sage $ sage tex_with_sage.sage # # , $ latex tex_with_sage.tex #

LyX, of course, does not know how to run sage. But he knows how to work with noweb. What it is now is not even important, we just need to have the file noweb.sty in the system. You can install the noweb package, or you can simply create this file.
2. Script

As soon as LyX starts collecting the final file (for example, pdf), it generates a * .tex file in the temporary directory and runs the usual commands (pdflatex, makeindex, etc.). Then it launches the final * .pdf or * .dvi file in the viewer or copies this file to the folder with the original document.
If the document type is noweb, a * .nv file will be generated (essentially the same * .tex), a special program will be launched for this file, which creates * .tex, after which pdflatex, makeindex and their ilk will be used .
As this program, we’ll give LyX such a simple script:
 #!/bin/bash name=`basename $1 .nw` # uncomment and replace path if you have problems with # old version of sagetex.sty in texlive # ln -sf /usr/local/share/texmf/tex/generic/sagetex/sagetex.sty sagetex.sty # generating *.sage file latex $1 > $name.sage.log 2>&1 echo "" >> $name.sage.log # generating *.sout file /usr/local/sage/sage $name.sage >> $name.sage.log 2>&1 ln -sf $1 $2 # now LyX can run latex in usual way 
#!/bin/bash name=`basename $1 .nw` # uncomment and replace path if you have problems with # old version of sagetex.sty in texlive # ln -sf /usr/local/share/texmf/tex/generic/sagetex/sagetex.sty sagetex.sty # generating *.sage file latex $1 > $name.sage.log 2>&1 echo "" >> $name.sage.log # generating *.sout file /usr/local/sage/sage $name.sage >> $name.sage.log 2>&1 ln -sf $1 $2 # now LyX can run latex in usual way

I put it in $HOME and called sagelyx.sh .

3. Configure LyX

Classes with Noweb must be available in the document settings. If they are not available, you may need to run -> .
Next, you need to tell LyX that you need to run our script for the Noweb documents (see picture).

We add to the preamble of all documents:
 \usepackage{sagetex} 

Using


Now you can enter a formula (or make a TeX-insert) in the document:
 $26^{3}\cdot10^{3}=\sage{26**3+10**3}$ 

')
If the formula preview is enabled, then LyX will replace the \sage command with ?? . This is because only one LaTeX pass is used for the preview. However, if you click the PDF button, a pdf with the numbers entered will open.
A simple example and my script: http://www.box.net/shared/air7v45yqk

What does not work?


Many things don't work ...
Something turns out bad in LyX and you have to do TeX inserts, for example, you don’t really use \sage{} inside the mathematical environment, because LyX removes spaces and inserts braces.
Something cannot be done in sagetex itself, for example, using strings with unicode text. To do this, you need to place the line at the beginning of the sage file.
 # -*- coding: utf-8 -*- 
but no means for this sagetex does not provide.

PS I hope that someday all this will become very, very simple (maybe even thanks to this post), as in cool commercial systems, but flexibility and freedom will remain in all possible ways.

Source: https://habr.com/ru/post/95289/


All Articles