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-runtime
recommended on this webpage might differ from your requirement: for example, I needed versionv4.10
forSymPy v1.11.1
. Please run the following Python code before installingantlr4-python3-runtime
to 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.py
to some directory that is in thePATH
environment 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}^2
and 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.py
Try these to print the output in equation form:
$ cat matsq.tex | computex.py -e
$ cat matmult.tex | computex.py -e
If 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-set
instead ofglobal-set-key
. - If you wish the key bindings to be available only in
latex-mode
of 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
z
forsgang-tex-computex
corresponding tocomputex.py -e
, - the key
e
forsgang-tex-computex-expand
corresponding tocomputex.py -e -E
, - the key
f
forsgang-tex-compute-factorize
corresponding tocomputex.py -e -f
, - the key
s
forsgang-tex-compute-simplify
corresponding tocomputex.py -e -s
.
- the key
This idea was borrowed from symtex.el authored by @ultronozm.