Sublime Forum

Matching delimiters in LatexTools

#1

Delimiters such as (), {} and [] are not paired if the cursor is next to certain symbols, a period, for example. Is there a way to correct this?

0 Likes

#2

You have to be more specific than that. Do you have a text selection? Is the cursor before or after the period? I will assume you have no selection with the cursor before the period, and you want auto-paired parentheses. In that case, consider the default key-binding below:

	{ "keys": ["("], "command": "insert_snippet", "args": {"contents": "($0)"}, "context":
		[
			{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
			{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true }
		]
	},

You want to add |\\. to the line below, right before the last ). If you are not familiar with regular expressions, | means or, and the period must be scaped twice because this is in JSON.

			{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true }

Therefore, we have:

	{ "keys": ["("], "command": "insert_snippet", "args": {"contents": "($0)"}, "context":
		[
			{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
			{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$|\\.)", "match_all": true }
		]
	},

Go to Preferences -> Key bindings, and paste the code above on the right-hand side (user key bindings).

0 Likes

#3

Where is the file that contains the definition of “(” ?

Here is where I looked but I couldn’t find it.

Preferences/Package Settings/LatexTools/Key Bindings – Default

0 Likes

#4

Those bindings are coming from the default key bindings;
try using just Preferences > Key Bindings and search the left hand pane.

0 Likes

#5

I think I am getting the knack of it (steep learning curve):

Apparently the right window in Preferences > Key Bindings is where I enter my personal definitions. I added:

{ "keys": ["("], "command": "insert_snippet", "args": {"contents": "(${0:$SELECTION})"}},

which produces a pair of parentheses unconditionally, and is what I wanted.

Thanks to all who replied.

1 Like

#6

If you want a pair of parentheses unconditionally, great. But consider the following code:

\documentclass{article}

\begin{document}

\begin{equation}
a + b)^{2} = a^{2} + 2ab + b^{2}
\end{equation}

\end{document}

Notice that the open parenthesis was forgotten in line 6. When you move the cursor to the beginning to line 6 and press (, based on the key binding you created, Sublime will insert (). To avoid this issue, I recommend using the approach I described in my previous post.

I hope this helps.

0 Likes

#7

Good point. I went for “unconditional” because it is so easy.

I will implement your code. I did not because I could not interpret what it does. For a newcomer JSON is not very intuitive.

By the way, I don’t understand why LatexTools does not implement your code as the default and instead defines the parentheses and other delimiters in a way that causes more problems than it solves. My latexing got slowed down enormously when I switched to sublime text/latextools until I redifined the basic delimiters (), [], {} and $$.

0 Likes