LaTeX forum ⇒ Text FormattingDate Arithmetic or Calculation Topic is solved

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm

Date Arithmetic or Calculation

Postby Pangur » Mon Jul 22, 2019 9:35 pm

My wife supplies me with material for a minister's manuscript booklet that I print prior to each Sunday. Usually, the booklet is printed several days (or even a week or two) in advance of the Sunday. I do not have much use for \today as it is not the day on which I print the stuff that matters, but the date on which she uses it.

As certain parts of the service vary little from week to week, but still need to be kept fresh, such as the short prayer for blessing the offering, she has a selection which I have stored in a stylesheet (.sty) from which I pick a different one for each Sunday of the month. I call the \newcommands names like \offerone, \offertwo, etc.

I would like to automate this, so that (if possible) LaTeX or a package should calculate which Sunday of the month it is, and output the appropriate prayer. For instance, if it were the third Sunday of the month, I would want it to pick \offerthree.

What I would like to be happen is this: I input the date of the Sunday (e.g. 4 August 2019), and assign that to some variable with which I can work. I would then divide the day_of_the_month part (i.e. 4) by 7 (which would give 0), and add 1 to the result. That would enable me to select the \offerone for the first Sunday of August.

The parts that I do not know how to do are:
(a) how to get LaTeX to pick up on what the Sunday date is if I tell it, for instance, \date{2019-08-04}. How can I fetch the 04 into a variable, please?
(b) I would then need to divide that variable by 7, and add 1, to calculate which Sunday of the month it was.
I think I know how to do the logic for selecting the correct prayer.

Maybe what I am asking is not possible using LaTeX alone. However, any advice that anyone can give me would be appreciated, as my lack of knowledge as to how to grab the day-number part of the date is hampering me.
Thanks for reading this far. If you can even help me to express my question better, I would appreciate that too.

User avatar
Ijon Tichy
Posts: 296
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Tue Jul 23, 2019 8:06 am

Some easy arithmetics:
  1. \documentclass{article}
  2. \usepackage{scrbase}% For \XdivY
  3. \usepackage{scrdate}% only for \ISODayName
  5. \makeatletter
  6. \newcommand*{\dayofisodate}[1]{%
  7. \expandafter\@dayofisodate\expanded{#1}\@nil
  8. }
  9. \newcommand*{\@dayofisodate}{}
  10. \def\@dayofisodate#1-#2-#3\@nil{#3}
  11. \newcommand*{\thisdate}{\@date}
  12. \renewcommand*{\today}{\the\year-\the\month-\the\day}
  13. \makeatother
  14. \newcommand*{\samedayofweek}[1]{%
  15. \numexpr \XdivY{\numexpr\number\dayofisodate{#1}-1\relax}{7} + 1\relax
  16. }
  17. \newcommand*{\isodayname}[1]{%
  18. \expandafter\ISODayName\expandafter{\expanded{#1}}%
  19. }
  20. \begin{document}
  21. \thisdate{}
  22. is the number \the\samedayofweek{\thisdate}
  23. \isodayname{\thisdate} of the month.
  25. \date{2019-08-04}
  26. \thisdate{}
  27. is the number \the\samedayofweek{\thisdate}
  28. \isodayname{\thisdate} of the month.
  30. Or for all day of august 2019:
  32. \makeatletter
  33. \day=1
  34. \@whilenum \day<31 \do {%
  35. \date{2019-08-\the\day}%
  36. \thisdate{}
  37. is the number \the\samedayofweek{\thisdate}
  38. \isodayname{\thisdate} of the month.\par
  39. \advance\day by \@ne
  40. }
  41. \makeatother
  42. \end{document}

Note, you will get a strange TeX error message, if the argument of \date is not an iso-date.

Note, \samedayofweek can be used like a TeX counter (which is not the same like a LaTeX counter). So you have to prefix it with \the to use it's arabic representation, as I've done to print the result. If you want to test, whether or not a date is a Sunday, you can use scrdate's \ISODayNumber, which expects an already expanded argument. So you cannot use \ISODayNumber{\thisdate} but have to use \expandafter\ISODayNumber\expandafter{\expanded{\thisdate}}.
Last edited by Ijon Tichy on Tue Jul 23, 2019 12:31 pm, edited 1 time in total.

Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm

Postby Pangur » Tue Jul 23, 2019 10:33 am

This is wonderful. I shall test it out later on today, and report back. I can see the suggestions opening up so many options for me. Thanks meantime.

Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm

Postby Pangur » Tue Jul 23, 2019 6:13 pm

Thanks again for the reply, which I found very useful, although it has taken me all day to learn quite what is going on in the code. I have now gotten to the stage that I can put in a date, and it will display it on the front cover, and state which corresponding day of the month it is. I am using \datetime2 package.

\newcommand*{\pubdate}{2019-08-25} % Specify the publication date.

\DTMdate{\pubdate} % Show publication date as Sunday 25th August 2019.

\renewcommand*{\thisdate}{\pubdate} % Make \thisdate same as publication date.
is the
number \the\samedayofweek{\thisdate} \isodayname{\thisdate} % number 4 Sunday
of the month.

As I do booklets for every second Wednesday morning, the above code works for that too :) Thanks for that bonus.

I really appreciate this help. The answer I accept, because it dealt with the questions that I was raising.

Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 8 guests