4

I am writing a template with yasnippet.

Writing a equation in latex using {eqnarray} means I will cite it somewhere so I will give it a label. When using {eqnarray*}, there don't need to be a label.

I am new to lisp and yasnippet, so I follow an existed template for c-mode:

# -*- mode: snippet -*-
# name : printf 
# contributor : joaotavora
# --
printf ("${1:%s}\\n"${1:$(if (string-match "%" text) "," "\);")
}$2${1:$(if (string-match "%" text) "\);" "")}

This works out well. Note that there is backslash in this template. My template is:

# -*- mode: snippet -*-
# name: eqnarray
# key: eq
# --
\begin{eqnarray$1}
${1:$(if (string-match "*" text) "\\label{eq:" "")}$2${1:$(if (string-match "*" text) "}" "")}
$0\end{eqnrrary$1}

The result will be:

\begin{eqnarray*}label{eq:xxx}
XXX\end{eqnarrary*}

The backslach expected in front of "label" is missing! When I replace \\ with \" or \(, I can get expected result, which means there will be " or ( in front of "label" respectively.

This really confuses me...

Besides I find a logic error in my code...The if command should be (if (string-match "*" text) "" "\\label{eq:"). Since this error has misled those who answered my question, I think that I must keep my error here:)

And I just begin to use yasnippet yesterday, I find that this template is really far from practice use, as those comments below pointed out...

4
  • 1
    I don't see a backslash in the C template: \\n is a newline, and \(\) are for the pattern matching. That's one of the joys of emacs lisp, the backslash expansion: each time a string is evaluated, backslashes are expanded. In your case, the backslash is expanded once in elisp, resulting in \label, then once again by yas, resulting in l. Adding two extra backslashes might do the trick.
    – T. Verron
    Jul 7, 2014 at 11:03
  • 3
    Besides this you really ought to not use eqnarray at all.
    – daleif
    Jul 7, 2014 at 11:06
  • Why don't you use C-c C-e to insert environments? Anyway, I second daleif advice not to use eqnarray at all.
    – giordano
    Jul 7, 2014 at 11:11
  • 1
    If C-c C-e doesn't work, install AUCTeX with M-x package-install RET auctex RET or use the (dumbed-down) default latex-mode, which provides C-c C-o. Jul 8, 2014 at 0:22

2 Answers 2

5

You need more backslashes! There are two levels of quoting going on. As you will notice in the first example to get \n it was necessary to put \\n in the source. In your case you need to write \\\\label:

# -*- mode: snippet -*-
# name: eqnarray
# key: eq
# --
\begin{eqnarray$1}
${1:$(if (string-match "*" yas-text) "\\\\label{eq:" "")}$2${1:$(if (string-match "*" yas-text) "}" "")}
$0\end{eqnrrary$1}

[I have a relatively new version of yassnippet where the old variable text is replaced by the newer yas-text.]

1

A solution with the skeleton-mode

Put this code in your .emacs

(define-skeleton eqnstar
  "Doc string"
  nil
  "\\begin{eqnarray*}\n"
  "  " _ " \n"
  "\\end{eqnarray*}")

Use it with M-x eqnstar.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .