我如何用$符号在崇高文本3中包含一个单词

时间:2017-05-14 00:00:21

标签: latex sublimetext3

我在Sublime text 3上使用乳胶;我想在高亮显示的文本周围放一个$ sign。例如,我有一个角色说

X_ {d}

我希望它能做一个键盘快捷键,在它之前和之后打印一个$符号。当我按下alt-shift-w我得到

时,我该怎么办?
<p>X_{d}</p>

有没有办法获得$?

类似$ X_ {d} $而不是<p>

2 个答案:

答案 0 :(得分:2)

您可以通过在用户键绑定中添加以下内容来实现此目的:

{ "keys": ["alt+shift+w"], "command": "insert_snippet", "args": { "contents": "\\$${1:$SELECTION}\\$" },
    "context": [
        { "key": "selector", "operator": "equal", "operand": "text.tex" },
    ]
},

为了使其可逆,您需要编写一个Python插件,因为该功能并未内置于ST:

  • 工具菜单 - &gt;开发者 - &gt;新插件......
  • 选择全部并替换为以下
import sublime
import sublime_plugin
import re


class TextAroundSelectionEventListener(sublime_plugin.EventListener):
    def on_query_context(self, view, key, operator, operand, match_all):
        if key not in ('text_preceding_selection', 'text_following_selection'):
            return None

        match = False
        for sel in view.sel():
            line = view.line(sel)
            before_sel = sublime.Region(line.begin(), sel.begin())
            after_sel = sublime.Region(sel.end(), line.end())
            check_region = before_sel if key == 'text_preceding_selection' else after_sel

            if operator in (sublime.OP_EQUAL, sublime.OP_NOT_EQUAL):
                match = view.substr(check_region) == operand
                if operator == sublime.OP_NOT_EQUAL:
                    match = not match
            else:
                if operator in (sublime.OP_REGEX_MATCH, sublime.OP_NOT_REGEX_MATCH):
                    operand += r'$'
                method = re.match if operator in (sublime.OP_REGEX_MATCH, sublime.OP_NOT_REGEX_MATCH) else re.search
                try:
                    match = bool(method(operand, view.substr(check_region)))
                except re.error as e:
                    print('error in keybinding context operand "' + str(operand) + '" for key "' + key + '":', e)
                    return None
                if operator in (sublime.OP_NOT_REGEX_MATCH, sublime.OP_NOT_REGEX_CONTAINS):
                    match = not match

            if match != match_all:
                return match
        return match


class TrimCharsAroundSelection(sublime_plugin.TextCommand):
    def run(self, edit, **kwargs):
        chars_to_trim = kwargs.get('chars_to_trim', 1)
        for sel in reversed(self.view.sel()):
            self.view.replace(edit, sublime.Region(sel.end(), sel.end() + chars_to_trim), '')
            self.view.replace(edit, sublime.Region(sel.begin() - chars_to_trim, sel.begin()), '')
  • 将文件夹ST建议(Packages/User/)保存为text_around_sel.py

然后,使用以下键绑定:

{ "keys": ["alt+shift+w"], "command": "insert_snippet", "args": { "contents": "\\$${1:$SELECTION}\\$" },
    "context": [
        { "key": "selector", "operator": "equal", "operand": "text.tex" },
        { "key": "text_preceding_selection", "operator": "not_regex_contains", "operand": "\\$$" },
        { "key": "text_following_selection", "operator": "not_regex_contains", "operand": "^\\$" },
    ]
},
{ "keys": ["alt+shift+w"], "command": "trim_chars_around_selection", "args": { "chars_to_trim": 1 },
    "context": [
        { "key": "selector", "operator": "equal", "operand": "text.tex" },
        { "key": "text_preceding_selection", "operator": "regex_contains", "operand": "\\$$" },
        { "key": "text_following_selection", "operator": "regex_contains", "operand": "^\\$" },
    ]
},

解释和推理:

  • The preceding_text and following_text contexts that are built in to ST always include the selected text,无论文本选择插入符在何处,都无法用于检测文本周围/外部选择。
  • (如果选择包括两端的美元,则可以在不创建任何新的上下文侦听器的情况下,使用text键以及^$锚点。)
  • 因此,我们需要编写自己的上下文来正确忽略选择。遗憾的是,Sublime(Find)API中的限制意味着我们必须使用Python的re模块执行此操作,这是一个功能较弱的正则表达式引擎 - 尽管出于此问题的目的,这没关系。 (如果需要,可能会有一个&#34; hacky&#34;绕过它,通过将线的相关内容复制到临时隐藏视图(即输出面板)并使用Sublime的find API来确保锚点按预期工作等,但这超出了此任务的范围。)
  • 然后trim_chars_around_selection命令以相反的顺序进行所有选择(因此,当文档被插件修改时,文档开头的选择偏移不会改变)并删除指定的数字来自选择任一端的字符。

答案 1 :(得分:0)

@Keith Hall提供的代码可以进一步推广以减少任何值并添加任何值。我会在这里指出这一点。

假设你想用一个特定的R和T包装一个乳胶文件,通过按下一个字符组合,所选字符串说Tamer将是RTamerT,并按下一次键绑定再次成为Tamer

{ "keys": ["alt+shift+q"], "command": "insert_snippet", "args": { "contents": "R${1:$SELECTION}T", "scope": "text.tex.latex"},
    "context": [
        { "key": "selector", "operator": "equal", "operand": "text.tex" },
        { "key": "text_preceding_selection", "operator": "not_regex_contains", "operand": "R" },
        { "key": "text_following_selection", "operator": "not_regex_contains", "operand": "^T" },
    ]
},

{ "keys": ["alt+shift+q"], "command": "trim_chars_around_selection", "args": { "chars_to_trim": 1, "scope": "text.tex.latex" },
    "context": [
        { "key": "selector", "operator": "equal", "operand": "text.tex" },
        { "key": "text_preceding_selection", "operator": "regex_contains", "operand": "R" },
        { "key": "text_following_selection", "operator": "regex_contains", "operand": "^T" },
    ]
},

我想分享,因为我想找到一种新的包装,它概括了Keith的代码,但不知道如何在崇高的文本中进行编码。试验和错误有助于我找到自己的方式,并想与您分享