5

Each week I prepare notes in TeX for a Wednesday class. Mostly I prepare the notes a couple of days before. Often the notes are copied and modified from previous notes. I add the date to the notes corresponding to the date of the class. Currently I do one of two things:

  • use \today and try to remember to compile the document on the day of the class; or
  • write the date manually and try to remember to update the date from the previous notes.

Needless to say that most of the time I end up forgetting to update the dates with either of these strategies.


My question is: how could I insert the date of next Wednesday in the TeX document (or today's date if it is Wednesday)?

(I know the advdate package but it seems to only allow advancing a fixed number of days.)

4

Use the datenumber and etoolbox packages for counting and checking, respectively.

\documentclass{article}

\usepackage{datenumber,etoolbox}

\newbool{weddate}
\ifnumcomp{\arabic{datedayname}}{=}{3}{\setbool{weddate}{true}}{\setbool{weddate}{false}}

\newcommand\nextwed{%
  \unlessboolexpr{bool {weddate} }{%
    \addtocounter{datenumber}{1}%
    \setdatebynumber{\arabic{datenumber}}%
    \ifnumcomp{\arabic{datedayname}}{=}{3}{\setbool{weddate}{true}}{\setbool{weddate}{false}}%
  }%
  \datedate\setbool{weddate}{false}%
}

\begin{document}

\noindent
Next Wednesday is \nextwed. \\
The Wednesday after that is \nextwed.\\
Then we come to \nextwed.

\end{document}

Note this does not change the \today command, that will still give you today. The above code gives the following result:

enter image description here

Using \datedate after each \nextwed call will print the same date over and over. The \ifnumcomp check in the preamble is necessary in case today is Wednesday (in that case \nextwed does nothing, just prints the date).

Awesome that you're preparing before your class! :)

2

Here is a solution using the datetime2 package and expl3. The \badroit_find_next_date_for_dow:nnn function is fairly general: it allows one to find the next (Monday or Tuesday or ... as indicated by the second argument) starting from the date specified in the first argument and stores the resulting date in the third argument—in a form that functions from datetime2 can conveniently use.

\documentclass{article}
\usepackage[english]{babel}
% Use option 'showdow' to have the day of week printed by default
\usepackage[calc,en-GB]{datetime2}
\usepackage{xparse}

\ExplSyntaxOn

\newcount \badroit_tmp_count

% #1: <name> (in the sense of datetime2) of the start date
% #2: number of days (offset)
% #3: <name> (in the sense of datetime2) used to store the resulting date
\cs_new_protected:Npn \badroit_compute_offset_date:nnn #1#2#3
  {
    \DTMsaveddateoffsettojulianday {#1} {#2} { \badroit_tmp_count }
    \DTMsavejulianday {#3} { \number \badroit_tmp_count }
  }

\cs_generate_variant:Nn \badroit_compute_offset_date:nnn { nx }

% #1: <name> of the start date
% #2: which day to look for (0 for Monday, 1 for Tuesday, etc.)
% #3: <name> where the computed date is to be saved
\cs_new_protected:Npn \badroit_find_next_date_for_dow:nnn #1#2#3
  {
    \badroit_compute_offset_date:nxn {#1}
      { \int_mod:nn { 7 + (#2) - \DTMfetchdow {#1} } { 7 } }
      {#3}
  }

% #1: which day to look for (0 for Monday, 1 for Tuesday, etc.)
\cs_new_protected:Npn \badroit_display_next_chosen_dow:n #1
  {
    \DTMsavenow { badroit_display_next_chosen_dow.today }
    \badroit_find_next_date_for_dow:nnn
      { badroit_display_next_chosen_dow.today }
      {#1}
      { badroit_display_next_chosen_dow.result }
    % Use \DTMusedate to have the printed day of week start in lowercase if
    % the language allows it (e.g., with \usepackage[french]{babel}).
    \DTMUsedate { badroit_display_next_chosen_dow.result }
  }

\NewDocumentCommand { \NextWednesday } { }
  {
    \badroit_display_next_chosen_dow:n { 2 } % 2 is the number for Wednesday
  }

\ExplSyntaxOff

\begin{document}

Next \DTMenglishweekdayname{2} is \NextWednesday.

\end{document}

Screenshot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.