Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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).

share|improve this question

migrated from stackoverflow.com Nov 21 '12 at 16:07

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 6 down vote accepted

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}
share|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

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

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

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}
share|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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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