有没有办法根据给定的日期来调节输出?

有没有办法根据给定的日期来调节输出?

我希望能够打印根据日期是否与给定日期字符串匹配而变化的文本

例如假设有一个命令

\iftoday#1#2#3

#2如果\today匹配字符串则打印#1,否则打印#3

因此我建议的用法示例如下:

   \iftoday{February 3, 2016}{There's no garbage today}{Please put out the garbage} 

我尝试过各种命令,例如 pdftexcmd 的 strcmp,但都无济于事。

我怀疑问题可能与匹配生成的字符串有关\today。虽然屏幕上显示\today返回。当我比较字符串时,February 3, 2016我无法使其匹配。February 3, 2016

显然,如果我能通过以任何标准日期格式表示今天的日期来实现这个功能,我会非常高兴。

非常感谢您的任何建议。

答案1

如果您允许指定日期、月份和年份数值上那么这相当简单。诀窍在于 TeX 还定义了整数\day\month因此\year您可以使用将它们与提供的日期进行比较\ifnum。这允许您生成:

在此处输入图片描述

使用代码

\documentclass{article}
% \ifToday{day}{month}{year}{yes message}{no message}
% for numerical day, month and year
\newif\iftoday
\newcommand\ifToday[5]{\todayfalse% it's not today by default!
    \ifnum\day=#1\relax\ifnum\month=#2\relax\ifnum\year=#3\relax\todaytrue\fi\fi\fi%
    \iftoday#4\else#5\fi%
}

\begin{document}
    \ifToday{3}{2}{2016}{There's no garbage today}{Please put out the garbage today}

    \ifToday{4}{2}{2016}{There's no garbage tomorrow}{Please put out the garbage tomorrow}
\end{document}

答案2

PGF 的答案是:

\documentclass{article}
\usepackage{pgfkeys,pgfcalendar}
\newcommand\iftoday[1]{%
\pgfcalendarifdate{#1}{equals=\year-\month-\day}{There's no garbage today}{Please put out the garbage}}
\begin{document}

You know what to do:
\iftoday{2016-02-02}

\end{document}

答案3

这取决于您的日期格式:

在此处输入图片描述

\documentclass{article}

\makeatletter
\newcommand{\iftoday}[1]{%
  \ifnum\pdfstrcmp{\today}{#1}=0
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\begin{document}

\today

\iftoday{February 2, 2016}
  {There's no garbage today}% <true>
  {Please put out the garbage}% <false>

\iftoday{February 3, 2016}
  {There's no garbage today}% <true>
  {Please put out the garbage}% <false>

\end{document}

相关内容