我如何指定一个相对日期并让 LaTeX 插入生成的绝对日期?
例如这样:
\somedatecommand{next thursday}
其结果应为:
Thursday, 20.10.2011
或者:
20.10.2011
对 Babel 的支持也很好(例如对于非英语文档)。
答案1
你可以使用包裹datetime
格式化日期并引用包文档:
从 2.42 版本开始,datetime 包现在与 babel 兼容,但是您必须在 babel 包之后加载 datetime 包。
较难的部分是确定要打印的日期。这可以使用\AdvanceDate[n]
以下宏来实现包裹advdate
将日期提前指定的天数。然后只需确定我们需要提前多少天才能达到“下一周的某一天”。
要从当前日期以外的某一天开始,可以使用\SetDate[dd/mm/yyyy]
。
\documentclass{article}
\usepackage{xstring}% Needed for \IfStrEqCase
\usepackage{datenumber}% Date formatting
\usepackage{advdate}% Needed for \AdvanceDate
\usepackage{pgf}% For math
\usepackage[ddmmyyyy]{datetime}
\newdateformat{mydate}{\twodigit{\THEDAY}.\twodigit{\THEMONTH}.\THEYEAR}
\newcounter{dateOffset}%
\newcommand*{\SetDateOffsetForNext}[1]{%
\pgfmathsetcounter{dateOffset}{7-int(\the\value{datedayname})}% Initialize
\IfStrEqCase{#1}{%
{mon}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+1,7))}}%
{tue}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+2,7))}}%
{wed}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+3,7))}}%
{thu}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+4,7))}}%
{fri}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+5,7))}}%
{sat}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+6,7))}}%
{sun}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+7,7))}}%
}[\PackageError{\SetDateOffsetForNext}{Do not know "#1" as day of week}{}]%
\IfEq{\the\value{dateOffset}}{0}{\pgfmathsetcounter{dateOffset}{7}}{}%
}%
\newcommand*{\PrintDateForNext}[1]{%
\SetDateOffsetForNext{#1}% Determine date offset
\AdvanceDate[\value{dateOffset}]% Advance to specified day
\mydate\today% Print specified date
\AdvanceDate[-\value{dateOffset}]% Restore current day
is next #1\par% debug output
}
\begin{document}
\noindent
Working from current day:
Today is \mydate\today\par
\PrintDateForNext{mon}\par
\PrintDateForNext{tue}\par
\PrintDateForNext{wed}\par
\PrintDateForNext{thu}\par
\PrintDateForNext{fri}\par
\PrintDateForNext{sat}\par
\PrintDateForNext{sun}\par
\bigskip\noindent
Back to ``May the Fourth Be With You":
\SetDate[04/05/1977]% dd/mm/yyyy
Today is \mydate\today\par
\PrintDateForNext{mon}\par
\PrintDateForNext{tue}\par
\PrintDateForNext{wed}\par
\PrintDateForNext{thu}\par
\PrintDateForNext{fri}\par
\PrintDateForNext{sat}\par
\PrintDateForNext{sun}\par
\end{document}