0

I want to construct a counter with two arguments - year and month, say \tt{year}{month}. The usage like blah-blah \tt{2022}{6}, blah-blah \tt{2022}{6} gives blah-blah 06/06, blah-blah 13/06. If the month has finished it starts the next one. The package datetime gives the opposite possibility: \dayofweekname{1}{6}{2022} gives the name of the date of the week.

3
  • 2
    you show \tt{2022}{6}, being used twice and returning 06/06 then 13/06, can you clarify what this command is intended to do and what Mondays is it counting? I'm guessing that you want it to return the date of the first Monday of the specified month in dd/mm format then count how many times the command has been used (with the same month argument?) and incrementing the week. What would ``\tt{2022}{6},\tt{2022}{7},\tt{2022}{6}` produce? First Monday in June, First Monday in July?, Second Monday in June? Commented Jun 3, 2022 at 22:37
  • @DavidCarlisle \tt{year}{month} is counter with increment one week. If it is placed first-time \tt{2022}{6} it gives the date of the first Monday of June - 06/06. The second time \tt{2022}{6} gives the Monday in the next week - 13/06 and so on. The \tt{2022}{6},\tt{2022}{7},\tt{2022}{6} will produce 06/06, 04/07, 13/06. In fact, I need for the counter which starts from a specific month, say June, and counts all Mondays next months - July, August, and so on.
    – nail
    Commented Jun 3, 2022 at 23:01
  • 1
    calling it a counter is confusing (as counter means something completely different in latex) and the proposed syntax, giving the start date each time seems a bit weird and means you would need to track an arbitrary number of separate threads. Simpler would be \setMonday{2022}{06} which sets the internal date to first monday of the specified monh, then \getmonday ... \getmonday to return successive dates Commented Jun 3, 2022 at 23:12

2 Answers 2

3

The solution uses a syntax more like what David Carlisle suggested. We use the pgfcalendar package to do all the computations.

\documentclass{article}
\usepackage{pgfkeys,pgfcalendar}
\newcount\currentjday
\newcount\daycalcoffset
\newcommand\findFirstMonday[2]{%
    \pgfcalendardatetojulian{#1-#2-1}{\currentjday}%
    \pgfcalendarjuliantoweekday{\currentjday}{\daycalcoffset}%
    \advance\currentjday by -\daycalcoffset\relax %find prev Monday
    \getNextMonday 
}
\newcommand\getNextMonday{%
    \advance\currentjday by 7\relax
    \pgfcalendarjuliantodate{\currentjday}{\theyear}{\themonth}{\theday}%
    \theyear-\themonth-\theday
}
\begin{document}
\findFirstMonday{2022}{6}

\getNextMonday

\getNextMonday
\end{document}
4
  • Nice use of pgfcalendar, but please remove the spurious spaces (try for instance X\findFirstMonday{2022}{6}\getNextMonday X). :-)
    – frougon
    Commented Jun 4, 2022 at 7:51
  • @frougon: % added. Fixed (I think). Commented Jun 4, 2022 at 15:17
  • Yes, this looks good, thanks!
    – frougon
    Commented Jun 4, 2022 at 16:28
  • @WillieWong Really nice and what I wanted.
    – nail
    Commented Jun 4, 2022 at 16:37
0

A little modification of the answer above is suitable for any day of the week.

\documentclass{article}
\usepackage{pgfkeys,pgfcalendar}
\newcount\firstjday
\newcount\fistgday
\newcommand\fWd[3]{%
\pgfcalendardatetojulian{#1-#2-1}{\firstjday}%
\pgfcalendarjuliantoweekday{\firstjday}{\fistgday}%
\advance\firstjday by -\fistgday \relax %find prev Monday
\advance\firstjday by #3 \relax %find prev day #3 (#3=0 is Monday)
\nWd 
}
\newcommand\nWd{%
\advance\firstjday by 7\relax
\pgfcalendarjuliantodate{\firstjday}{\theyear}{\themonth}{\theday}%
\theyear-\themonth-\theday
}
\begin{document}
\fWd{2022}{6}{0} \nWd\  \nWd\ \nWd 
\end{document}

The \fWd has a third argument which is a day of the week, 0 is Monday, 1 is Tuesday, and so on.

2
  • 1
    Please include a full MWE with a preamble and a document body with a brief example.
    – Unknown
    Commented Jun 4, 2022 at 19:20
  • @Unknown Ok. Done.
    – nail
    Commented Jun 4, 2022 at 19:49

You must log in to answer this question.

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