views:

392

answers:

9

How can I write the following code quickly 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 way faster than writing

A
B
C
D
.
.
.
Y
Z

and then doing macro on each line? (changing A to \newcommand{\cA}{\mathcal A})

+3  A: 

A to Z is only 26 lines. You'd waste more time automating generation than just naturally using keyboard macros, imho.

JB
+1 just define a macro when you do the first one(including the move to the next line), then execute the macro with a repeat-count of 25 and you're done.
Paul
+7  A: 

I agree that a keyboard macro will get the result the quickest.

More fun is a programmatic 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")))
HD
A: 

I like HD's loop a little better than mine, but here's an alternate, more generalized approach:

(defun my-append (str)
  "Substitute A-Z in STR and insert into current buffer.

Looks for '--HERE--' (without quotes) in string STR, substitutes the
letters A-Z 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 t t) "\n")

      (setq lcv (+ lcv 1)))))

Shouldn't be too hard to combine the two.

Joe Casadonte
A: 

Or the interactive way if you want to do them one at a time

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

just eval and M-x somefun every time you want the text

Andrew Cox
+5  A: 

In general, the first time you are confronted with this kind of problem, you'd use keyboard macros, as JB already said.

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

For your convenience, and my own illumination, I actually went ahead and tested it:

I would start with

A
...
A

26 times

and do a

M-x replace-regexp

A
\\newcommand{\\c\,(string (+ ?A \#))}{\\mathcal \,(string (+ ?A \#))}
now that is the real mans way of doing it :)
Andrew Cox
Indeed. </Teal'c's voice>
J.F. Sebastian
+3  A: 

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

perl -e"print map {q(\newcommand{\c).$_.q(}{\mathcal ).qq($_}\n)} A..Z
J.F. Sebastian
+2  A: 

Do you know the rectangle selection ?

There's also string-rectangle:

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

This will insert that string before each line since the mark.

Leonel
+3  A: 

I don't have enough karma or whatever to comment, but I wanted to improve HD's 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 off, Emacs Lisp has reader syntax for chars. Instead of (string-to-char "X"), you can just write ?X. Then, you can use the printf-style format instead of char-to-string and concat to produce the final result:

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

Now it's concise enough to type without thinking into the M-: prompt.

I will also point out that TeX has macros too, if this is indeed TeX.

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

jrockway
A: 

Not exactly an answer.

For people who don't use emacs or vim, I'd like to add that you can open Excel and use its autofill feature and text concatenation function to generate such code.

RamyenHead