This package makes it easy to operate symbolically on TeX code using SAGE. The main entry point is the command symtex-dwim, bound to C-c v by default. This command evaluates a SAGE expression and stores the result of that evaluation, converted to TeX, in the kill-ring. If we run symtex-dwim with an active region, then the contents of that region are parsed are stored in the SAGE object expr, which we can reference in our 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 preview-auto.)
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.
The SAGE expressions are evaluated in an active SAGE session.
Emacs Calc gives a built-in way to do something in the direction of this package, but sometimes one wants to use features of SAGE that are not found in Calc. Another 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.
There is a related VS Code plugin.
This package requires sage-shell-mode (or sage-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
:ensure (:host github :repo "ultronozm/symtex.el" :depth nil)
:after latex
:bind
(:map global-map ("C-c V" . symtex-process))
(:map LaTeX-mode-map ("C-c v" . symtex-dwim)))Before using symtex commands, you need to have a running Sage process in a buffer. You can create this buffer in one of two ways:
- If using sage-shell-mode: Run
M-x sage-shell:run-sageto start a Sage process. - If using sage-mode: Run
M-x sage-runto start a Sage process.
By default, symtex prefers a buffer named *Sage*. Some versions of sage-mode instead create *Python*. You do not need to rename that buffer: if there is exactly one live Sage process, symtex detects and uses it automatically.
If you have multiple Sage processes, or prefer to use a specific process buffer, customize symtex-process-buffer:
(setq symtex-process-buffer "*My-Custom-Sage-Buffer*")The usual workflow is:
- Start Sage with one of the commands above.
- In a TeX buffer, select a math expression and run
M-x symtex-dwim(orC-c vwith the sample binding). Enter a Sage expression involvingexpr, such asfactor(expr)orexpr^3. - Yank the TeX result from the kill ring with
C-y.
With an active region, C-u C-c v runs symtex-expand-expression without prompting. With no active region, C-c v evaluates the Sage expression you enter directly, which is useful for retrieving a value computed in the shared Sage session.
- This package temporarily uses override advice while parsing to compensate for a few Emacs calc bugs related to TeX parsing. Patches have been submitted upstream (https://lists.gnu.org/archive/html/bug-gnu-emacs/2024-01/msg01776.html, https://lists.gnu.org/archive/html/bug-gnu-emacs/2024-01/msg01777.html) that should eventually make such override advice unnecessary.

