2

I'm modifying a calendar by Robert Krause (TeXample) and I'm searching for a method to get this calendar work dynamically. See also my original post here.

As nobody has answered on that oruginal post, I'm asking this new question, which is probably helpful for many other users and diverese uses. How can I define an associative array in Tikz and loop through it?

In my case this array should look like this:

\exams = [
    {Chemistry}{2017-05-13},
    {Maths}{2017-06-26},
    {Physics}{2017-03-11}
]

In my calendar code, where the cells are defined, I have to loop through this array to check, if there are any exams to display:

% Tikzpicture environment for the first six month of the year
\foreach{\exams as \exam => \date}{
    \if{\date <= \year-06-30}{
        \exam{\date}{\exam}
    }
}

% Tikzpicture environment for the last six month of the year
\foreach{\exams as \exam => \date}{
    \if{\date > \year-06-30}{
        \exam{\date}{\exam}
    }
}

This little makro is used to write the name into the field:

\def\exam#1#2{
    \node [
        anchor=north west, 
        text width= 3.4cm,
        text=red,
        font=\bf
    ] at
    ($(cal-#1.north west)+(3em, -0.2em)$) {\scriptsize{#2}};
}

It is called like this:

\exam{2017-06-23}{Physical Chemistry}

The loop should call this makro and insert the date and name.

Sam
  • 2,958
  • it is rather hard to debug disconnected fragments, if #1 is 2017-06-23 what is the intention of cal-2017-06-23.north west where is that node defined? – David Carlisle Dec 28 '16 at 17:29
  • @DavidCarlisle Please check the code in this gist. I would really enjoy your help :) – Sam Dec 29 '16 at 10:12

1 Answers1

4

The tikz syntax is rather different (and tex doesn't really have arrays) but you can iterate through subject/date pairs, which seems to be the main requirement here.

enter image description here

\documentclass{article}
\usepackage{tikz}
\def\year{2017}
\errorcontextlines1000
\begin{document}

\def\exams {
    Chemistry/2017-05-13,
    Maths/2017-06-26,
    Physics/2017-03-11,
    French/2017-08-01
}
\def\datenum#1{\expandafter\xdatenum#1\relax}
\def\xdatenum#1-#2-#3\relax{#1#2#3}

A:

% Tikzpicture environment for the first six month of the year
\foreach \subject/\sdate in \exams{
\ifnum\datenum\sdate<\datenum{\year-06-30}
 [exam \sdate, \subject]\par
\fi
}

B:

% Tikzpicture environment for the last six month of the year
\foreach \subject/\sdate in \exams{
\ifnum\datenum\sdate > \datenum{\year-06-30}
   [exam \sdate, \subject]\par
\fi
}

\end{document}
David Carlisle
  • 757,742