LaTeX: dynamic pmatrix snippets

249 views
Skip to first unread message

Michael Bach

unread,
Mar 25, 2010, 6:05:24 PM3/25/10
to smart-snippet and YASnippet
Hello everyone,

I am new to this list and to yasnippet in general. I had my share of
fights and collaborations with Emacs and AUCTeX. I am newly addicted
to yasnippet and use it more often every day... it comes in really
handy for LaTeX, e.g. for figures and tables, and in general for
nested environments with many customization options.

Recently, while setting up an exercise sheet for math students, I
wondered whether I could write a snippet that does the following:

Imagine writing exercises concerning linear algebra. Fancy takes me
and I want to pose a problem dealing with a 3x4 matrix. Or a 4x4
matrix. Or a good old 3-dimensional vector (i.e. 1x3 matrix). Or a
quick 3x3 matrix for determinant calculation. You see where I am
going?

To be more specific I need:

pmat - TAB --> "Enter dimensions of matrix: " --> depending on input
(lets say for now 3 rows and 3 columns)

\begin{pmatrix}
& & \\
& & \\
& &
\end{pmatrix}

I thought of doing this kind of thing in Emacs Lisp, but the good
thing with yasnippet in this case would be a nice visual feedback for
my inputs and the ability to navigate via TAB and S-TAB.

I am now working with a subset of n x m matrices, i.e. I defined pm33,
pm22, pm34 and so on. This is unnecessary hardcoding I think, thus my
post.

Any suggestions on this one? Thanks in advance!

João Távora

unread,
Mar 26, 2010, 12:09:48 PM3/26/10
to smart-...@googlegroups.com
Hey very nice idea, was quite fun to program :) , though I don't know if the latex syntax is OK, I took it from your example matrix.

# -*- mode: snippet -*-
# name: nxm matrix
# key: mat
# 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))
i
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)))

You see how this works? The actual snippet body is built dynamically and then yas/expand-snippet is called. Nonetheless the snippet definition file is similar to any other snippet.

It's only supported in the SVN trunk of yasnippet, upcoming 0.7, notice the new "# type: command" directive.

This is in my opinion a great improvement to YASnippet and quite underused (haven't seen any suggestions of use). It's true, the YASnippet documentation does not mention it yet :-) but I have announced it a couple of time here, and I hope you can find other uses for this feature. (and also test it!)

> --
> You received this message because you are subscribed to the Google Groups "smart-snippet and YASnippet" group.
> To post to this group, send email to smart-...@googlegroups.com.
> To unsubscribe from this group, send email to smart-snippe...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/smart-snippet?hl=en.
>

Michael Bach

unread,
Mar 26, 2010, 1:20:15 PM3/26/10
to smart-snippet and YASnippet
Wow, nice job! I really like it. I checked out the svn trunk and tried
it. I had to do some minor changes for LaTeX (single & instead of &&)
and as a math tutor I had to change the element numbering from
m00..m33 to m11..m33 ;)

the # type: command directive is a marvellous thing. One big entry
removed from wishlist... Thanks to your example and explanation, I can
guess how to use this new functionality. Let us see where it could be
used next...

Thanks João for your efforts and help!

Just for completeness, your code and my personal adaptions:

# -*- 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)))

João Távora

unread,
Mar 27, 2010, 10:28:02 AM3/27/10
to smart-...@googlegroups.com
Glad you like it! I'll post this example in the documentation.

If you think of any more ideas (also for other languages, not just LaTeX), or a wishlist for yasnippet, let me know!

PS:I was trying to nail it on those mm's, but failed miserably on the m00 :-) it seems my calculus is not what it used to be, of course its m11.

Reply all
Reply to author
Forward
0 new messages