SublimeText / LaTeXTools

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

Custom keybinding for creating a table #1461

Closed JacobEFO closed 4 years ago

JacobEFO commented 4 years ago

Hey LatexTools enthusiasts.

I have become quite familiar with most of the default kebinding, however now I wish to create a custom keybinding for creating my default tables. Say my tables look like this:

\begin{table}[htbp]
    \centering
    \caption{PLACEHOLDER}
    \begin{tabular}{}
        \dtoprule
        \hline
        \dbottomrule
    \end{tabular}
\end{table}

And I wish to use "super+l","t" for this. I have tried to use the "command":"latexenv" for this, where I assume it somehow calls "latexEnvironment.py" with some default argument, but I have had no luck in passing any arguments to "latexenv".

Is there anyone here, who can point me towards the way of doing this or how to properly understand the JSON syntax and keys to the functions.

Best regards

Edit: Somehow I cannot figure out how to properly format my latex code here and pastebin doesn't allow me more code snips for today. Sorry guys.

r-stein commented 4 years ago

I think the easiest way to archive this is to create a snippet and bind a keybinding towards it

  1. select Preferences > Browse Packages..., this will open the file explorer at the ST packages location
  2. open the User folder (if it does not exists create it)
  3. create a new file (e.g. my_table.sublime-snippet) and open it with Sublime Text (or any other editor)
  4. insert the following content (you may adapt this to match your preferences):
    <snippet>
        <content><![CDATA[
    \begin{table}[htbp]
        \centering
        \caption{${1:PLACEHOLDER}}
        \begin{tabular}{}
            \dtoprule
            \hline
            \dbottomrule$0
        \end{tabular}
    \end{table}
    ]]></content>
        <!-- <tabTrigger>mytable</tabTrigger> -->
        <scope>text.tex.latex</scope>
    </snippet>
  5. add the following keybinding (Preferences > Key Binding) (adapt the file name to the name you gave above)
        {
            "keys": ["ctrl+l", "ctrl+t"],
            "command": "insert_snippet",
            "args": { "name": "Packages/User/my_table.sublime-snippet" },
            "context": [
                { "key": "selector", "operator": "equal", "operand": "text.tex.latex" }
            ],
        },
JacobEFO commented 4 years ago

Hey r-stein,

Thank you very much. It works like a charm!

Definitely a much better approach than I had initially. Where do you get to know about the "${1:PLACEHOLDER}" and "\dbottomrule$0" as well as the definitions of the scope?

Seems like $1 places the cursor at the mark and scope checks to see if it is in the text part of a latex document (seen similarly for math environment checks). But what does $0 do?

r-stein commented 4 years ago

The scope of the snippet is not relevant if you don't use the tab trigger, but you can see the current scope by pressing ctrl+alt+shift+p.

The fields $1, $2, ... can be used to insert text at the corresponding and jump to the next field with tab. The exit position is marked with $0.

You can use placeholders and even create fields based on transformations on other fields, e.g. auto remove special characters for labels:

<snippet>
    <content><![CDATA[
\begin{table}[htbp]
    \centering
    \caption{${1:PLACEHOLDER}}
    \label{${1/(\w)|(\W+)/\L\1(?2:_:)/g}}
    \begin{tabular}{}
        \dtoprule
        \hline
        \dbottomrule$0
    \end{tabular}
\end{table}
]]></content>
    <!-- <tabTrigger>mytable</tabTrigger> -->
    <scope>text.tex.latex</scope>
</snippet>
JacobEFO commented 4 years ago

Ah, excellent with the tab completion markers $1/$0. Makes good sense to have such functionality.

I can see, that LaTeXTools already utilize a few .sublime.snippet. Could have just looked there, doh.

ST3 and LaTeXTools is such a strong combination, I am ashamed I didn't find this combination all those years ago. Thanks for keeping it up to date.

r-stein commented 4 years ago

@JacobEFO I did forgot to link the snippets documentation: https://sublime-text-unofficial-documentation.readthedocs.io/en/latest/extensibility/snippets.html