LaTeX Mode and Smartparens
Hi everyone, and first of all: thanks for this awesome tool.
At the moment I am trying to configure smartparens to work with LaTeX-mode and AucTeX. I am using the default smartparens configuration and some key bindings.
To switch to math-mode I do not use dollar signs, but "\( \)" and "\[ \]". I use them, because the Dollar Signs belong to tex rather then to LaTeX. https://tex.stackexchange.com/questions/510/are-and-preferable-to-dollar-signs-for-math-mode https://tex.stackexchange.com/questions/503/why-is-preferable-to
If I for example I tried to to use the trigger "\l(" inside "\( \)" it does not work. Inside "$ $" it does.
Maybe the function sp-in-math-p is not recognizing that I am in math mode, when I am inside the above pairs.
Furthermore, before using smartparens I had the following snippet in my configuration
(add-hook 'LaTeX-mode-hook
(lambda () (set (make-variable-buffer-local 'TeX-electric-math)
(cons "\\(" "\\)"))))
When I typed "$" it automatically inserted "\( \)" the correct delimiters. I found the following issue: https://github.com/Fuco1/smartparens/issues/652
And I tried to use
(sp-local-pair 'latex-mode "\\(" "\\)" :trigger-wrap "$")
instead of the above snippet. Unfortunately, it did not work. Does anyone know how to fix this?
And then there is a more involved problem with the quotation marks.
If you use AucTex and if you include the babel package with a correspoding language option, e.g.
\usepackage[ngerman]{babel}, typing the quote symbol " twice inserts the correct left quotation mark for your language. Typing it again twice at the end the correct right quotation mark for your language is added. It is possible to use auctex to determine the correct pair of quotation marks, which will be entered when typing the quotation mark symbol?
And last but not least, I was wondering about how to use the tags defined in smartparens-latex.el?
Typing "\b{" does not invoke the expected set of "delimiters".
What am I doing wrong?
And I expected the enviromnent pairs \begin{env_name} ... \end{env_name} to be treated as a regular set of pair delimiters.
Environment & version information
-
smartparensversion: 20200322.2051 - Active
major-mode:latex-mode - Smartparens strict mode: nil
- Emacs version (
M-x emacs-version): GNU Emacs 26.3 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.10) of 2019-08-29 - Starterkit/Distribution: plain emacs, no starter kit
- OS: gnu/linux
Thanks a lot, Nils
Hello everyone,
yesterday evening I had some time to further investigate my issues. The first issue was that smartparens does not recognise that I am in math-mode, when I am using "\( \)" or "\[ \]" as delimiters. This issue only seems to be there, when I am in my *scratch* buffer and activate latex-mode. If I open or create a complete LaTeX file, then smartparens recognises, that I am in Math-mode and correctly expands "\l(" . That's fine for me.
The next issue was related to imitate the behaviour of TeX-Electric-Math and to insert "\( \)" when typing a "$" sign. This was already solved in https://github.com/Fuco1/smartparens/issues/652 . At first this solution did not work for me: I could not wrap text in the wished pair of delimiters. The text vanished and even more text around it was deleted.
I luckily stumbled about the following wiki entry about "wrapping": https://github.com/Fuco1/smartparens/wiki/Wrapping I saw that wrapping is related to "cua-mode", so I tried to switch of cua-mode, by commenting out the following lines in my .emacs file
;; (cua-selection-mode t)
;; (setq cua-auto-tabify-rectangles nil)
And it worked. Since I was used to the delete behaviour of cua-mode, I tried to keep it by setting
(delete-selection-mode 1)
But this did not work. I did some research on it and I found the following entry on stackexchange. https://emacs.stackexchange.com/questions/38947/smartparen-latex-with-delete-selection-mode
There was a bug in AucTeX, which should be possible to remedy by the lisp-code posted in the last comment of the above stackechange post:
(put 'TeX-insert-quote 'delete-selection nil)
(put 'TeX-insert-dollar 'delete-selection nil)
A further research showed that this bug was already taken care of: https://emacs.stackexchange.com/questions/21315/delete-selection-mode-and And I checked the tex.el file of the AucTeX (12.2.0) package, and the fix is there. But it does not resolve the issue with cua-selection-mode and delete-selection-mode.
However, reading the following issue https://github.com/Fuco1/smartparens/issues/834 gave me the hint to rebind the "$" sign to the "self-insert-command"
(defun my-smartparens-TeX-mode-hook ()
(local-set-key "$" 'self-insert-command)
(local-set-key "\"" 'self-insert-command))
(add-hook 'LaTeX-mode-hook 'my-smartparens-TeX-mode-hook)
Even though it looks quite hacky and I am wondering if there might be a cleaner solution, it solves the issue for the math-mode delimiters, may it be the "$" sign or "\( \)" delimiters. But for the quotes it does not work. Although the wrapped region is not deleted anymore, it is wrapped in regular double quotes and not in the expected " `` '' " pair of quotes.
Furthermore I observe the same behaviour if I do not call the above function via a LaTex-mode hook, but if I load the babel package with "ngerman" option.
\usepackage[ngerman]{babel}
To summarise almost all issues are resolved, but I am left with the issue of "delete-selection-mode" or "cua-selection-mode" and quotes. I does not yet work. Furthermore loading the babel package with another language than English, seems also to disturb the quotes' typesetting.
P.S.: It seems that more structured tags like "\begin{env} ... \end{env}", mentioned at the end of my previous post, are not supported at the moment. And that's fine for me. And concerning the automatic determination of the quotes pair using the babel package (https://www.gnu.org/software/auctex/manual/auctex/Quotes.html) I can just redefine the quotes pair every time I am writing in another language. That's not too much work.
@nils-schween Hi, I just want to let you know that your efforts are very much appreciated and I will try to read and resolve it when I can. The problem is that I can't work very much these days.
Latex support is a bit rusty now since I wrote my thesis and I stopped using latex on a daily basis :blush: If you find a solution feel free to comment further, and thanks for understanding!
@Fuco1 thank you! I'll keep thinking about it :)