3

Q: how do I get smartparens not to insert a pair of ``LaTeX'' quotes?

The goal

I set up smartparens to insert a pair of quotation marks unless point is just before a word or just after it, where * is point:

some text * some more text ==> some text "*" some more text
*some text                 ==> "*some text
some text*                 ==> some text"*

I have it set up for text-mode and org-mode like so:

(sp-local-pair '(text-mode org-mode) "\"" nil
               :unless '(sp-point-before-word-p
                         sp-point-after-word-p))

The problem

LaTeX is goofy with ``double quotes'' and `single quotes'. That is: double quotes are done with two backticks on the front and two ticks at the end. This doesn't work:

(sp-local-pair '(latex-mode) "\"" nil
               :unless '(sp-point-before-word-p
                         sp-point-after-word-p))

The ugly "solution"

After much tinkering, the following works somewhat:

(with-eval-after-load "latex"
  (sp-local-pair '(latex-mode) "`" nil
                 :unless '(sp-point-before-word-p))
  (sp-local-pair '(latex-mode) "'" nil
                 :unless '(sp-point-after-word-p))
  (sp-local-pair '(latex-mode) "``" nil
                 :unless '(sp-point-before-word-p))
  (sp-local-pair '(latex-mode) "''" nil
                 :unless '(sp-point-after-word-p)))

But that seems a little ludicrous for such simple functionality. It's also far from perfect, in that I get new and sometimes baffling errors from yasnippet, of all things.

The mournful appeal

Am I missing some simple, LaTeX-specific way of doing this?

  • Sorry, what does "LaTex is goofy" mean? Doesn't work how? – Tyler Mar 1 '17 at 21:29
  • @Tyler: edited: an opening quote is comprised of two backticks, and a closing quote is comprised of two ticks (ie, apostrophes). – Dan Mar 1 '17 at 21:34
  • Sorry, I don't understand what you want to eventually achieve: do you want an easy way to insert ``LaTeX'' quotes? Do you use AUCTeX? – giordano Mar 2 '17 at 8:08
  • @giordano: yes, I'm using AUCTeX. smartparens inserts pairs of parentheses, quotes, etc. When I enter ", it gives me a pair of LaTeX quotes. I want it to do that except when point is just before the start of a word (when I want it to insert just the opening quote) or just after the end of a word (when I want it to insert just the closing quote). – Dan Mar 2 '17 at 13:18
  • Why not just use C-q " in the special situations? – Andrew Swann Mar 3 '17 at 8:34
1

I believe I have it. The goal is to get smartparens to:

  • insert a pair of ``LaTeX quotes''
  • unless at the beginning of a word, when it inserts the ``opening quote
  • unless at the end of a word, when it inserts the closing quote''

Here's the code:

(require 'smartparens-latex)

(sp-local-pair '(tex-mode plain-tex-mode latex-mode LaTeX-mode)
               "``" "''"
               :trigger "\""
               :unless '(sp-latex-point-after-backslash
                         sp-point-before-word-p
                         sp-point-after-word-p)
               :pre-handlers  '(sp-latex-pre-slurp-handler)
               :post-handlers '(sp-latex-skip-double-quote))

(sp-local-pair '(tex-mode plain-tex-mode latex-mode LaTeX-mode)
               "`" "'"
               :trigger "'"
               :unless '(sp-latex-point-after-backslash
                         sp-point-before-word-p
                         sp-point-after-word-p)
               :pre-handlers  '(sp-latex-pre-slurp-handler)
               :post-handlers '(sp-latex-skip-double-quote))

Note that this modifies the settings I found when digging around in the code base in smartparens-latex.

NB: this only seems to work if you redefine the local pair for both the double quotes and the single quotes. If you do only the former, this is what you get when trying to insert a double quote at the beginning of a word:

``'gasp, clutch my pearls

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.