Search code examples
sublimetext3sublimetext-snippet

Latex Sublime snippet


I'm using a Sublime snippet to create a Latex state machine template. However, it does nothing (when I type "stmach" and press tab, stmach disappears but the Latex code isn't included). I don't understand why, as all my other snippets are working fine.

If I remove some lines in the snippet, it works, but I need the whole block :/

<snippet>
<content><![CDATA[
    \begin{center}
        \begin{tikzpicture}[shorten >=1pt,node distance=4.5cm,on grid,auto]

        \tikzstyle{every state}=[draw=blue!50,very thick,fill=blue!20] % Node color

        \node[state,initial,initial text=reset, initial where=below] (configuration) {$conf$}; % Node name and position
        \node[state] (init) [below right=of configuration] {$init$};

        \path[->] % Arrow
        (configuration)
            edge  [bend left]                 node {finishConfiguration=1} (init)

        (init)
            edge  [bend left]                 node {} (configuration);

        \end{tikzpicture}
    \end{center}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>stmach</tabTrigger> 
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>text.tex</scope>

Note that the Latex code works without any error or warning.


Solution

  • The $ character has special meaning in Sublime Text snippets, and therefore needs to be escaped to be valid.

    You can escape them with a preceding backslash, as the documentation shows:

    <snippet>
    <content><![CDATA[
        \begin{center}
            \begin{tikzpicture}[shorten >=1pt,node distance=4.5cm,on grid,auto]
    
            \tikzstyle{every state}=[draw=blue!50,very thick,fill=blue!20] % Node color
    
            \node[state,initial,initial text=reset, initial where=below] (configuration) {\$conf\$}; % Node name and position
            \node[state] (init) [below right=of configuration] {\$init\$};
    
            \path[->] % Arrow
            (configuration)
                edge  [bend left]                 node {finishConfiguration=1} (init)
    
            (init)
                edge  [bend left]                 node {} (configuration);
    
            \end{tikzpicture}
        \end{center}
    ]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>stmach</tabTrigger> 
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>text.tex</scope>
    </snippet>
    

    Using the syntax highlighting from https://github.com/sublimehq/Packages/pull/576 makes the problem clearer, as ST unfortunately doesn't offer any feedback, as you have seen: invalid snippet

    How it looks when escaped: valid snippet