Ask Your Question

formatting an output as a numbered equation

asked 2022-01-01 14:47:22 +0100

moon gravatar image

Hi there, As by now and after googling for some time I didn't manage to find a option on how to format my dagemath outpiuts as numbered equation. Consider this very basic example:

ex1=latex(r"\noindent Consider this  equation : ")
ex2='$$'+latex(a*x^2 + b*x +c ==0) +'$$'
ex3=latex("Solution : ")
ex1 + ex2 + ex3 +ex4

I just need the equation to be included in an "equation" environment. One can see from my print that I'm at the very beginning stage in learning. Thx in advance for the help.

edit retag flag offensive close merge delete


Welcome to Ask Sage! Thank you for your question.

slelievre gravatar imageslelievre ( 2022-01-02 05:37:47 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2022-01-02 11:49:41 +0100

Emmanuel Charpentier gravatar image

Equation numbering (and, more generally, (cross-)references management) is not in the scope of Sage.

What you are looking for is a document preparation system able to include and manage Sage inputs, outputs and references to them.

One solution is Sage's SageTeX, which offers the possibility of interleaving the source of a Sage program and its results (textual, LaTeX and/or figures) with the text describing it, by post-processing the result of a first LaTeX run with a tool executing Sage snippets and inserting their source and/or results in the LaTeX source for (re-)processing. This solution, part of the standard Sage distributions, is the best for seasoned LaTeX users aiming at the preparation PDF/printed papers or presentations with their usual tools.

I have also had some success with PythonTeX, which allows for the use of several languages (notably including R) the same way.

The problem is more complex when aiming at preparing other types of documents, such as (X)HTML pages or (shudder...) Word or Powerpoint files (a necessity outside scientific academia...) :

  • One possibility is to prepare LaTeX files and convert them to your desired output, for example with pandoc, which as an astonishing range of output formats.

  • Another one is to use a document preparation system able to generate your desired output(there are legions) and pre- or post-process it with a SageTeX-like tool (those are rare...). I have had some success with :

    • codebraid, which uses markdown for text input and pandoc as the processor ; supports a handful of programming languages (including R), generates anything pandoc can generate ;

    • org-mode's babel tool, which allows the insertion and execution of code written in about 70 programming languages and tools, and generate a large range of outputs (notably thanks to a clever use of pandoc in some cases) ; interestingly, this tool is a package of the (remarkable) emacs text editor, which might be considered as a bug or as a feature, according to your taste...

  • A third possibility is to use a system supporting Pythoncode and feeding it preprocessed input, calling from sage.all import * early in the program code. The number of text preparation system supporting Python is larger than than the number of systems supporting Sage directly ; I am thinking of knitr, rmarkdown and derived systems, aiming at supporting R but able to support Python.

  • It could be worthy to look for such tools for processing Jupyter notebooks : there exists an not-so-small cottage industry of Jupyter extensions, some of them aiming at extending its markdown abilities...

I'm pretty certain that other tools, text formats and supported code languages are available somewhere on the 'Net ; what I mentioned is what I tried with at least some success. FWIW, I have used LaTeX for 35 years, but the limitations of PDF-only output made me look at other solutions ; it turns out that my current personal favorite is org-mode, probably because I have been an emacs user for 35 years...


edit flag offensive delete link more


Very nice and informative answer! There is also the GNU Texmacs. It can run sage, R, python, maxima and a bunch of other CAS integrated in an interesting WYSIWYG text format. I don't know exactly why, but it is a very underestimated software.

cav_rt gravatar imagecav_rt ( 2022-01-02 19:56:05 +0100 )edit

In addition to SageTeX, pythonTeX and TeXmacs, there is also

slelievre gravatar imageslelievre ( 2022-01-03 02:25:30 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2022-01-01 14:47:22 +0100

Seen: 166 times

Last updated: Jan 02 '22