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}
  • Do you mean when user presses TAB immediately point should jump to the mirror field? – Mark Karpov Jul 27 '15 at 17:04
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.

  • 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 Jul 28 '15 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... – Mark Karpov Jul 28 '15 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\}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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