5

I'd like to be able to print text that varies depending on whether the date matches a given date string

For example suppose there was a command

\iftoday#1#2#3

which printed #2 if \today matched the string in #1 and #3 otherwise.

So a sample of my proposed usage would be:

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

I've tried various commands like pdftexcmd's strcmp, to no avail.

I suspect the problem may have to do with matching the string generated by \today. While on screen it appears that \today returns February 3, 2016. I can't get it to match February 3, 2016 when I compare strings.

Obviously, I'd be perfectly happy if I could get this to work by represent today's date in any of the standard date formats.

Thanks very much in advance for any suggestions.

Leo Simon
  • 2,199
  • You can highlight code in your post using back-ticks. For code-blocks indent them by four spaces or use the {} on the gui. –  Feb 03 '16 at 06:58
  • \todayis already fairly complex; from texdef: \today: macro:->\ifcase \month \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 \space \number \day , \number \year. You might have better luck with pgfcalendar's \pgfcalendarifdate{ date }{ tests }{ code }{ else code }. – jon Feb 03 '16 at 07:00

3 Answers3

4

If you will allow the day, month and year to be specified numerically then this is fairly straightforward. The trick is that TeX also defines integers \day, \month and \year so you can compare these with the supplied date using \ifnum. This allows you to produce:

enter image description here

using the code

\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}
  • Thanks very much. I tried some of the other solutions but they conflicted with one of my default packages, can't figure out which one. but this one doesn't. If anybody's using it in the US, you would want to reverse day and month in the \ifnum line. – Leo Simon Feb 03 '16 at 15:18
  • LeoSimon -- The conflict is surprising. With mine it is possible; surprising, but possible. However, for there to be a conflict with @werner's solution is downright incredible (in the literal sense of the word). – jon Feb 03 '16 at 15:33
4

A PGF answer:

\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}
jon
  • 22,325
  • Thanks for this @jon. More generally, is it possible to extend your pgf answer to condition on a range of months or years? To be concrete, I'd like to be able to display the answer to a question in my lecture notes, but only after this years problem set has been handed in, but before next year's class has started. Say for example, between 4/18/2016 and 1/1/2017? Thanks for your help. I'll use any date format that works. – Leo Simon Apr 15 '16 at 16:21
  • @LeoSimon -- Do you mean: if the date falls between range X and Y, then print A; otherwise print B? – jon Apr 15 '16 at 17:25
  • yes, that's what I meant. Sorry for not being more explicit – Leo Simon Apr 15 '16 at 22:27
2

This does depend on your date format:

enter image description here

\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}
Werner
  • 603,163