Emacs-lisp: prettify-symbols-mode for LaTeX
Asked Answered
T

2

5

I was trying to port over the "pretty entities" behaviour from org-mode to latex-mode using the Emacs builtin prettify-symbols-mode. This mode uses font-lock-mode to display character sequences in a buffer as a single (unicode) character. By default for instance emacs-lisp code

(lambda () t)

becomes

(λ () t)

It does however seem to require the character sequences to be separated by some characters, e.g. white-spaces. For instance in my setup, the replacement

\alpha \beta -> α β`

will work, but it will fail when the strings are not separated, e.g.

\alpha\beta -> \alphaβ

This is an issue specifically, because I wanted to use this prettification to make quantum mechanical equations more readable, where I e.g. the replacement like

|\psi\rangle -> |ψ⟩

Is it possible to avoid this delimiter-issue using prettify-symbols-mode? And if it is not, is it possible by using font-lock-mode on a lower level?

Thrill answered 8/4, 2014 at 12:42 Comment(0)
C
6

Here's the code that should do what you want:

(defvar pretty-alist
  (cl-pairlis '("alpha" "beta" "gamma" "delta" "epsilon" "zeta" "eta"
                "theta" "iota" "kappa" "lambda" "mu" "nu" "xi"
                "omicron" "pi" "rho" "sigma_final" "sigma" "tau"
                "upsilon" "phi" "chi" "psi" "omega")
              (mapcar
               (lambda (x) (make-char 'greek-iso8859-7 x))
               (number-sequence 97 121))))
(add-to-list 'pretty-alist '("rangle" . ?\⟩))
(defun pretty-things ()
  (mapc
   (lambda (x)
     (let ((word (car x))
           (char (cdr x)))
       (font-lock-add-keywords
        nil
        `((,(concat "\\(^\\|[^a-zA-Z0-9]\\)\\(" word "\\)[a-zA-Z]")
            (0 (progn
                 (decompose-region (match-beginning 2) (match-end 2))
                 nil)))))
       (font-lock-add-keywords
        nil
        `((,(concat "\\(^\\|[^a-zA-Z0-9]\\)\\(" word "\\)[^a-zA-Z]")
            (0 (progn
                 (compose-region (1- (match-beginning 2)) (match-end 2)
                  ,char)
                 nil)))))))
   pretty-alist))

As you can see above, pretty-alist starts out with greek chars. Then I add \rangle just to demonstrate how to add new things. To enable it automatically, add it to the hook:

(add-hook 'LaTeX-mode-hook 'pretty-things)

I used the code from here as a starting point, you can look there for a reference.

Calibrate answered 8/4, 2014 at 13:20 Comment(3)
Thanks, I'll be trying ot as soon as possible.Thrill
Any idea how I can add Σ to the options list for sum. sigma shows this 'ς' instead?Etiquette
Just follow the example with rangle.Calibrate
I
2

The code of prettify-symbols-mode derives from code developped for languages like Haskell and a few others, which don't use something like TeX's \. So you may indeed be in trouble. I suggest you M-x report-emacs-bug requesting prettify-symbol-mode be improved to support TeX-style syntax.

In the mean time, you'll have to "do it by hand" along the lines of what abo-abo suggests.

One note, tho: back in the days of Emacs-21, I ported X-Symbol to work on Emacs, specifically because I wanted to see such pretty things in LaTeX. Yet, I discovered that it was mostly useless to me. And I think it's even more the case now. Here's why:

  • You can just use an actual ψ character in your LaTeX code instead of \psi nowadays. So you don't need display tricks for it to look "right".
  • Rather than repeating |\psi\rangle I much prefer defining macros and then use \Qr{\Vangle} (where \Vangle turns into \psi ("V" stands for "metaVariable"), and \Qr wraps it in a braket) so I can easily tweak those macros and know that the document will stay consistent. At that point, pretty-display of \psi and \rangle is of no importance.
Insole answered 8/4, 2014 at 13:51 Comment(2)
I was actually thinking of extending my code to allow more complex definitions, e.g. by writing a comment that declares how to text-replace a certain macro. Never followed up on that though, because I switched to LyX (where graphical display of user-defined math-macros is included). That said, I found such macros -- without a graphical representation as in LyX -- to kill my own ability to later understand the source code if I ever come back to edit the file again, so I don't want to know how bad that would get when reading someone else's TeX.Thrill
As for ψ directly in the source: While I love the TeX input method, reasonable results will be obtained only when using LuaTeX or XeTeX, which is limiting (though honestly I'd be greatful if all publications came with proper unicode symbols). When using PDFLaTeX with \usepackage[utf8(x)]{inputenc} sadly unrecognized symbols are silently ignored rather than raising an error. PS: Sorry for the belated reply.Thrill

© 2022 - 2024 — McMap. All rights reserved.