質問
emacsで次のコードをすばやく作成するにはどうすればよいですか
\newcommand{\cA}{\mathcal A}
\newcommand{\cB}{\mathcal B}
\newcommand{\cC}{\mathcal C}
...
...
\newcommand{\cY}{\mathcal Y}
\newcommand{\cZ}{\mathcal Z}
書くよりも速い方法はありますか
A
B
C
D
.
.
.
Y
Z
そして各行でマクロを実行しますか? (Aを\ newcommand {\ cA} {\ mathcal A}に変更)
解決
一般に、この種の問題に初めて直面したときは、JBが既に述べたように、キーボードマクロを使用します。
2回目は、Steve Yeggeによる非常に興味深い記事をご覧ください。 http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-22.html には、あなたとまったく同じ問題の解決策が含まれています。
あなたの便宜と、私自身のイルミネーションのために、私は実際に先に進んでテストしました:
始めます
A ... A
26回
そして
M-x replace-regexp
A \\newcommand{\\c\,(string (+ ?A \#))}{\\mathcal \,(string (+ ?A \#))}
他のヒント
キーボードマクロが最も早く結果を得ることに同意します。
より楽しいのはプログラムによるアプローチです:
(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")))
AからZはわずか26行です。キーボードマクロを自然に使用するよりも、生成の自動化に多くの時間を浪費することになります。
背景に依存します。 M-!
と入力するだけです:
perl -e"print map {q(\newcommand{\c).背景に依存します。 M-!
と入力するだけです:
<*>.q(}{\mathcal ).qq(背景に依存します。 M-!
と入力するだけです:
<*>}\n)} A..Z
長方形の選択を知っていますか?
string-rectangle
もあります:
- テキストの先頭にポイント(カーソル)を置き、マーク(
C-SPC
) - 最後の行の先頭にポイントを置きます
- type
M-x string-rectangle
- 文字列を入力(\ newcommand {\ c)
これにより、マーク以降の各行の前にその文字列が挿入されます。
カルマなどコメントするのに十分なものがありませんが、HDのスタイルを少し改善したかったです。
オリジナルは次のとおりです。
(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")))
まず、Emacs Lispにはcharsのリーダー構文があります。 (string-to-char&quot; X&quot;)
の代わりに、単に?X
と書くことができます。次に、 char-to-string
および concat
の代わりにprintfスタイルの format
を使用して、最終結果を生成できます。
(loop for n from ?A to ?Z
do (insert (format "\\newcommand{\\c%s}{\\mathcal %s}\n" n n)))
これで M-:
プロンプトを考えずに入力するだけで十分に簡潔になりました。
TeXにもマクロがあることを指摘します(これが実際にTeXである場合)。
編集:Joe Casadonteのスタイルに関するもう1つのアドバイス。 (incf foo)
は、(setq foo(+ foo 1))
よりも入力がはるかに簡単です。
または、一度に1つずつ実行する場合はインタラクティブな方法
(defun somefun(inputval)
(interactive "sInputVal: ")
(insert ( format "\\newcommand{\\c%s}{\\mathcal %s}" inputval inputval) )
)
テキストが必要なときはいつもevalとM-x somefunだけ
正確な答えではありません。
emacsやvimを使用していない人のために、Excelを開いてその自動入力機能とテキスト連結機能を使用してそのようなコードを生成できることを追加したいと思います。
私はHDのループが私のものよりも少し好きですが、代替の、より一般化されたアプローチがあります:
(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)))))
この2つを組み合わせるのはそれほど難しくないはずです。