2

I have this LaTeX code:

\begin{enumerate}[a]

\affiliation{...}

\affiliation{...}

\affiliation{...}

 ...

\affiliation{...}

\end{enumerate}

and, using a script written in Emacs Lisp, I want to obtain:

\affiliation[a]{...}

\affiliation[b]{...}

...

\affiliation[n]{...}

I started from the following code thinking I could change it 'quite easily' in order to resolve my problem, but I failed.

Here it is my code:

(perform-replace "\\\\affiliation{"
     `((lambda (data count)
                   (concat "\\\\affiliation{"(number-to-string (+ 1 count))"}"))) nil t nil 1 nil a z)

How can I modify number-to-string to obtain lowercase letters instead of numbers?

A note: \begin{enumerate}[a] could be absent, so first occurence of \affiliation{...} is replaced using the following code:

(perform-replace "\\\\begin{enumerate}\\[a\\]\n\n\\\\affiliation{" "\n\n\\\\affiliation[a]{" t t nil 1 nil a z)
Drew
  • 75,699
  • 9
  • 109
  • 225
Onner Irotsab
  • 431
  • 2
  • 9

2 Answers2

2

You probably want the make-string function. If count starts at 0 and counts up, then:

(make-string 1 (+ ?a count))

will go a, b, c, etc.

Sue D. Nymme
  • 1,406
  • 12
  • 13
  • 3
    Beware of going past `z`, however. This function won't automatically roll over to `aa`, `ab`, etc., if that's what you need. – Sue D. Nymme Apr 13 '18 at 20:55
2

For your specific example, you can also use

C-M-% affiliation RET \&[\,(string (+ ?a \#))] RET
  • C-M-% is the default binding of query-replace-regexp
  • affiliation is regexp to search for
  • \&[\,(string (+ ?a \#))] is the replacement pattern
    • \& is the whole match, that is, affiliation
    • \, runs a Lisp expression after it and captures the result
    • (string char) converts a character (number) to string
    • \# is the number of replacements done so far (starting with zero)

Also see C-h f query-replace-regexp

xuchunyang
  • 14,302
  • 1
  • 18
  • 39