Honza's UltiSnips Snippet gentbl<number>x<number>
available in the repository https://github.com/honza/vim-snippets/blob/master/UltiSnips/tex.snippets
Using this snippet which uses the python interpolation feature of UltiSnips, you can enter e.g.
gentbl5x3<tab>
which will expand to
\begin{tabular}{||||}
& & \\
& & \\
& & \\
& & \\
& & \\
\end{tabular}
Note the jump marks of UltiSnips are not visible.
For a possibility to adapt this for matrix environments, see the update of this answer.
Additionally rows can be added afterwards with the snippet tr<number><tab>
. Having three columns you would enter
tr3<tab>
to get
& & \\
Update due to request in comment
global !p
def create_matrix(snip):
matrix_str = (snip.buffer[snip.line].split('mat')[0]+'matrix').strip()
rows = 'x'.join(snip.buffer[snip.line].split("x", 2)[:-1])
cols = 'x'.join(snip.buffer[snip.line].split("x", 2)[-1:])
int_val = lambda string: int(''.join(s for s in string if s.isdigit()))
rows = int_val(rows)
cols = int_val(cols)
offset = cols + 1
old_spacing = snip.buffer[snip.line][:snip.buffer[snip.line].rfind('\t') + 1]
snip.buffer[snip.line] = ''
final_str = old_spacing + "\\begin{"+matrix_str+"}\n"
for i in range(rows):
final_str += old_spacing + '\t'
final_str += " & ".join(['$' + str(i * cols + j + offset) for j in range(cols)])
final_str += " \\\\\\\n"
final_str += old_spacing + "\\end{"+matrix_str+"}\n$0"
snip.expand_anon(final_str)
endglobal
pre_expand "create_matrix(snip)"
snippet "(small|[bBpvV])?mat(rix)?(\d+)x(\d+)" "Generate (small|[bBpvV])?matrix of *rows* by *columns*" br
endsnippet
This adds following snippet variants
matrix<N>x<M><Tab>
bmatrix<N>x<M><Tab>
Bmatrix<N>x<M><Tab>
pmatrix<N>x<M><Tab>
vmatrix<N>x<M><Tab>
Vmatrix<N>x<M><Tab>
smallmatrix<N>x<M><Tab>
Additionally, all of them can be shortened by replacing matrix
with mat
, e.g.
bmat4x3<Tab>
which will give you
\begin{bmatrix}
& & \\
& & \\
& & \\
& & \\
\end{bmatrix}
LuaSnip
for a LuaSnip version you can find a definition here:
https://github.com/evesdropper/luasnip-latex-snippets.nvim