SublimeText / LaTeXTools

LaTeX plugin for Sublime Text
https://latextools.readthedocs.io/
2k stars 364 forks source link

Autopair dollar-sign $$ in brackets {...} #1474

Closed phatsp closed 4 years ago

phatsp commented 4 years ago

I have to deal with the multiple-choice command a lot, like \choice{$1$}{$2$}{$3$}{$4$}. I'd like LaTexTools to auto pair the $ within those brackets {} just as it does in a normal LaTeX environment. Is there any way I could do this. I looked up the posts but couldn't find the answer. I'm a teacher and not familiar with programming so I really hope you could help me in detail.

r-stein commented 4 years ago

Sorry for the late reply.

Do I understand you correctly that you want this behavior: choice

I just tried it out with an new setup and it seems to behave like this.

phatsp commented 4 years ago

Yes, that's what I'm hoping for. After your reply I did remove LaTexTools then reinstalled it again in my Sublime Text 3 but still couldn't get that autopairing as you did. I can't figure out why. Is there any config in settings that I need to turn on to make this work?

r-stein commented 4 years ago

I don't know why it does not behave as desired.

The corresponding keybinding is the following:

    { "keys": ["$"], "command": "insert_snippet", "args": {"contents": "\\$$0\\$"},
    "context":
        [
            { "key": "setting.command_mode", "operator": "equal", "operand": false },
            { "key": "selector", "operator": "equal", "operand": "text.tex.latex" },
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            // do not insert this if it is escaped
            { "key": "preceding_text", "operator": "not_regex_contains", "operand": "(?:\\\\\\\\)*(?:\\\\|\\$)$", "match_all": true },
            // don't insert, it inside math environments
            { "key": "selector", "operator": "not_equal", "operand": "meta.environment.math, string.other.math", "match_all": true },
            // don't insert, if there is an open dollar math environment at the end of the line
            { "key": "eol_selector", "operator": "not_equal",
              "operand": "meta.environment.math.inline.dollar - punctuation.definition.string.end, string.other.math - punctuation.definition.string.end",
              "match_all": true }
        ]
    },

You may open your keybindings (Preferences > Key Bindings) and paste it there (on the right side). This gives the keybinding precedence over other keybindings for the $ key.

If this still does not help you may remove values from the context and just use this keybinding:

    { "keys": ["$"], "command": "insert_snippet", "args": {"contents": "\\$$0\\$"},
    "context":
        [,
            { "key": "selector", "operator": "equal", "operand": "text.tex.latex" },
        ]
    },

This would also enable the keybinding at stages where you may not want it to be enabled.

phatsp commented 4 years ago

I think I figured out why now. I think it may be due to some conflicts with LaTeXYZ package that I have on my ST.

First, I opened the LaTexTools' Sublime-keymap file then compared the keybinding code as you told and they are exactly the same. Then I decided to keep LaTeXTools and uninstalled LaTeXYZ. It then produced the auto-pair $$ as desired.

Now I want to keep LaTEXYZ installed in my ST since it comes with many autocomplete math commands. I did try to overwrite the keybindings by following your advice: Preference-->Key Bindings and copying it to the right-hand side, but it produced this error. I've tried both keeping and removing the curly brackets {}. Am I doing it correctly?

image image

r-stein commented 4 years ago

You need to keep the array [] around the keybindings. Hence if this is your only kbd:

[
    { "keys": ["$"], "command": "insert_snippet", "args": {"contents": "\\$$0\\$"},
    "context":
        [
            { "key": "setting.command_mode", "operator": "equal", "operand": false },
            { "key": "selector", "operator": "equal", "operand": "text.tex.latex" },
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            // do not insert this if it is escaped
            { "key": "preceding_text", "operator": "not_regex_contains", "operand": "(?:\\\\\\\\)*(?:\\\\|\\$)$", "match_all": true },
            // don't insert, it inside math environments
            { "key": "selector", "operator": "not_equal", "operand": "meta.environment.math, string.other.math", "match_all": true },
            // don't insert, if there is an open dollar math environment at the end of the line
            { "key": "eol_selector", "operator": "not_equal",
              "operand": "meta.environment.math.inline.dollar - punctuation.definition.string.end, string.other.math - punctuation.definition.string.end",
              "match_all": true }
        ]
    },
]
phatsp commented 4 years ago

It works perfectly now. Thank you so much for your patience.