1

For the LaTeXTools package, used in Sublime text, how would you define the same shortcut to operate differently for two different LaTeX document classes?

E.g. suppose that you wanted to define "control+enter" in the keymap to operate in the following way for BEAMER:

// new equation line (LHS + RHS)
{ "keys": ["control+enter"], "command": "insert_snippet", "args": {"contents": "\n\\uncover<+->{$1} &   \\uncover<.->{=$2}  \\\\\\\\$0"}, 
"context":  
    [
        {"key": "selector", "operator": "equal", "operand": "text.tex.latex string.other.math, text.tex.latex meta.environment.math"},
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }
    ]
},

But then have "control+enter" operate in the following way for ARTICLE

// new equation line (LHS + RHS)
{ "keys": ["control+enter"], "command": "insert_snippet", "args": {"contents": "\n$1    &   =$2 \\\\\\\\$0"}, 
"context":  
    [
        {"key": "selector", "operator": "equal", "operand": "text.tex.latex string.other.math, text.tex.latex meta.environment.math"},
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }
    ]
},

That is, have "control+enter" wrap an align environment with uncover overlays for beamer, but not for the article document class.

How would you do it?

What would you need to define the "operand" (or scope) as in each case?

  • You can always see the current scope with ctrl+alt+shift+p. Unfortunatly the scopes do not depend on the documentclass, so you can define a different scope for math, but not for beamer. However it is possible to write your own context, which checks for the document type. – r-stein Jun 26 '17 at 22:38
  • How would you go about writing your own context? – Pravin Jun 27 '17 at 10:59
1

You can't do this with the built-in scopes, but need a context. I will add the next prerelease (which is st3-4.0.0-alpha.3) via LaTeXTools#1167. If you have that prerelease you can just add the keybindings:

{ 
    "keys": ["ctrl+enter"], "command": "insert_snippet", "args": {"contents": "\n$1    &   =$2 \\\\\\\\$0"}, 
    "context":  
    [
        { "key": "selector", "operand": "text.tex.latex meta.environment.math" },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }
    ]
},
{ 
    "keys": ["ctrl+enter"], "command": "insert_snippet", "args": {"contents": "\n\\uncover<+->{$1} &   \\uncover<.->{=$2}  \\\\\\\\$0"}, 
    "context":  
    [
        { "key": "selector", "operand": "text.tex.latex meta.environment.math" },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "latextools.documentclass", "operand": "beamer" }
    ]
},

Until the release is published you can manually install it by

  1. Download the file here
  2. Open the LaTeXTools folder: in the Menu select Preferences > Browse Packages... and then open the LaTeXTools folder
  3. Put the downloaded file there

To emulate a snippet install "Chain Of Command" and create this keybinding:

{
    "keys": ["tab"],
    "command": "chain",
    "args": {"commands": [
        ["delete_word", {"forward": false}],
        ["insert_snippet", {"contents": "\\mybox<..>"}],
    ]},
    "context":
    [
        { "key": "auto_complete_visible", "operand": false },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "\\bbox$", "match_all": true },  // <- change "box" here
        { "key": "selector", "operand": "text.tex.latex meta.environment.math" },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "latextools.documentclass", "operand": "beamer" },
    ]
},
  • Wow that is fantastic. Great work, thank you very much. – Pravin Jun 29 '17 at 9:59
  • Is it possible to do something similar with a snippet? – Pravin Jul 3 '17 at 4:46
  • No, you can't add your own contexts to keybindings. However you may emulate a snippet with a keybinding. – r-stein Jul 3 '17 at 6:49
  • How would you code the keybinding? E.g. say I wanted to convert 'box' and then hit tab and form an emulated snippet through a keybinding, how would I do that? – Pravin Jul 3 '17 at 8:15
  • @Pravin I have added a keybinding for that in the answer. – r-stein Jul 3 '17 at 10:40

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.