8

I want to create a simple week-based schedule in some automatic fashion. Like a cleaning schedule among roommates, a winter-service schedule among neighbors etc.

Basically I want to transfer a simple case distinction rule like

x % n == 0   -> party_1 in charge
x % n == 1   -> party_2 in charge
...
x % n == n-1 -> party_n in charge

(where x is the current week-number)

into a DIN-A4 calendar like schedule. Where e.g. each month is a block, each weak is a line in that block which is annotated with the assigned party and week-number (and possibly colorized).

The calendar library of tikz looks promising, but I can't find a command to access the weeknumber (for use in if/ifdate clauses).

Sure, I can always autogenerate 52 if-clauses for a tikz-calendar command, but perhaps there is a more elegant way?

1 Answer 1

5

I did a little bit of rtfm and I've come up with some macros for that task.

First, we need to compute the 'day of the year' for a given date, i.e. the number of days that have passed since the beginning of a year with respect to a given date:

\newcount\begincnt
\def\initbegin{%
  \pgfcalendardatetojulian{2014-01-01}{\begincnt}
}
\def\doy#1#2{%
  \initbegin#1=#2\advance#1by-\begincnt\relax%
}

The initbegin macro initializes the begincnt counter to the julian day number (JDN) of the 1st of January we want to use in our calendar.

doy gets the JDN of a 2014 date as input and sets the counter given as first argument to the day-of-the-year result.

With that we can define a weak of the year (woy) macro:

\def\woy#1#2{%
  \doy{#1}{#2}\yearoff{\cnte}{#2}\advance#1by\cnte\divide#1by7\advance#1by1\relax%
}

Again, a result counter is the first argument and 2nd argument is a counter that contains the JDN of a date.

The yearoff macro returns 2 for a 2014 date - it is needed as 'correction' for years where the first of January is not a Monday. Its code:

\newcount\cntd
\newcount\cnte
\def\myyear{}
\def\mydummy{}

\def\yearoff#1#2{%
  \pgfcalendarjuliantodate{#2}{\myyear}{\mydummy}{\mydummy}%
  \pgfcalendardatetojulian{\myyear-01-01}{\cntd}%
  \pgfcalendarjuliantoweekday{\cntd}{#1}\relax%
}

With that we are (nearly) ready to define a custom pgf-key for ifdate-clauses in PGF- and TikZ-Calendar constructs:

\newcount\cnta
\newcount\cntb
\newcount\cntp

\pgfkeys{/pgf/calendar/modb/.cd,.value required,.code={
  \woy{\cnta}{\pgfcalendarifdatejulian}\mod{\cntb}{\cnta}{\cntp}
    \ifnum\cntb=#1\relax%
      \pgfcalendarmatchestrue%
    \fi%
}}

This filter uses another custom macro mod to compute the modulo (\cntp contains the number of parties):

\def\mod#1#2#3{%
  #1=#2\relax%
  \divide#1by#3\relax%
  \multiply#1by-#3\relax%
  \advance#1by#2\relax%
}

Examples

We can use the filter like this:

On 2014-01-06 the 2nd party is \pgfcalendarifdate{2014-01-06}{modb=1}{not} in charge.

Or in a TikZ-calendar command:

\tikz
  \calendar[dates=2014-01-01 to 2014-03-last, week list,month label above centered]
    if (modb=2) [nodes={fill=blue!60}]
  ;

(which colorizes all days where the third party is in charge)

Or as part of a calendar style:

\begin{tikzpicture}[every calendar/.style={
  if={(modb=0) [nodes={fill=yellow!60}]}
}
1
  • In the \woy definition, to have the desired week number, you do not want to advance the counter #1 by 1 at the end.
    – fyusuf-a
    Sep 20, 2015 at 18:27

You must log in to answer this question.

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