15

alphalph defines three methods for continuing a set of symbols if you hit the end.

  • alph continues like so: a,b,c…z,aa,ab,ac…
  • mult continues like so: a,b,c…z,aa,bb,cc…
  • wrap continues like so: a,b,c…z,a,b,c…

I'd like something that works like these, except that it continues as follows:

  • a,b,c…z,za,zb,zc…zy,zz,zza…

This looks weird for letters, obviously. But if you were defining something that takes a number as input and outputs that number of dots, say, then it makes sense. The actual use case is the following: I want to define something that takes a counter as input and outputs that number represented by dice (using epsdice). At the moment, I just explicitly define the number function like so:

\newcommand*{\dicecount}[1]{%
  \expandafter\@dicecount\csname c@#1\endcsname%
}
\newcommand*{\@dicecount}[1]{%
  \ifcase#1\or\epsdice{1}\or\epsdice{2}\or\epsdice{3}%
  \or\epsdice{4}\or\epsdice{5}\or\epsdice{6}%
  \or\epsdice{3}\epsdice{4}%
  \or\epsdice{6}\epsdice{2}%
  \or\epsdice{5}\epsdice{4}%
  \or\epsdice{5}\epsdice{5}%
  \or\epsdice{6}\epsdice{5}%
  \or\epsdice{6}\epsdice{6}%
  \else\@ctrerr\fi%
}

(Note I added some randomness here, but I'd settle for a case where it just outputs a bunch of sixes and then whatever. Bonus points if you can build in this pseudorandomness)

I tried looking at the code of alphalph, but I'm not at the stage where I can understand that. But I guess for the "one shot" case, patching alphalph is probably not what I want, and there's a simpler way of just testing whether a number is bigger than 6, adding a die if it is, and so on…

lockstep
  • 250,273
Seamus
  • 73,242
  • 1
    I think that your proposition of "numbering" is more logical than the standard propositions because it respects the lexical order. – projetmbc Dec 07 '11 at 17:52
  • You can make alph obey lexical order by prepending a suitable number of "zeroes". This is how we make normal numbers obey lexical order: 01,02,03… – Seamus Dec 07 '11 at 20:08
  • The feature that I was lookinf for this kind of feature : http://tex.stackexchange.com/questions/37508/how-to-build-one-efficient-alpha-numbering . – projetmbc Dec 08 '11 at 09:24

2 Answers2

12
\documentclass[a4paper]{article}
\usepackage{expl3}
\usepackage{epsdice}

\ExplSyntaxOn
\cs_new:cpn {@weirdalph} #1
  {
   \prg_replicate:nn {\int_div_truncate:nn {#1}{26}}{z}
   \int_to_alph:n {\int_mod:nn {#1}{26}}
  }

\cs_new:cpn {@dicecount} #1
  {
   \prg_replicate:nn {\int_div_truncate:nn {#1}{6}}{\epsdice{6}}
   \exp_args:Nx \epsdice{\int_mod:nn{#1}{6}}
  }
\ExplSyntaxOff

\makeatletter
\def\weirdalph#1{\expandafter\@weirdalph\csname c@#1\endcsname}
\def\dicecount#1{\expandafter\@dicecount\csname c@#1\endcsname}
\makeatother


\newcounter{pippo}

\begin{document}

\setcounter{pippo}{4}

\arabic{pippo}: \weirdalph{pippo}

\setcounter{pippo}{27}

\arabic{pippo}: \weirdalph{pippo}

\setcounter{pippo}{53}

\arabic{pippo}: \dicecount{pippo}

\end{document}

With \int_div_truncate:nn we compute the quotient, that is, the number of z's, which are inserted with \prg_replicate:nn (thanks to Bruno Le Floch). Then we compute the remainder (\int_mod:nn) converting it into a character via \int_to_alph:n. It's easy to modify it for 6 and \epsdice.

enter image description here

Peter Grill
  • 223,288
egreg
  • 1,121,712
  • 1
    Since you're not using the step argument of \prg_stepwise_function:nnnN (i.e., you're simply copying the z a given number of times), you should use \prg_replicate:nn {\int_div_truncate:nn {#1}{26}}{z} instead. Also, I think the preferred way to get non-expl-syntax functions is to use xparse (although it is an overkill here). – Bruno Le Floch Dec 07 '11 at 16:00
  • 1
    @BrunoLeFloch One function a day. :) – egreg Dec 07 '11 at 16:37
  • 1
    @BrunoLeFloch Well, as it's being given an \@... name it would be easier to simply \makeatletter in addition to \ExplSyntaxOn and create \@weirdchar directly. – Joseph Wright Dec 07 '11 at 16:48
6

I came up with this-not really very clean code, but it works (without the epsdice package, I don't have it installed).

\documentclass{article}

\makeatletter
\newcounter{dicecount}
\newcommand{\setdicesize}[1]{\def\@dicesize{#1}}
\setdicesize{6}
\newcommand{\dice}[1]{%
  \setcounter{dicecount}{#1}%
  \@whilenum \c@dicecount>\@dicesize \do {%
    \texttt{\@dicesize}\addtocounter{dicecount}{-\@dicesize}%
  }%
  \ifnum \c@dicecount>0 \texttt{\number\c@dicecount}\fi
}
\makeatother

\begin{document}
\begin{enumerate}
\item \dice{1}
\item \dice{2}
\item \dice{3}
\item \dice{4}
\item \dice{5}
\item \dice{6}
\item \dice{7}
\item \dice{8}
\item \dice{9}
\item \dice{10}
\item \dice{11}
\item \dice{12}
\item \dice{13}
\item \dice{14}
\item \dice{15}
\item \dice{16}
\item \dice{17}
\item \dice{18}
\end{enumerate}
\end{document}
mbork
  • 13,385