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.

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.

share|improve this question
    
What do you mean with “return `` in the other cases”? –  Qrrbrbirlbel Jun 8 '13 at 20:59
    
@Qrrbrbirlbel it's a typo, sorry. –  Colas Jun 9 '13 at 15:16
add comment

3 Answers

up vote 12 down vote accepted
\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}
share|improve this answer
2  
\symbol{\numexpr`a-1+#1} may be even clearer. –  egreg Jun 8 '13 at 19:13
    
Does it work when #1<1 or #1>26? –  mbork Jun 8 '13 at 19:45
    
@mbork: No. It can't handle such thinks. –  Marco Daniel Jun 8 '13 at 22:00
    
@mbork: that can easily be extended to get something like aaab, see my edit –  Herbert Jun 9 '13 at 13:31
    
@Herbert: If you want to provide a bigger range you can use: \newcommand*\makeAlph[1]{% \ifnum#1<1\else \ifnum#1>26 \count0=#1 \divide\count0 by 26% \makeAlph{\the\count0}% \makeAlph{\numexpr(#1-26*\the\count0)}% \else\symbol{\numexpr96+#1}\fi\fi} –  Marco Daniel Jun 9 '13 at 13:51
add comment

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.

share|improve this answer
    
Dear @egreg, what does it mean fully expandable ? –  Colas Jun 8 '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 8 '13 at 19:41
add comment

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.

share|improve this answer
    
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 8 '13 at 18:50
1  
I'd use \DeclareExpandableDocumentCommand, so that \makealph can be used in an expansion context. –  egreg Jun 8 '13 at 19:23
    
@egreg: Thanks. –  Marco Daniel Jun 8 '13 at 19:27
    
@MarcoDaniel I've never seen such things... (expl3) What is it ? Is it a huge great new thing ? –  Colas Jun 8 '13 at 19:37
    
@Colas: Yes it's a huge and of course great thing ;-) –  Marco Daniel Jun 8 '13 at 22:00
add comment

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.