Creating Code from Emacs

How can I quickly write the following code in emacs?

\newcommand{\cA}{\mathcal A} \newcommand{\cB}{\mathcal B} \newcommand{\cC}{\mathcal C} ... ... \newcommand{\cY}{\mathcal Y} \newcommand{\cZ}{\mathcal Z} 

Is there a faster way than writing

 A B C D . . . Y Z 

and then do a macro on each line? (change A to \ newcommand {\ cA} {\ mathcal A})

+4
source share
9 answers

In general, the first time you encounter such a problem, you should use keyboard macros, as JB already said.

For the second time, check out this very interesting article by Steve Yegg: http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-22.html , which contains solutions to problems like yours.

For your convenience and my own coverage, I really went ahead and tested it:

I would start with

  A
 ...
 A

26 times

and execute

Mx replace-regexp

  A
 \\ newcommand {\\ c \, (string (+? A \ #))} {\\ mathcal \, (string (+? A \ #))}
+5
source

I agree that the keyboard macro will get the result faster.

More fun - software approach:

 (loop for n from (string-to-char "A") to (string-to-char "Z") for c = (char-to-string n) do (insert (concat "\\newcommand{\\c" c "}{\\mathcal " c "}\n"))) 
+7
source

A to Z total 26 lines. You spend more time automating generation than natural ones using keyboard macros, imho.

+4
source

It depends on your background. I would just type M-! and:

 perl -e"print map {q(\newcommand{\c).$_.q(}{\mathcal ).qq($_}\n)} A..Z 
+3
source

Do you know the rectangle? ?

There is also a string-rectangle :

  • place point (cursor) at the beginning of the text, mark ( C-SPC )
  • point at the beginning of the last line
  • type Mx string-rectangle
  • enter the string (\ newcommand {\ c)

This will insert this line before each line with a label.

+3
source

I don’t have enough karma or anything else to comment, but I wanted to improve the HD style a bit.

Here is the original:

 (loop for n from (string-to-char "A") to (string-to-char "Z") for c = (char-to-string n) do (insert (concat "\\newcommand{\\c" c "}{\\mathcal " c "}\n"))) 

First, Emacs Lisp has a read syntax for characters. Instead of (string-to-char "X") you can simply write ?X Then you can use the printf format style instead of char-to-string and concat to get the final result:

 (loop for n from ?A to ?Z do (insert (format "\\newcommand{\\c%s}{\\mathcal %s}\n" nn))) 

Now it is concise enough to enter text without thought into the M-: prompt.

I will also point out that TeX also has macros, if it is really TeX.

Edit: Another style tip for Joe Casadonte; (incf foo) much easier to type than (setq foo (+ foo 1)) .

+3
source

Or an interactive way if you want to do this one at a time

 (defun somefun(inputval) (interactive "sInputVal: ") (insert ( format "\\newcommand{\\c%s}{\\mathcal %s}" inputval inputval) ) ) 

just eval and mx somefun every time you want text

+1
source

Not quite the answer.

For people who do not use emacs or vim, I would like to add that you can open Excel and use its autocomplete function and text concatenation function to create such code.

+1
source

I like the HD loop a little better than mine, but here's an alternative, more general approach:

 (defun my-append (str) "Substitute AZ in STR and insert into current buffer. Looks for '--HERE--' (without quotes) in string STR, substitutes the letters AZ in the string and then inserts the resulting string into the current buffer." (interactive "sInput String: ") (let ((lcv 0) letter newstr) (while (< lcv 26) (setq letter (+ ?A lcv)) (insert (replace-regexp-in-string "--HERE--" (char-to-string letter) str tt) "\n") (setq lcv (+ lcv 1))))) 

It should not be too difficult to combine the two.

0
source

All Articles