TransWikia.com

Tikz-Calendar: How to work with an array of dates?

TeX - LaTeX Asked by S.V. on July 16, 2021

I want do define a set with dates chemistryclasses={2021-05-25,2021-06-01,2021-06-08,2021-06-15,2021-06-22,2021-06-29,2021-07-06,2021-07-13}
and then I want the code to paint the corresponding dates in tikz-calendar.
In the MWE below I could do that. However, I would like to automatize the routine. It is tedious to write all those if (equals=2021-05-25). At the moment I use Python to write the .tex file. It reads a CSV file with the dates and the generate the .tex file. But I was thinking about an all-LaTeX solution.
I tried something like

foreach x in {2021-05-25,2021-06-01,2021-06-08,2021-06-15,2021-06-22,2021-06-29,2021-07-06,2021-07-13} {%
  if (equals=x) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
}

but it didn’t work. So how can I automatize these things inside LaTeX? A good workflow would be:

1)Write the dates in a file

2)LaTeX reads the dates and save to a variable

3)LaTeX generates the calendar.

MWE:

documentclass[12pt]{article}
usepackage{hyperref}
hypersetup{bookmarks=true,colorlinks=true,citecolor=blue}
usepackage{tikz}
usetikzlibrary{positioning,calc,calendar,shapes.geometric}


% =============
% = TColorBox =
% =============
usepackage[most]{tcolorbox}
tcbuselibrary{breakable}
tcbset{left=4mm,sharpish corners,colback=white}


% ============
% = Calendar =
% ============
newcommand{calrow}[1]{
node[anchor=base,xshift=0.5ex](mon){M}; % Shift first weekday
node[base right=0.1em of mon](tue){T}; 
node[base right=0.0em of tue](wed){W};
node[base right=0.0em of wed](thu){T}; 
node[base right=0.1em of thu](fri){F};
node[base right=0.2em of fri](sat){S}; 
node[base right=0.2em of sat](sun){S};
node[black,above=0.1em of thu]{textbf{#1}};}
newcommand{calperiod}[1]{%
    calendar (month-#1)[dates=2021-#1-01 to 2021-#1-last,
    every day/.style={anchor=base}, % Center days
    day text={%d=},rounded corners=0,
    anchor=base,text height=1ex,text depth=-0.5ex] % Make node placement easier
    if(Sunday) [red] 
    teachingdays;
}
newcommand{teachingdays}{
if (equals=2021-05-11) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-05-18) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-05-25) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-06-01) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-06-08) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-06-15) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-06-22) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-06-29) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-07-06) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-07-13) {node [fill=yellow,draw,regular polygon,regular polygon sides=4] {};}
if (equals=2021-05-11) [blue]
if (equals=2021-05-18) [blue]
if (equals=2021-05-25) [blue]
if (equals=2021-06-01) [blue]
if (equals=2021-06-08) [blue]
if (equals=2021-06-15) [blue]
if (equals=2021-06-22) [blue]
if (equals=2021-06-29) [blue]
if (equals=2021-07-06) [blue]
if (equals=2021-07-13) [blue]
}
newcommand{generatehyperlinks}{
node [hyperlink node=data01,draw=none,regular polygon,regular polygon sides=4] at (month-05-2021-05-11) {};
node [hyperlink node=data02,draw=none,regular polygon,regular polygon sides=4] at (month-05-2021-05-18) {};
node [hyperlink node=data03,draw=none,regular polygon,regular polygon sides=4] at (month-05-2021-05-25) {};
node [hyperlink node=data04,draw=none,regular polygon,regular polygon sides=4] at (month-06-2021-06-01) {};
node [hyperlink node=data05,draw=none,regular polygon,regular polygon sides=4] at (month-06-2021-06-08) {};
node [hyperlink node=data06,draw=none,regular polygon,regular polygon sides=4] at (month-06-2021-06-15) {};
node [hyperlink node=data07,draw=none,regular polygon,regular polygon sides=4] at (month-06-2021-06-22) {};
node [hyperlink node=data08,draw=none,regular polygon,regular polygon sides=4] at (month-06-2021-06-29) {};
node [hyperlink node=data09,draw=none,regular polygon,regular polygon sides=4] at (month-07-2021-07-06) {};
node [hyperlink node=data10,draw=none,regular polygon,regular polygon sides=4] at (month-07-2021-07-13) {};
}
tikzset{
    hyperlink node/.style={
        alias=sourcenode,
        append after command={
            let     p1 = (sourcenode.north west),
                p2=(sourcenode.south east),
                n1={x2-x1},
                n2={y1-y2} in
            node [inner sep=0pt, outer sep=0pt,anchor=north west,at=(p1)] {hyperlink{#1}{XeTeXLinkBox{phantom{rule{n1}{n2}}}}}
                    %xelatex needs XeTeXLinkBox, won't create a link unless it
                    %finds text --- rules don't work without XeTeXLinkBox.
                    %Still builds correctly with pdflatex and lualatex
        }
    }
}
linespread{1.3}


begin{document}

% ============
% = Calendar =
% ============


begin{tikzpicture}[every calendar/.style={week list}]
    matrix[%
            row 1/.style={black,node distance=.3ex},%
            row 3/.style={black,node distance=.3ex},
            column sep=1ex,draw=none]{%
            % first row: week day and month
            calrow{May} & calrow{June} & calrow{July} 
            % second row: calendar
            calperiod{05} & calperiod{06} & calperiod{07} 
            };
            generatehyperlinks
end{tikzpicture}


newpage
begin{itemize}
    item hypertarget{data01}{01}
    item hypertarget{data02}{02}
    item hypertarget{data03}{03}
    item hypertarget{data04}{04}
    item hypertarget{data05}{05}
    item hypertarget{data06}{06}
    item hypertarget{data07}{07}
    item hypertarget{data08}{08}
    item hypertarget{data09}{09}
    item hypertarget{data10}{10}
end{itemize}
end{document}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP