如何使用 \newcommand 将数值“翻译”为单词?

如何使用 \newcommand 将数值“翻译”为单词?

我可以使用 \newcommand 将数值“翻译”成单词吗?

例如:

\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
}}

因此,当我输入时,\Calendar{2}{2}{1989}我期望输出如下February 2nd 1989

答案1

不使用任何包,按顺序使用条件\ifcase ... \or ... \fi。事实上,您只使用日历日期,可以假设#1范围在 1..12 和#21..31 之间。可以建立检查以确保某些日期不存在(例如 2001 年 2 月 29 日)。

在此处输入图片描述

\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}

答案2

你绝对应该使用专用包,但如果你想自己实现它,使用expl3它很容易。

\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}

在此处输入图片描述

使用\Calendar{31/12/2017}语法:

\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}

相关内容