I would like to do 2 things: first mirror a field and replace spaces with "-". If nothing is typed in the field, junto to the mirror. The code is:
\begin{theorem}[$1]
\label{thm-${1:$( ???? )}}
$0
\end{theorem}
I'm not sure I fully understand the question, this will mirror your first field replacing spaces with -
character:
# -*- mode: snippet -*-
# name: theorem
# key: theorem
# --
\begin\{theorem\}[$1]
\label\{thm-${1:$(replace-regexp-in-string "[[:space:]]" "-" yas-text)}\}
$0
\end\{theorem\}
P.S. I would also downcase the whole thing, just because it looks better, but to do this you would need to define auxiliary function in your config or this may get cumbersome.
yas-text
argument -- this may be more readable than inlining. Note that you'll end up with thm-
label if you leave first field empty, it's possible to inset \label
element only when first filed is not empty, but I'm not sure you want it...
Commented
Jul 28, 2015 at 15:03
Here is how I ended up doing. We can define a function as below and load it in emacs.
(defun string-for-latex-labels (string)
"format automatic labels for latex environment to use with snippet"
(setq string (replace-regexp-in-string "[[:punct:]]" "-" string))
(setq string (replace-regexp-in-string "[[:space:]]" "-" string))
(setq string (replace-regexp-in-string "-+" "-" string))
(setq string (replace-regexp-in-string "^-" "" string))
(setq string (replace-regexp-in-string "-$" "" string))
(setq string (downcase string))
)
Then we can just define a snippet as
# -*- mode: snippet -*-
# name: theorem
# key: theorem
# --
\begin\{theorem\}[$1]
\label{thm-${2:${1:$(string-for-latex-labels yas-text)}}}
$0
\end\{theorem\}
TAB
immediately point should jump to the mirror field?