Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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...

share|improve this question
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 at 11:03
3  
Besides this you really ought to not use eqnarray at all. –  daleif Jul 7 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 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. –  Sean Allred Jul 8 at 0:22

2 Answers 2

up vote 5 down vote accepted

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.]

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.