15

Is it possible to have a simple command \makeAlph which would do the following :

  • return a when it receives 1
  • return b when it receives 2
  • etc.
  • return nothing (or whatever ; actually that's not important for me) in the other cases ?

Thank you.

PS : I know how to do it with counters but this is not what I want.

Colas
  • 6,772
  • 4
  • 46
  • 96

3 Answers3

20

The LaTeX kernel already has it:

\makeatletter
\newcommand{\makeAlph}[1]{\@alph{#1}}
\makeatother

However this returns an error if the argument is greater than 26. If you want that nothing is returned for an out of range input, just copy the definition of \@alph without the error message:

\newcommand{\makeAlph[1]{%
  \ifcase #1\or a\or b\or c\or d\or e\or f\or g\or h\or
    i\or j\or k\or l\or m\or n\or o\or p\or q\or r\or
    s\or t\or u\or v\or w\or x\or y\or z\fi}

If you want to return ? for the out of range cases,

\newcommand{\makeAlph[1]{%
  \ifcase #1?\or a\or b\or c\or d\or e\or f\or g\or h\or
    i\or j\or k\or l\or m\or n\or o\or p\or q\or r\or
    s\or t\or u\or v\or w\or x\or y\or z\else ?\fi}

The advantage over the approaches shown in other answers is that the last two definitions use only fully expandable functions. Also \int_to_alph:n is fully expandable, of course.

egreg
  • 1,121,712
  • Dear @egreg, what does it mean fully expandable ? – Colas Jun 08 '13 at 19:38
  • @Colas That you can use the last version of \makeAlph in \edef or \write. Not so important, perhaps, in this application, but in many situations it's crucial. – egreg Jun 08 '13 at 19:41
17
\documentclass{article}%
\newcommand*\makeAlph[1]{\symbol{\numexpr96+#1}}
\begin{document}

\makeAlph{10}
\makeAlph{22}

This is the first letter of the alphabet : \makeAlph{1}

\end{document}

if you want to allow all numbers then use:

\documentclass{article}%
\newcommand*\makeAlph[1]{%
  \ifnum#1<1\else% do nothing if < 1
    \ifnum#1>26 a\makeAlph{\numexpr#1-26}% start loop
    \else\symbol{\numexpr96+#1}\fi\fi}
\begin{document}

This is the first letter of the alphabet : \makeAlph{1}

\makeAlph{10}  \makeAlph{44}
\makeAlph{-3}
\end{document}
15

There is also a nice expl3 implementation

\documentclass{article}%
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand \makealph { m }
 {
  \int_to_alph:n { #1 }
 }
\ExplSyntaxOff
\begin{document}

\makealph{1}

\makealph{5}

\makealph{35}

\end{document}

As pointed out the definition of \makealph should be done by \DeclareExpandableDocumentCommand so that it can be used in an expansion context.

If you want use a LaTeX2e solution you can use the package alphalph:

\documentclass[12pt]{article}%
\newcounter{mycounter}
\usepackage{alphalph}
\begin{document}

\setcounter{mycounter}{2}
\alph{mycounter}

\setcounter{mycounter}{35}

\alphalph{\value{mycounter}}

\alphalph{17}
\end{document}

Please note that the package alphalph requires a numerical input and not a counter.

Marco Daniel
  • 95,681
  • About the command : alph{}. I would like to have a command makeAlph that I can use in my text, to do something like : This is the first letter of the alphabet : \makeAlph{1}. – Colas Jun 08 '13 at 18:50
  • 2
    I'd use \DeclareExpandableDocumentCommand, so that \makealph can be used in an expansion context. – egreg Jun 08 '13 at 19:23
  • @MarcoDaniel I've never seen such things... (expl3) What is it ? Is it a huge great new thing ? – Colas Jun 08 '13 at 19:37
  • @Colas: Yes it's a huge and of course great thing ;-) – Marco Daniel Jun 08 '13 at 22:00