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)

2 Answers 2

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.

1
  • 3
    Beware of going past z, however. This function won't automatically roll over to aa, ab, etc., if that's what you need. Commented Apr 13, 2018 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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