-
Notifications
You must be signed in to change notification settings - Fork 287
Previewing TeX in Macaulay2 Emacs buffers
Automatic preview means that whenever the output is a latex fragment, it will automatically be compiled and rendered. For example:
imageTo enable this feature, copy the emacs code below to your .enacs or init.el. Note that it requires
-
The
texfragEmacs package, which is for example installable in Emacs viaM-x package-install RET texfrag. If you are unsure whethertexfragis already installed, runM-x list-packagesand check the installed packages listed at the very bottom. -
A functioning
auctexsetup in Emacs, meaning you can compile latex documents with Emacs. -
The
dvipngsystem executable, which is for example installable via:- on Debian / Ubuntu:
sudo apt install dvipng - on Fedora:
sudo dnf install texlive-dvipng - on MacOS:
dvipngis bundled with most TeX distributions
If you are unsure whether
dvipngis already installed, rundvipng --versionin your terminal and check the output. - on Debian / Ubuntu:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Automatic LaTeX Rendering for Macaulay2 Buffers ;; Requires: 'texfrag' package (M-x package-install RET texfrag) ;; 'dvipng' system executable ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (with-eval-after-load 'M2 ; Change 'M2 to your package's actual feature name if needed (defun M2--texfrag-standalone-header () "Generate a wide article LaTeX preamble for Macaulay2 latex fragments." (concat "\\documentclass{article}\n" "\\setlength{\\textwidth}{10000pt}\n" "\\usepackage{amsmath,amsfonts}\n" "\\usepackage[utf8]{inputenc}\n" "\\usepackage[T1]{fontenc}\n")) (defun M2--texfrag-comint () "Internal texfrag setup for `M2-comint-mode'." (when (bound-and-true-p texfrag-mode) (setq-local texfrag-comments-only nil) (setq-local texfrag-frag-alist '(("\\$" "\\$" "$" "$"))) (setq-local texfrag-header-function #'M2--texfrag-standalone-header))) (defmacro M2--with-texfrag-preview-settings (&rest body) "Execute BODY with AUCTeX preview settings strictly bound to DVI/PNG." `(let ((preview-image-type 'dvi*) (preview-dvi*-image-type 'png) (preview-LaTeX-command-replacements '(preview-LaTeX-disable-pdfoutput)) (preview-dvipng-command "dvipng -picky -noghostscript -o %m/prev%%03d.png %d") (TeX-PDF-mode nil) (TeX-master t)) ,@body)) (defun M2--texfrag-auto-render-output (_string) "Scan newly arrived comint output line-by-line and compile LaTeX fragments." (when (and (bound-and-true-p texfrag-mode) (boundp 'comint-last-output-start) comint-last-output-start) (let ((start comint-last-output-start) (end (point-max))) (save-excursion (goto-char start) (while (re-search-forward "^[ \t]*o[0-9]+[ \t]*=[ \t]*\\$\\(?:.\\|\n\\)*?\\$[ \t]*$" end t) (let ((match-start (match-beginning 0)) (match-end (match-end 0))) (M2--with-texfrag-preview-settings (if (fboundp 'texfrag-region) (texfrag-region match-start match-end) (when (fboundp 'preview-region) (preview-region match-start match-end)))))))))) (define-minor-mode M2-texfrag-auto-mode "Minor mode to auto-compile and preview LaTeX fragments in Macaulay2 shells." :init-value nil :lighter " M2-LaTeX" (if M2-texfrag-auto-mode (cond ((not (require 'texfrag nil t)) (setq M2-texfrag-auto-mode nil) (user-error "Package 'texfrag' is required. Please run: M-x package-install RET texfrag")) ((not (executable-find "dvipng")) (setq M2-texfrag-auto-mode nil) (user-error "Executable 'dvipng' not found on your system PATH")) (t (when (boundp 'texfrag-setup-alist) (add-to-list 'texfrag-setup-alist (list #'M2--texfrag-comint #'M2-comint-mode))) (unless (bound-and-true-p texfrag-mode) (texfrag-mode 1)) (add-hook 'comint-output-filter-functions #'M2--texfrag-auto-render-output nil t) (M2--with-texfrag-preview-settings (if (fboundp 'texfrag-document) (texfrag-document) (when (fboundp 'preview-buffer) (preview-buffer)))))) (remove-hook 'comint-output-filter-functions #'M2--texfrag-auto-render-output t) (when (fboundp 'preview-clearout-buffer) (preview-clearout-buffer)))) ;; Automatically activate the mode whenever a Macaulay2 shell buffer starts (add-hook 'M2-comint-mode-hook #'M2-texfrag-auto-mode))
Using the texfrag Emacs package, it is possible to preview TeX code in Macaulay2 interaction buffers in a way similar to the preview-latex mode that comes with AUCTeX.
After installing texfrag (which is available through MELPA), add the following to your .emacs or .init.el:
(require 'texfrag) (defun texfrag-M2-comint () "Texfrag setup for `M2-comint-mode'." (when texfrag-mode (setq texfrag-comments-only nil texfrag-frag-alist '(("\\$" "\\$" "$" "$"))))) (push (list #'texfrag-M2-comint #'M2-comint-mode) texfrag-setup-alist)
Then, when in an M2 interaction window buffer, start texfrag using M-x texfrag-mode. Alternatively, you can set texfrag to start automatically in Macaulay2 interaction buffers by adding the following to your configuration:
(add-hook 'M2-comint-mode-hook #'texfrag-mode)
As an example, consider the following session:
image
Now suppose we run M-x preview-at-point (or its key binding, C-c C-p C-p). Then we get the following:
image