This package provides commands for executing minted SAGE blocks in a TeX buffer and displaying the results in either a verbatim
or align*
block. This is very similar to what one can do in org-mode using source blocks, babel and ob-sagemath, but the present package works directly in a TeX buffer, which I find convenient. I first saw such functionality in the package xenops. This package provides a variant of a subset of that functionality, as a standalone package, tailored to my needs, e.g., with SAGE rather than Python.
This package requires ob-sagemath, sage-shell-mode, AUCTeX and mmm-mode, 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 mmm-mode
:custom
(mmm-global-mode 'maybe)
:config
(face-spec-set 'mmm-default-submode-face
'((((background light)) (:background "#ddffff"))
(((background dark)) (:background "#004444")))
'face-defface-spec))
(use-package czm-tex-mint
:after mmm-mode
:demand t
:custom
(LaTeX-command "latex -shell-escape")
:config
(czm-tex-mint-initialize)
:bind
(:map czm-tex-mint-mode-map
("C-c C-c" . czm-tex-mint-evaluate)
("C-c C-l" . czm-tex-mint-evaluate-latex))
:hook
(mmm-sage-shell:sage-mode-enter . czm-tex-mint-enable)
(mmm-sage-shell:sage-mode-exit . czm-tex-mint-disable))
Customize the binds to your liking.
Note: the LaTeX-command
customization is so that you can compile TeX buffers containing minted
blocks (using \usepackage{minted}
).
In a TeX buffer, activate mmm-mode
. Use the command czm-tex-mint-new-block
to create a new minted sage block. (You might wish to bind this to a key. Alternatively, with my dynexp package, this step is achieved by typing “bms” and hitting space.) This yields something like the following:
\begin{minted}{sage} \end{minted}
Type some sage code in the block, and run czm-tex-mint-evaluate
(C-c C-c
by default). This yields:
\begin{minted}{sage} a=3 b=5 a+b a*b \end{minted} \begin{results*} \begin{verbatim} 8 15 \end{verbatim} \end{results*}
If you instead run czm-tex-mint-evaluate-latex
(C-c C-l
by default), then you get:
\begin{minted}{sage} a=3 b=5 a+b a*b \end{minted} \begin{results*} \begin{align*} 8\\ 15 \end{align*} \end{results*}
If you manually edit things, then you might need to use commands like mmm-parse-buffer
to make sure that mmm-mode knows where all the source blocks are.
- If your minted code block begins with a left-bracket “[” and you use TeX-fold-mode, then part of the code might get folded along with the header. To get around this, you can insert a blank line at the start of your code block.