1

I want to calculate the beginning and ending date of a week (first day is Monday, last is Sunday). This is working fine for a single date. Now I want to calculate the start and end of multiple weeks consecutively in a loop, given the starting date (always a Monday) and the number of weeks to calculate. This works only for the first calculation, meaning that the date of the next Sunday is correct, but after that the date is always reset after each loop.

Resulting Document

My algorithm to calculate the next date looks like this:

# cursor is at a Monday
for n in 1 .. 10:
    print(cursor) # Monday
    add 6 days to cursor
    print(cursor) # Sunday
    add 1 day to cursor
end for

Minimum Working Example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[calc]{datetime2}
\usepackage{pgffor}
\setlength{\parindent}{0pt}

\begin{document}

\newcommand{\nweeks}{4}
\newcommand{\startdate}{2020-09-07}

\newcount\datecount
\newcommand{\dateoffset}[2]{%
    \DTMsaveddateoffsettojulianday{#1}{#2}{\datecount}%
    \DTMsavejulianday{#1}{\datecount}%
}

\DTMsavedate{currentdate}{\startdate}

Start-Week-1: \DTMusedate{currentdate}\\
\dateoffset{currentdate}{6}
End-Week-1: \DTMusedate{currentdate}\\
\dateoffset{currentdate}{1}
Start-Week-2: \DTMusedate{currentdate}\\
\dateoffset{currentdate}{6}
End-Week-2: \DTMusedate{currentdate}\\
\dateoffset{currentdate}{1}

\DTMsavedate{currentdate}{\startdate}

\foreach \n in {1,...,\nweeks}{
    Start-Week-\n: \DTMusedate{currentdate}\\
    \dateoffset{currentdate}{6}
    End-Week-\n: \DTMusedate{currentdate}\\
    \dateoffset{currentdate}{1}
}

\end{document}

1 Answer 1

2

I'm afraid you've fallen into the typical trap of pgffor's \foreach macro: it evaluates the loop body inside a TeX group, therefore local assignements performed therein are lost at the end of each iteration. My suggestion would be to use expl3's \int_step_inline:nnn macro or one of its sisters. Another option would be to perform a global assignment to the \datecount count register at the end of the loop; however, mixing local and global assignments to the same macro or register should be avoided in general (cf. chapter 27 of the TeXbook regarding “save stack buildup”). Here is a solution using expl3's \int_step_inline:nnn macro:

\documentclass{article}
\usepackage{expl3}
\usepackage[calc]{datetime2}

\ExplSyntaxOn
% Borrow \int_step_inline:nnn from expl3
\cs_new_eq:NN \intstepinline \int_step_inline:nnn
\ExplSyntaxOff

\newcount\datecount
\newcommand*{\dateoffset}[2]{%
    \DTMsaveddateoffsettojulianday{#1}{#2}{\datecount}%
    \DTMsavejulianday{#1}{\datecount}%
}

\newcommand{\nweeks}{4}
\newcommand{\startdate}{2020-09-07}
\DTMsavedate{currentdate}{\startdate}

\setlength{\parindent}{0pt}

\begin{document}

\intstepinline{1}{\nweeks}{%
  Start-Week-#1: \DTMusedate{currentdate}%
  \dateoffset{currentdate}{6}\\
  End-Week-#1: \DTMusedate{currentdate}%
  \dateoffset{currentdate}{1}%
  \par
}

\end{document}

enter image description here

6
  • 1
    Welcome again in TeX.SE. All the best.
    – Sebastiano
    Sep 2, 2020 at 14:26
  • 1
    Hi @Sebastiano! I was just passing by but am glad to see you! :-) Take care.
    – frougon
    Sep 2, 2020 at 14:46
  • Sorry, I can't get your example to work. I'm using pdfLaTeX on Overleaf and I'm getting the following error: You can't use macro parameter character # in horizontal mode. Sep 2, 2020 at 18:59
  • 1
    @ShadowEagle Sorry, but I believe you did not use my code as is, or your expl3 package is very ancient. I don't see any # used in horizontal mode. Maybe you didn't copy the definition of \intstepinline? The first error would definitely be more informative than the last one.
    – frougon
    Sep 2, 2020 at 19:42
  • 1
    Okay, thanks for the follow-up. I'm glad to hear you've got it working.
    – frougon
    Sep 2, 2020 at 21:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .