Convert LaTeX -> SymPy -> evaluate -> LaTeX in Python3 and GNU Emacs.
- Install Python3 (tested using
v3.11.2). - Install SymPy (tested using
v1.11.1). - The Python3 package
antlr4-python3-runtime. Please follow the installation instructions here. The version ofantlr4-python3-runtimerecommended on this webpage might differ from your requirement: for example, I needed versionv4.10forSymPy v1.11.1. Please run the following Python code before installingantlr4-python3-runtimeto determine the desired version.>>> from sympy.parsing.latex import parse_latex >>> parse_latex("\int")
- Optional: Install GNU Emacs.
- Clone this repository.
- Copy the file
computex.pyto some directory that is in thePATHenvironment variable of your Operating system.
Run computex.py -h to get help.
Create a file named matsq.tex with the following contents:
\begin{pmatrix}
a & b \\
c & d \\
\end{pmatrix}^2and another one named matmult.tex with the following contents:
\begin{pmatrix}
a & b \\
c & d \\
\end{pmatrix}\begin{pmatrix}
e & f \\
g & h \\
\end{pmatrix}Now, try the following commands in a UNIX shell:
$ cat matsq.tex | computex.py
$ cat matmult.tex | computex.pyTry these to print the output in equation form:
$ cat matsq.tex | computex.py -e
$ cat matmult.tex | computex.py -eIf you are using Windows and do not have cat, then try using the type command instead.
This step is optional.
- If you wish the key bindings to be available in all buffers, then add
the following to your emacs Initialization File:
If you are using emacs 29 or above, then consider using
(setq computex-file "/path/to/computex.el") (if (file-exists-p computex-file) (progn (load computex-file) (global-set-key (kbd "C-`") sgang-computex-map)))
keymap-global-setinstead ofglobal-set-key. - If you wish the key bindings to be available only in
latex-modeof AUCTeX, then add the following to your emacs Initialization File instead:(setq computex-file "/path/to/computex.el") (if (file-exists-p computex-file) (progn (load computex-file) (eval-after-load 'latex '(define-key LaTeX-mode-map (kbd "C-`") sgang-computex-map))))
- Optional: Change the
"C-`"above to any key you wish.
- Select the LaTeX code to evaluate (excluding any
$-sign or other math delimiters). - Call any of the functions
sgang-tex-computex-*defined incomputex.el. Assuming thatC-`is bound tosgang-computex-map, typeC-`followed by:- the key
zforsgang-tex-computexcorresponding tocomputex.py -e, - the key
eforsgang-tex-computex-expandcorresponding tocomputex.py -e -E, - the key
fforsgang-tex-compute-factorizecorresponding tocomputex.py -e -f, - the key
sforsgang-tex-compute-simplifycorresponding tocomputex.py -e -s.
- the key
This idea was borrowed from symtex.el authored by @ultronozm.
