Mathematical calculations and graphics in LyX using Sage

LyX is a WYSIWYM document processor that transparently works with LaTeX. That is, the user creates in this processor's files *.lyx, which later created and compiled *.tex.
Sage is a computer algebra system, working transparently with other mathematical packages. The primary interface is the command line, it uses all the variety of python features. Also available a web interface that can be tested on the developers site.
Together, these two developments opens up interesting possibilities. For example, you can create reports that calculate, graphs and analytical calculations will be performed automatically, almost like matchade some, but with the evident structure of LyX, the flexibility of python and the possibilities of typesetting in LaTeX.



Installation


LyX is almost any modern distro, so installed the usual way. It is written in Qt, there is a port under Windows. LyX drags LaTeX.
Sage is not so simple. It is now only for linux distributions, if presented, is not particularly healthy. Start under Windows the developers suggest using a virtual machine.
The developers offer Sage download build for your architecture (386 or amd64) and distribution (Ubuntu and Fedora, but I think it will work in others). The Assembly contains, in addition to sage-modules, python, maxima, gap, and much more. Download the archive, unpack to some folder and run it from sage or sage-notebook — server for the web interface. Also it includes style for LaTeX, which is always possible download separately.
It's not particularly compatible with the package Manager, but comfortable enough.
I will assume that you unpacked the archive into the directory /usr/local/sage.

Preparation


Now, when you installed LyX and Sage, you can proceed with the consolidation.
This will require:
    the
  1. to Set LaTeX.
  2. the
  3. to Write the script.
  4. the
  5. Configure LyX.

1. Configuring LaTeX

The sagetex style mentioned above should be visible to Latex. In the manual to the style suggest to put it in the folder ~/texmf/tex/genetic. Under Ubuntu probably need to be put in ~/.local/share/texmf/tex/genetic
I have worked (still without LyX):
$ 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/ # well, or the path where you just put the style

By the way, TexLive, which is placed by default in Ubuntu, contains the style of sagetex. But there is a problem with versions of sage and sagetex different versions with each other not compatible, so it is important that the LaTeX had to deal with a new sagetex.sty.
To find out which file takes up the LaTeX, if you say \usepackage{sagetex},
$ kpsewhich sagetex.sty

This style involves the following sequence of Assembly of documents, use this:
$ latex tex_with_sage.tex # generates a file with the commands sage tex_with_sage.sage
$ sage tex_with_sage.sage # generated file system responses
# build the index and other commands that you need
$ latex tex_with_sage.tex # get result


LyX, of course, is not able to run sage. But he knows how to work with noweb. That is now not even important, we just need to be the file noweb.sty. You can set the noweb package, but you can just create the file.
2. Script

Once LyX is starting to gather the output file (e.g. pdf), it generates in the temporary directory file *.tex and runs the usual commands (pdflatex, makeindex, etc.). Then he runs in the viewer result file *.pdf or *.dvi or copy this file to the folder with the source document.
As this program, we'll give LyX'such a simple script:
#!/bin/bash

name=`basename $1 .nw`

# uncomment and replace the 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

# LyX can now run latex in the usual way


I put it in $HOME called sagelyx.sh.

3. Configuring LyX

In the document settings should be available to classes with Noweb. If they are unavailable, you may need to run Tools->Reconfigure.
Further, you need to tell LyX'have that for Noweb documents you need to run our script (see figure).

In the preamble of all documents added:
\usepackage{sagetex}

Usage


It is now possible to enter a formula (or make TeX-insert):
$26^{3}\times 10^{3}=\sage{26**3+10**3}$


If you enable the preview formulas, LyX will replace the command \sage to ??. This is because preview uses only one pass of LaTeX. However, if you click View PDF, it opens a pdf stamped with a numbers.
A simple example my script: http://www.box.net/shared/air7v45yqk

What's not working?


Not working a lot...
Something bad turns into LyX and have to make TeX insert, for example does not use \sage{} inside math environment, because LyX removes whitespace and inserts the curly braces.
Something you cannot do in the sagetex example to use strings with uniatowski text. To do this, at the beginning of the sage file to place a line
# -*- coding: utf-8 -*-
but no funds for that sagetex doesn't provide.

PS I Hope that someday all of this will be very, very simple (maybe even through this post) as cool commercial systems, but at the same time remain the flexibility and freedom in all possible meanings.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Tactoom. How about the middle of blogging?

SumIT Weekend of 18-19 February, the idea for iPad and Hackathon

Knowledge base. Part 2. Freebase: make requests to the Google Knowledge Graph