Sometimes we need to perform computations (symbolic, numerical, …) involving mathematical expression that we have already typeset in TeX. This package provides commands that simplify doing. It is essentially an Emacs frontend for latex2sympy, adapted to work with SAGE, with some bug fixes and convenience features thrown in. There is a related VS Code plugin.
The main entry point is the command symtex-dwim, bound to C-c v by default. This command evaluates a SAGE expression (in a SAGE session, which we can access separately via the *Sage* buffer) and stores the result of that evaluation, converted to TeX, in the kill-ring, from where we are free to yank it into our TeX buffer.
If we run symtex-dwim with an active region, then the contents of that region, converted from TeX, are stored in the SAGE object expr, which we can use in our own expression. For example, we can cube a matrix by highlighting its TeX code, running symtex-dwim, and entering expr^3 in the minibuffer:
(The live previewing is via the package czm-preview.)
When the region is inactive, the command symtex-dwim behaves similarly, except that expr is not set to anything before our SAGE expression is evaluated.
If we run symtex-dwim with an active region and a prefix argument (C-u C-c v by default), then we evaluate a “default” expression, which by default is essentially expr.expand() (see the customization variable symtex-expand-expression). Here’s an example where we use this feature to multiply some matrices.
A common workflow for me is to assign some of my TeX expressions to SAGE variables. I do this by highlighting those expressions, running symtex-dwim, and entering, for instance, a = expr, which assigns the highlighted expression to the variable a. I then perform some calculations directly in the *Sage* buffer. When I’ve produced some result that I want to put back in my TeX buffer, I call symtex-dwim with an inactive region.
An alternative approach would be to use ob-sagemath in an org-mode buffer, which can naturally combine SAGE code and TeX, but I often find it more convenient to work in a TeX buffer, so that I have direct access to AUCTeX and to packages that build upon it. This approach is also collaborator-friendly, since more mathematicians are familiar with TeX than with org.
This package requires ob-sagemath, sage-shell-mode and czm-tex-util.el, so install those first.
Download this repository, install using M-x package-install-file (or package-vc-install, straight, elpaca, …), and add something like the following to your init file:
(use-package symtex
:bind
(:map LaTeX-mode-map
("C-c v" . symtex-dwim)))Modify suitably, replacing C-c v with your preferred bind.
I use elpaca, with the following in my init file:
(use-package symtex
:elpaca (:host github
:repo "ultronozm/symtex.el"
:files ("*.el" "*.py")
:depth nil)
:after latex
:bind
(:map global-map
("C-c V" . symtex-process))
(:map LaTeX-mode-map
("C-c v" . symtex-dwim)))- If you haven’t already launched a
*Sage*buffer and executed some commands in that, thensymtex-dwimtypically doesn’t work correctly the first time it has been called, due to some deprecation warnings. Sorry! Just try it again and it should work from then on. - This package relies on converting objects back and forth between SymPy and Sage, which introduces issues now and then. It can help to play around with the SAGE member functions
._sage_()and._sympy_(); see for instance the horrendous contents of the customization variablesymtex-sympy2latex-expr.
This package uses Python code adapted from latex2sympy2.

