在 newcommand 中扩展宏参数

在 newcommand 中扩展宏参数

我想使用datetime's\monthname作为命令的一部分;考虑这个命令行pdflatex使用片段:

$ pdflatex
This is pdfTeX, Version 3.1415926-1.40.11 (TeX Live 2010)
 restricted \write18 enabled.
**\documentclass{article}
entering extended mode
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, dumylang, nohyphenation, lo
aded.

*\usepackage{datetime}
(/PATH/TO/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/PATH/TO/texlive/texmf-dist/tex/latex/base/size10.clo))

*\newdate{someDate}{30}{05}{2011}
(/PATH/TO/texlive/texmf-dist/tex/latex/datetime/datetime.sty
(/PATH/TO/texlive/texmf-dist/tex/latex/fmtcount/fmtcount.sty
(/PATH/TO/texlive/texmf-dist/tex/latex/base/ifthen.sty)
(/PATH/TO/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/PATH/TO/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)
(/PATH/TO/texlive/texmf-dist/tex/latex/fmtcount/fc-english.def)
(/PATH/TO/texlive/texmf-dist/tex/latex/fmtcount/fc-USenglish.def)
No configuration file fmtcount.cfg found.
))

*\newcommand\someDateStr{\getdateday{someDate} \monthname[\getdatemonth{someDate}] \getdateyear{someDate}}

*\typeout{\someDateStr}
30 \monthname[05] 2011

*\typeout{\monthname[\getdatemonth{someDate}]}
\monthname[05]

因此,看起来在构造中\monthname[\getdatemonth{someDate}],Latex 只会扩展\getdatemonth{someDate},但不会扩展\monthname部分;我怀疑它与方括号作为参数有关,因为如果我尝试使用edef,则会收到错误:

*\edef\someDateStr{\getdateday{someDate} \monthname[\getdatemonth{someDate}] \getdateyear{someDate}}
! Argument of \reserved@a has an extra }.
<inserted text> 
                \par 
<*> ...atemonth{someDate}] \getdateyear{someDate}}

? 

所以,问题是:我如何将其用作\monthname[\getdatemonth{someDate}]新命令的一部分,以使其结果为(在本例中)“May”?

提前感谢任何回复,
干杯!

答案1

datetime 的主要问题是它面向印刷日期而不是操纵它们。

“完全可扩展”的版本将是

\makeatletter
\newcommand{\xnewdate}[4]{\@namedef{date@#1}{/#2/#3/#4/}}
\def\xgetdateday#1{\expandafter\expandafter\expandafter\xget@I\csname date@#1\endcsname}
\def\xgetdatemonth#1{\expandafter\expandafter\expandafter\xget@II\csname date@#1\endcsname}
\def\xgetdateyear#1{\expandafter\expandafter\expandafter\xget@III\csname date@#1\endcsname}
\def\xget@I/#1/#2/#3/{#1}
\def\xget@II/#1/#2/#3/{#2}
\def\xget@III/#1/#2/#3/{#3}
\def\xgetdatemonthname#1{%
\ifcase\number\xgetdatemonth{#1}\relax
\or January%
\or February%
\or March%
\or April%
\or May%
\or June%
\or July%
\or August%
\or September%
\or October%
\or November%
\or December%
\fi}
\makeatother

\typeout{\xgetdateday{somedate} \xgetdatemonthname{somedate} \xgetdateyear{somedate}}

相关内容