6

Is there a command that does the reverse of \alph, i.e. takes a letter and gives me the corresponding number of the letter in the alphabet?

If there is, how about \Alph, \roman or \Roman?


Context:

I have a sectioning command \subtask for typesetting solutions to exercise sheets where I sometimes don't answer the subtasks in the "correct" order. The subtasks are numbered with small Latin letters using \alph.

I adjusted the command so that I can write \subtask[4] when I want to jump to the subtask "d". This is fine but \subtask[d] would be more user friendly. Hence the question.

2
  • Since tasks uses expl3, there is \int_from_alph:n; can you make a small compilable example of what you have now?
    – egreg
    Apr 12, 2016 at 16:21
  • @egreg: You misunderstood, I am not using the tasks package. I defined the sectioning command myself using scrartcl's \DeclareNewSectionCommand and adjusted it as described above. I decided not to include a MWE because I was of the opinion that the details of my implementation aren't relevant to the question. If you still think, an MWE would be helpful, I can add one. However, I think the answers are already perfectly good.
    – schtandard
    Apr 12, 2016 at 16:45

3 Answers 3

6

The backtick in numerical context can be used to get the character code of a letter. This can be used to calculate the position of the letter in the alphabet.

  • The definition range for the argument of \inversalph is a to z and A to Z for \inverseAlph.

  • The letter can be hidden inside a macro (or even nested macros).

\documentclass{article}
\newcommand*{\inversalph}[1]{%
  \the\numexpr(\expandafter`\romannumeral-`\x#1-`a+1)\relax
}
\newcommand*{\inversAlph}[1]{%
  \the\numexpr(\expandafter`\romannumeral-`\x#1-`A+1)\relax
}
\begin{document}
  a: \inversalph{a},
  b: \inversalph{b},
  k: \inversalph{k},
  z: \inversalph{z}

  A: \inversAlph{A},
  B: \inversAlph{B},
  K: \inversAlph{K},
  Z: \inversAlph{Z}

  \def\lettera{a}
  \def\letterz{z}
  \def\letteraa{\lettera}
  \def\letterzz{\letterz}
  \lettera: \inversalph{\lettera} = \inversalph{\letteraa},
  \letterz: \inversalph{\letterz} = \inversalph{\letterzz}

   Page: \inversalph{\alph{page}}
\end{document}

Result

Remarks:

  • \numexpr is used for the calculations.
  • \romannumeral-`\x is a trick to expand the following token multiple times.
5

A quick solution with \int_from_alph:n or \int_from_roman:n from expl3. Note that a and A does not matter here, as well as uppercase and lowercase roman figures are identical here.

The \setcounter{section}{\alphtonumber{w}} example was used to show that the macros are expandable.

\documentclass{article}


\usepackage{xparse}

\ExplSyntaxOn
\newcommand{\alphtonumber}[1]{%
  \int_from_alph:n{#1}
}


\newcommand{\romantonumber}[1]{%
  \int_from_roman:n{#1}
}

\ExplSyntaxOff

\usepackage{pgffor}


\begin{document}


\alphtonumber{a} -- \alphtonumber{z}

And \alphtonumber{M}\ is a pretty number and \romantonumber{MMXVI} is a good year (more or less), as well as \romantonumber{mdcccclxxiv} was a good year. 


\setcounter{section}{\alphtonumber{w}}

\section{Foo}

\end{document}

enter image description here

2

The number is always stored in the counter while \alph and the like are only for displaying the value.

For example if you define

\newcounter{foo}
\renewcommand{\thefoo}{\alph{foo}}

with

\thefoo

you get a, b, c, …

but with

\value{foo}

you can always access the integer value of the counter.

5
  • 2
    Downvoting without explaining why ist really nice …
    – Tobi
    Apr 12, 2016 at 17:06
  • 1
    I am not the downvoter, but I agree with your anger! (+1)
    – user31729
    Apr 12, 2016 at 17:14
  • I have more and more the impression that the politeness of some TeX.SX users steps down :-(
    – user31729
    Apr 12, 2016 at 17:21
  • @ChristianHupfer thanks for the empathy up-vote :-D
    – Tobi
    Apr 12, 2016 at 17:50
  • Not only empathy. I would have upvoted immediately after posting my solution, but I didn't catch you have answered too!
    – user31729
    Apr 12, 2016 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .