Snippet for delimiters for LaTeX

86 views
Skip to first unread message

Dror Atariah

unread,
Jul 19, 2012, 4:35:22 AM7/19/12
to smart-...@googlegroups.com
In LaTeX you have several options for delimiters:

\left< \right>
\left( \right)
\left\{ \right\}
...

I want to write a snippet which will expand to these delimiters; I want it to be interactive, namely using "yas/choose-value". I encounter two problems:
1. Say that I chose the "<" delimiter, then at the end of the expression ">" should appear. How can I do it?
2. The second problem relates to the usage of chars like "{" and "(". Is the following: ${2:$$(yas/choose-value '("<" "\{" "("))} valid?

Thanks!

Anders Eurenius

unread,
Jul 24, 2012, 2:29:30 AM7/24/12
to smart-...@googlegroups.com
Well, there is a trick for part 1, here's one of my snippets:
----
# -*- mode: snippet -*-
# name: ix
# binding: 。
# --
[${1:`(cond
((string-match "^[+-]?[0-9]+$" yas/selected-text) "")
((string-match "'" yas/selected-text) "\"")
(t "'")
)`}${1:`yas/selected-text`}${1:`(cond
((string-match "^[+-]?[0-9]+$" yas/selected-text) "")
((string-match "'" yas/selected-text) "\"")
(t "'")
)`}]$0
----
So, basically, you'd do something like ${2:`(cond ...)`} and give ">"
if yas/text is "<". (my snippet examines the selected text.)

aes
> --
> You received this message because you are subscribed to the Google Groups
> "smart-snippet and YASnippet" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/smart-snippet/-/mVDdDH2o2wUJ.
> 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.

João Távora

unread,
Jul 24, 2012, 4:44:00 AM7/24/12
to smart-...@googlegroups.com
Hi!

You want something like

# -*- mode: snippet -*-
# name: lr
# key: lr
# --
\\left${1:$$(yas/choose-value '("{" "("
"["))}\\right${1:$(yas/latex-closing-delimiter-for yas/text)}

while having this function defined somewhere (latex-mode/.yas-setup.el
is recommended)

(defun yas/latex-closing-delimiter-for (text)
(cond ((string= text "{") "}")
((string= text "[") "]")
((string= text "(") ")")))

This might need some tweaking if you decide to enter gibberish instead
of an opening delimiter in $1.

João
> --
> You received this message because you are subscribed to the Google Groups
> "smart-snippet and YASnippet" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/smart-snippet/-/mVDdDH2o2wUJ.
> 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.



--
João Távora

OMF

unread,
Jul 24, 2012, 5:57:22 AM7/24/12
to smart-...@googlegroups.com
Just to mention an alternative method not involving a choice function but rather using multiple snippets, I use the following four separate snippets for brackets (includes vertical brackets)


# -*- mode: snippet -*-
# key: {
# group: math
# name: {}
# --
\left\{ $1 \right\}$0


# -*- mode: snippet -*-
# key: [
# group: math
# name: []
# --
\left[ $1 \right]$0


# -*- mode: snippet -*-
# key: |
# group: math
# name: ||
# --
\lvert $1 \rvert$0


# -*- mode: snippet -*-
# key: (
# group: math
# name: ()
# --
\left( $1 \right)$0

Dror Atariah

unread,
Jul 24, 2012, 10:51:19 AM7/24/12
to smart-...@googlegroups.com
KISS... I guess this is the solution I will take. Simple, easy to maintain... Thanks for the joint effort!

Cheers.
Dror
Reply all
Reply to author
Forward
0 new messages