2

Smartparens is quite useful, but I just do not understand its documentation.

How do I make smartparens insert "` and "' instead of `` and '' when pressing " in latex-mode?

Remark (because formating this question is somewhat difficult)
Smartparens inserts :

``''

on ", but I need:

"`"'

Note: I do use (require 'smartparens-config) to initially set up smartparens stuff. This loads smartparens-latex.el and this sets up the ``'' pair. I'm not able to change the behavior to insert those pairs, which I need. Neither by editing smartparens-latex.el nor by commands in my init.el.
Also note: I'm using auctex.

1

In principle the following lisp code should work in your initialization file. The action :rem for sp-local-pair removes ``'' from the list of pairs and thereafter we add "`"' . Furthermore we remove TeX-insert-quote from LaTeX-mode-map.

(eval-after-load "smartparens-latex"
  '(sp-with-modes '(
              tex-mode
              plain-tex-mode
              latex-mode
              LaTeX-mode
              )
          (sp-local-pair "``" nil :actions :rem)
          (sp-local-pair "\"`" "\"'" :trigger "\"" :actions '(insert wrap)
                 :unless '(sp-latex-point-after-backslash))))

(eval-after-load "latex" '(define-key LaTeX-mode-map "\"" nil))

Note, that removing and adding of pairs to major modes is described there: https://github.com/Fuco1/smartparens/wiki/Pair-management.

Since there seems to be a bug (see below), insertion with above code is not working. Therefore you could use following workaround:

(eval-after-load "smartparens-latex"
    '(sp-with-modes '(tex-mode plain-tex-mode latex-mode LaTeX-mode)
         (sp-local-pair "``" nil :actions :rem)
         (sp-local-pair "\"" nil :actions nil)
         (sp-local-pair "\"`" "\"'" :trigger "\"" :actions '(insert wrap) 
                :unless '(sp-latex-point-after-backslash))))

(eval-after-load "latex" '(define-key LaTeX-mode-map "\"" nil))

This additional disables the global " " pair for latex modes.


A side-note on testing:

As of 2017-07-09 (melpa-version 20170708.1326), https://github.com/Fuco1/smartparens/ seems to be broken.

Already the unmodified version of smartparen shows strange behavior. If I press " in the middle between Testing and quotes of the following LaTeX text

\documentclass{article}
\usepackage[ngerman]{babel}
\begin{document}
Testing  quotes
\end{document}

I get the following strange auto-insertion:

\documentclass{article}
\usepackage[ngerman]{babel}
\begin{document}
Testing'' ` quotes
\end{document}

One can follow the execution of sp-insert-pair. The trigger character " is deleted. The open part of the pair is decomposed into its prefix " and its last character `. The last character is inserted. Then it skips backward one thing. Thereby, the thing is whatever sp-get-thing returns (here it is just space). There it inserts the closing part ''.

Corresponding bug-report on the development page of smartparens: https://github.com/Fuco1/smartparens/issues/772.

If I use the above modification I get

\documentclass{article}
\usepackage[ngerman]{babel}
\begin{document}
Testing"' ` quotes
\end{document}

The double-quote of the closing part indicates that the pair is actually right. But, we get the strange behavior as we get it for the unmodified version of smartparens.

  • Big thanks, with your help I got a better understanding of smartparens and I could find a workaround. Please review/edit/change my workaround information in your answer. Then I think your answer solves the problem. – jue Jul 10 '17 at 11:41
  • @jue I removed my previous comment since it was not true. In my test of your fix I had :actions nil replaced by :actions :rem. With your version (:actions nil) it seems to work with the newest version of smartparens. I also tested the melpa-stable version (that is: 1.10.1). That worked without workaround. – Tobias Jul 10 '17 at 13:44
  • 1
    @jue Finally I better understand your workaround. The case of :actions nil is not explicitly described in the documentation but one just has to accept it as one valid action list meaning that no actions are taken. The difference between :actions :rem and :actions nil is that :actions :rem removes the pair from the considered action list while :actions nil overrides the actions of the pair in the global list. Now I gained quite a bit insight into a minor-mode which I never use;-)). – Tobias Jul 10 '17 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.