# How can I use a char as a counter?

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)


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.

• 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

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`