0

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}
1
  • Do you mean when user presses TAB immediately point should jump to the mirror field? Commented Jul 27, 2015 at 17:04

2 Answers 2

0

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.

2
  • Thanks, @Mark. Here is how to complement the code with what you suggested in P.S.. You can substitute this line in your answer: \label{thm-${1:$(downcase (replace-regexp-in-string "[[:space:]]" "-" yas-text))}}
    – Diogo
    Commented Jul 28, 2015 at 14:58
  • @Diogo, you are welcome. I prefer keep transforming code minimal, because it's already inserted in surrounding text and all this can quickly get messy. You can define your own function for complex processing and use it with 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
0

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\}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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