With the ext.calendar-plus
library of my tikz-ext
package we can hack something more automatic together:
This approach uses the following conditions that were first implemented in another answer of mine but are now part of the library mentioned.
week of month = <value>
:
Returns true if the current date is in the <value>
th week of the month.
For week of month = 1
this is basically equal to between = <month>-01 and <month>-07
but doesn't do as much calculations.
week of month' = <value>
:
Returns true if the current date is in the <value>
th last week of the month.
and = <list of conditions>
:
Returns true if all conditions in the list evaluate to true
It also uses two new conditions that were created for this answer but are also now part of the library:
calendar week of month = <value>
Returns true if the current date is in the <value>
th calendar week of the month.
calendar week of month' = <value>
Returns true if the current date is in the <value>
th last calendar week of the month.
They both use the current day and the current weekday to calculate the number of calendar week and compare it with the given value.
The calendar week of month'
condition uses the calendar week of month
key but reverses the numbers before it's called.
If we now and
the two conditions
week of month = 1
(i.e. first seven days of the week) and
calendar week of month = 2
(i.e. the seven days in the second calendar week)
we found those days and week days where we'll want to have another day of the previous week added to our calendar.
The same applies for the end of the week with
week of month' = 1
(i.e. the last seven days of the week) and
calendar week of month' = 2
.
If these conditions apply we directly execute style add day from week before
or add day from week after
. (With “directly” I mean that they're executed even before the actual current day is called – it might be more logical to add this code to execute at end day scope
.)
Since this is executed as part of the if
s it might inherit settings that originally apply to the actual current day. However, if we add our if
s as early as possible, we might be in the clear.
The add day from week before
and add day from week after
simulate time travel to the week before/after the currently processed day.
The \pgftransformyshift
is obviously dependent of the actual style of the calendar, it might be better to put this into the add day from week …
style so that this code can be better reused for other calendar styles.
Code
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{ext.calendar-plus}
\makeatletter
\tikzset{
add day from week before/.default=, add day from week after/.default=,
add day from week before/.code={%
\begingroup % shift whole calendar 7 days in the past
\advance\pgfcalendarcurrentjulian by -7
\pgfcalendarjuliantodate{\pgfcalendarcurrentjulian}{\pgfcalendarcurrentyear}{\pgfcalendarcurrentmonth}{\pgfcalendarcurrentday}%
\pgfcalendarjuliantoweekday{\pgfcalendarcurrentjulian}{\pgfutil@tempcntb}%
\edef\pgfcalendarcurrentweekday{\the\pgfutil@tempcntb}%
\pgfcalendarjulianyeartoweek{\pgfcalendarcurrentjulian}{\pgfcalendarcurrentyear}{\pgfutil@tempcntb}%
\edef\pgfcalendarcurrentweek{\ifnum\pgfutil@tempcntb<10 0\fi\the\pgfutil@tempcntb}%
\pgftransformyshift{\pgfkeysvalueof{/tikz/day yshift}}%
\tikzset{every day from week before/.try,#1}%
\tikzdaycode
\endgroup},
add day from week after/.code={%
\begingroup % shift whole calendar 7 days in the future
\advance\pgfcalendarcurrentjulian by +7
\pgfcalendarjuliantodate{\pgfcalendarcurrentjulian}{\pgfcalendarcurrentyear}{\pgfcalendarcurrentmonth}{\pgfcalendarcurrentday}%
\pgfcalendarjuliantoweekday{\pgfcalendarcurrentjulian}{\pgfutil@tempcntb}%
\edef\pgfcalendarcurrentweekday{\the\pgfutil@tempcntb}%
\pgfcalendarjulianyeartoweek{\pgfcalendarcurrentjulian}{\pgfcalendarcurrentyear}{\pgfutil@tempcntb}%
\edef\pgfcalendarcurrentweek{\ifnum\pgfutil@tempcntb<10 0\fi\the\pgfutil@tempcntb}%
\pgftransformyshift{-(\pgfkeysvalueof{/tikz/day yshift})}%
\tikzset{every day from week after/.try,#1}%
\tikzdaycode
\endgroup}}
\makeatother
\begin{document}
\sffamily
\tikz\calendar[
dates=2022-01-01 to 2022-03-31, week list, month label above centered,
% as early as possible
if = {(and = {week of month = 1, calendar week of month = 2}) [add day from week before]},
if = {(and = {week of month'= 1, calendar week of month'= 2}) [add day from week after]},
every day from week before/.style=gray,
every day from week after/.style={gray,if=(Sunday)[red!25!gray]},
] if (Sunday) [red];
\end{document}
Output
