Discussion:
[AUCTeX] Snippet for tabular
Denis Bitouzé
2016-02-29 17:36:19 UTC
Permalink
Hi,

I just came across the following yasnippet (see
https://groups.google.com/d/msg/smart-snippet/8Og-1U5zJ2E/3aIMTNmwrKAJ):

--8<---------------cut here---------------start------------->8---
# -*- mode: snippet -*-
# name: nxm matrix
# key: pm

# type: command
# --
(insert "\\begin{pmatrix}\n")
(let ((width (read-number "Matrix width?" 3))
(height (read-number "Matrix height?" 3))
(snippet-text ""))
(dotimes (i height)
(dotimes (j width)
(setq snippet-text (format "%s ${%d:m%d%d} %s"
snippet-text
(1+ (+ (* height i) j))
(1+ i)
(1+ j)

(if (= j (1- width))
(if (/= i (1- height)) "\\\\\\"
"")
"&"))))
(setq snippet-text (format "%s\n" snippet-text)))
(yas/expand-snippet (format "%s\\end{pmatrix}" snippet-text)))
--8<---------------cut here---------------end--------------->8---

What's nice: you are able to specify the dimensions of the matrix and
the resulting template provides placeholders.

What about similar feature provided by AUCTeX for e.g. tabulars?

All the best.
--
Denis
Joost Kremers
2016-02-29 20:20:55 UTC
Permalink
Post by Denis Bitouzé
I just came across the following yasnippet (see
[...]
Post by Denis Bitouzé
What's nice: you are able to specify the dimensions of the matrix and
the resulting template provides placeholders.
What about similar feature provided by AUCTeX for e.g. tabulars?
Why not implement such a feature with yasnippet? No reason why AUCTeX
should provide its own template system if we already have yasnippet, is
there?
--
Joost Kremers
Life has its moments
Denis Bitouzé
2016-02-29 20:35:00 UTC
Permalink
Post by Joost Kremers
Post by Denis Bitouzé
I just came across the following yasnippet (see
[...]
Post by Denis Bitouzé
What's nice: you are able to specify the dimensions of the matrix and
the resulting template provides placeholders.
What about similar feature provided by AUCTeX for e.g. tabulars?
Why not implement such a feature with yasnippet?
That's what I am about to do.
Post by Joost Kremers
No reason why AUCTeX should provide its own template system if we
already have yasnippet, is there?
I see at least 3 reasons for this:

1. This requires Yasnippet, not known by everybody.
2. Yasnippet doesn't provide templates for tabulars: the user has to
create its own snippets by himself and, considering the elisp code
I posted, that's not such an easy task (I wouldn't be able to do it
by myself).
3. AUCTeX /does provide/ a (rather simple) template for tabulars:
┌────
│ C-c C-e + tabu + TAB + RET + RET + llll + RET
└────
gives:
┌────
│ \begin{tabular}{llll}
│ |&&&
│ \end{tabular}
└────
`|' standing for the cursor position.
--
Denis
Mosè Giordano
2016-02-29 23:19:59 UTC
Permalink
Hi Denis,
Post by Denis Bitouzé
Hi,
I just came across the following yasnippet (see
--8<---------------cut here---------------start------------->8---
# -*- mode: snippet -*-
# name: nxm matrix
# key: pm
# type: command
# --
(insert "\\begin{pmatrix}\n")
(let ((width (read-number "Matrix width?" 3))
(height (read-number "Matrix height?" 3))
(snippet-text ""))
(dotimes (i height)
(dotimes (j width)
(setq snippet-text (format "%s ${%d:m%d%d} %s"
snippet-text
(1+ (+ (* height i) j))
(1+ i)
(1+ j)
(if (= j (1- width))
(if (/= i (1- height)) "\\\\\\"
"")
"&"))))
(setq snippet-text (format "%s\n" snippet-text)))
(yas/expand-snippet (format "%s\\end{pmatrix}" snippet-text)))
--8<---------------cut here---------------end--------------->8---
What's nice: you are able to specify the dimensions of the matrix and
the resulting template provides placeholders.
What about similar feature provided by AUCTeX for e.g. tabulars?
This looks like a good idea. For tabular-like environments with
format specifier we have that great mechanism that reads the format
and decides how many ampersands should be inserted, but for those
environments without a format specifier we have to insert all
ampersands by hand.

Maybe something like this should work:

--8<---------------cut here---------------start------------->8---
(defun LaTeX-env-ampersands (env)
"Insert ENV environment and ask for the number of columns of rows.
Insert ampersands for a table with the given numbers of columns
and rows."
(LaTeX-insert-environment env)
(let* ((columns (read-number "Columns number: " 3))
(rows (and (natnump columns)
(null (zerop columns))
(read-number "Rows number: " 3))))
(and (natnump rows)
(null (zerop rows))
(LaTeX-env-ampersands--columns-rows columns rows))))

(defun LaTeX-env-ampersands--columns-rows (columns rows)
"Insert ampersands for a table with COLUMNS columns and ROWS rows."
;; Prevent from asking optional argument for "\\" macro.
(let ((TeX-insert-macro-default-style 'mandatory-args-only))
(save-excursion
(dotimes (line rows)
(insert (make-string (1- columns) ?&))
(unless (= line (1- rows))
(end-of-line)
(just-one-space)
(TeX-insert-macro "\\")
(indent-according-to-mode)
(newline-and-indent)))))
(indent-according-to-mode))
--8<---------------cut here---------------end--------------->8---

This is just a start, the code can be improved. You should associate
`LaTeX-env-ampersands' to "pmatrix" environment (actually we will do
that in amsmath.el style file when implemented). Comments welcome.

Bye,
Mosè
Denis Bitouzé
2016-03-01 06:58:11 UTC
Permalink
Post by Mosè Giordano
Hi Denis,
Hi Mosè,
Post by Mosè Giordano
Post by Denis Bitouzé
Hi,
I just came across the following yasnippet (see
--8<---------------cut here---------------start------------->8---
# -*- mode: snippet -*-
# name: nxm matrix
# key: pm
# type: command
# --
(insert "\\begin{pmatrix}\n")
(let ((width (read-number "Matrix width?" 3))
(height (read-number "Matrix height?" 3))
(snippet-text ""))
(dotimes (i height)
(dotimes (j width)
(setq snippet-text (format "%s ${%d:m%d%d} %s"
snippet-text
(1+ (+ (* height i) j))
(1+ i)
(1+ j)
(if (= j (1- width))
(if (/= i (1- height)) "\\\\\\"
"")
"&"))))
(setq snippet-text (format "%s\n" snippet-text)))
(yas/expand-snippet (format "%s\\end{pmatrix}" snippet-text)))
--8<---------------cut here---------------end--------------->8---
What's nice: you are able to specify the dimensions of the matrix and
the resulting template provides placeholders.
What about similar feature provided by AUCTeX for e.g. tabulars?
This looks like a good idea. For tabular-like environments with
format specifier we have that great mechanism that reads the format
and decides how many ampersands should be inserted, but for those
environments without a format specifier we have to insert all
ampersands by hand.
[...]
This is just a start, the code can be improved. You should associate
`LaTeX-env-ampersands' to "pmatrix" environment (actually we will do
that in amsmath.el style file when implemented). Comments welcome.
Nice, many thanks!

Just are missing the placeholders: more generally, placeholders are IMHO
a missing feature of AUCTeX (compared to some TeX editors such as
TeXstudio), e.g. for fractions.

All the best.
--
Denis
Loading...