7

Getting the next letter of the alphabet is no problem in most programming languages I know. However, in LaTeX, it doesn't seem to be that trivial.

I have looked at how to make 26 macros (here at stackoverflow) but that question and its answers does not involve manipulations of the underlying unicode or something like that.

It is fine if the boundary case (e.g. the lowercase “z”) is not handled at all. I was thinking myself of something like the following:

\mydefalphabet abcdefghijklmnopqrstuvwxyz \mydefalphabet
\mydefalphabetshifted bcdefghijklmnopqrstuvwxyza \mydefalphabetshifted

Can I return in this case the symbol at the shifted range when given the nonshifted range perhaps?

The envisioned command would be:

\newcommand\nextletter[1]{ 
}

And used as $\nextletter{i}$ in math mode. I can then use $\robotindex$ (equal to i) and $\other{\robotindex}$ in my formulas (with other equivalent to nextletter).

6

Approach 1

This works for miniscule letters in the a-z range and has an optional argument (the shifting amount).

Code

\documentclass{article}
\newcommand*{\nextLetter}[2][1]{%
    \edef\numLet{\expandafter\number\expandafter`#2}%
    \edef\numNewLet{\number\numexpr\numLet+#1\relax}%
    \ifnum\numNewLet>122\relax%
        \edef\numNewLet{\number\numexpr\numNewLet-26\relax}%
    \fi%
    \expandafter\char\expandafter\numNewLet%
}
\def\robotindex{i}
\begin{document}
\nextLetter{a}%           a => b
\nextLetter{z}%           z => a
\nextLetter[13]{a}%       a => n
\nextLetter{\robotindex}% i => j
\end{document}

Approach 2

This approach uses the macros \myAlphabet and \myAlphabetShifted to look for the “next” letter.

Code

\documentclass{article}
\usepackage{xstring}
\def\myAlphabet{abcdefghijklmnopqrstuvwxyz}
\def\myAlphabetShifted{bcdefghijklmnopqrstuvwxyza}
\newcommand*{\nextLetter}[1]{%
    \StrPosition{\myAlphabet}{#1}[\myPosition]%
    \StrChar{\myAlphabetShifted}{\myPosition}%
}
\def\robotindex{i}
\begin{document}
\nextLetter{a}%           a => b
\nextLetter{z}%           z => a
\nextLetter{a}%           a => b
\nextLetter{\robotindex}% i => j
\end{document}
| improve this answer | |
  • Both approaches work flawlessly! Especially the second one will be trivial for me to use also for Greek letters. It's now super easy to be consistent through my papers, thanks! – Anne van Rossum Nov 22 '12 at 13:12
6

Here's a way:

\documentclass{article}
\newcommand{\robotindex}{i}
\newcommand{\nextletter}[1]{\expandafter\donextletter\expandafter{#1}}
\newcommand{\donextletter}[1]{\ifnum`#1=122 \else\char\numexpr`#1+1\relax\fi}

\begin{document}
\nextletter{a}

\nextletter{\robotindex}

X\nextletter{z}X
\end{document}

The output is

b
j
XX

| improve this answer | |
  • @StephanLehmke Because I made a mistake. :) – egreg Nov 21 '12 at 16:21
1

You can just use Alph{}, \alph{} with a counter to get the corresponding alphabetic letter:

enter image description here

Code:

\documentclass{article}
\usepackage{pgffor}%  \foreach is my preferred looping

\newcounter{CharNumber}% can probably do with out this, but I know this works.

\begin{document}
\noindent
\foreach \x in {1,...,26} {%
    \setcounter{CharNumber}{\x}%
    \Alph{CharNumber}%
}
\par\noindent
\foreach \x in {1,...,26} {%
    \setcounter{CharNumber}{\x}%
    \alph{CharNumber}%
}
\par\noindent
\foreach \x in {1,...,26} {%
    \setcounter{CharNumber}{\x}%
    \Roman{CharNumber}, %
}
\par\noindent
\foreach \x in {1,...,26} {%
    \setcounter{CharNumber}{\x}%
    \roman{CharNumber}, %
}
\end{document}
| improve this answer | |
  • This goes from numbers to letters, how can it be done the other way around? Something like this? \newcounter{CharNumber} \newcommand\nextletter[1]{ \setcounter{CharNumber}{#1} \addtocounter{CharNumber}{1} \Alph{CharNumber} } (sorry for the formatting here in the comment section) – ondervloei Nov 21 '12 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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