2

Could I 'translate' a numerical value into a word with \newcommand?

For example:

\newcommand\Calender[1]{#1#2#3
IfEqCase{#1}{
{1}{January}
{2}{February}
{3}{March}
{4}{April}
{5}{May}
{6}{June}
{7}{July}
{8}{August}
{9}{September}
{10}{October}
{11}{November}
{12}{December}}
IfEqCase{#2}{
{1}{1st}
{2}{2nd}
{3}{3rd}
\\ use 'th' for any other number
}}

So when I type \Calendar{2}{2}{1989} I would expect an output as February 2nd 1989

4
  • 2
    If this is really what you want to do, (and not a simple example of a more general problem) why not use the datetime2 package.
    – Alan Munn
    Aug 11, 2017 at 19:00
  • Thanks, but would it be possible without using a package? Aug 11, 2017 at 19:11
  • 2
    You can always do something without a package, since you can write your own code to duplicate the package. The advantage of using well-known packages (at least in user documents) is that (i) they are usually written by skilled TeX programmers and (ii) they deal with corner cases you might not have thought of. If you're writing your own package or class, however, the decision to add a dependency or not is more nuanced.
    – Alan Munn
    Aug 11, 2017 at 19:57
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. Apr 23, 2018 at 9:19

2 Answers 2

6

Without using any package, condition sequentially using \ifcase ... \or ... \fi. The fact that you're only using calendar dates, one can assume that #1 will range between 1..12 and #2 between 1..31. One can build in checks to ensure that certain dates do not exist (like February 29th 2001, say).

enter image description here

\documentclass{article}

\newcommand{\Calendar}[3]{%
  \ifcase #1\or% 0
    January\or % 1
    February\or % 2
    March\or % 3
    April\or % 4
    May\or % 5
    June\or % 6
    July\or % 7
    August\or % 8
    September\or % 9
    October\or % 10
    November\or % 11
    December% 12
  \fi
  \space
  #2%
  \ifcase #2\or% 0
    st\or % 1
    nd\or % 2
    rd\or % 3
    th\or th\or th\or th\or th\or th\or th\or % 4, 5, 6, 7, 8, 9, 10,
    th\or th\or th\or th\or th\or th\or th\or th\or th\or th\or % 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
    st\or % 21
    nd\or % 22
    rd\or % 23
    th\or th\or th\or th\or th\or th\or th\or % 24, 25, 26, 27, 28, 29, 30
    st% 31
  \fi
  \space
  #3%
}

\begin{document}

\Calendar{2}{2}{1989}

\Calendar{1}{31}{1990}

\Calendar{3}{23}{1991}

\end{document}
1
  • You could also make the input more user friendly with \newcommand{\Calendar}[1]{\parseCalendar#1} \def\parseCalendar#1/#2/#3{...}, which would allow \Calendar{1/10/1989}.
    – Alan Munn
    Aug 11, 2017 at 20:20
3

You should definitely use a dedicated package, but in case you want to implement it yourself, with expl3 it's easy.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\Calendar}{mmm}
 {% #1 = month, #2 = day, #3 = year
  \int_case:nn { #1 }
   {
    {1}{January}
    {2}{February}
    {3}{March}
    {4}{April}
    {5}{May}
    {6}{June}
    {7}{July}
    {8}{August}
    {9}{September}
    {10}{October}
    {11}{November}
    {12}{December}
   }
  \nobreakspace
  #2
  \int_case:nnF { #2 }
   {
    {1}{st} {2}{nd} {3}{rd}
    {21}{st} {22}{nd} {23}{rd}
    {31}{st}
   }
   {th}% other cases
  \nobreakspace
  #3
 }
\ExplSyntaxOff

\begin{document}

\Calendar{1}{20}{1999}

\Calendar{2}{21}{2001}

\Calendar{3}{11}{2003}

\Calendar{5}{12}{2005}

\Calendar{7}{13}{2005}

\Calendar{3}{31}{2007}

\end{document}

enter image description here

With \Calendar{31/12/2017} syntax:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\Calendar}{m}
 {
  \faceb_calendar:n { #1 }
 }

\cs_new:Nn \faceb_calendar:n
 {
  \__faceb_calendar:w #1 \q_stop
 }
\cs_new:Npn \__faceb_calendar:w #1/#2/#3 \q_stop
 {% #1 = month, #2 = day, #3 = year
  \int_case:nn { #1 }
   {
    {1}{January}
    {2}{February}
    {3}{March}
    {4}{April}
    {5}{May}
    {6}{June}
    {7}{July}
    {8}{August}
    {9}{September}
    {10}{October}
    {11}{November}
    {12}{December}
   }
  \nobreakspace
  #2
  \int_case:nnF { #2 }
   {
    {1}{st} {2}{nd} {3}{rd}
    {21}{st} {22}{nd} {23}{rd}
    {31}{st}
   }
   {th}% other cases
  \nobreakspace
  #3
 }
\ExplSyntaxOff

\begin{document}

\Calendar{1/20/1999}

\Calendar{2/21/2001}

\Calendar{3/11/2003}

\Calendar{5/12/2005}

\Calendar{7/13/2005}

\Calendar{3/31/2007}

\end{document}
2
  • And an expl3 version of the / delimited input...?
    – Alan Munn
    Aug 11, 2017 at 21:38
  • @AlanMunn Added
    – egreg
    Aug 12, 2017 at 8:47

You must log in to answer this question.

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