1

I have defined an array of ISO-dates. Next, I need to convert the dates to Julian days.

As the following code shows, this is possible using the \foreach statement + \pgfcalendardatetojulian (method 1). However, I would love to get it by invoking the first and last element of the array, in order to easily compute long periods of time (method 2). Unfortunately, it does not work.

How can I archive this?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calendar}
        
\pgfkeys{/pgf/calendar/method one/.code={
    \newcount\n
    \foreach \d in \holiday{
        \pgfcalendardatetojulian{\d}{\n}
        \ifnum \pgfcalendarcurrentjulian = \n
            \global\pgfcalendarmatchestrue
        \fi
        }}}
            
\pgfkeys{/pgf/calendar/method two/.code={
    \newcount\first \pgfcalendardatetojulian{\holiday[0]}{\first} \advance \first -1
    \newcount\last \pgfcalendardatetojulian{\holiday[1]}{\last} \advance \last +1
    \ifnum \pgfcalendarcurrentjulian > \first
        \ifnum \pgfcalendarcurrentjulian < \last
            \global\pgfcalendarmatchestrue
    \fi\fi
    }}
        
\begin{document}
        
\def\holiday{2021-1-1, 2021-1-10}
        
\begin{tikzpicture}
\calendar[dates=2021-01-01 to 2021-01-last, style = week list]
%   if (method one) [red];    % this works
    if (method two) [red];    % this doesn't
\end{tikzpicture}
        
\end{document}
2
  • Both methods work for me !!! Sep 28, 2021 at 18:38
  • @HafidBoukhoulda, it looks like it works, but it should compute every day between 2021-1-1 and 2021-1-10, not only the first day of that period.
    – e_moro
    Sep 28, 2021 at 20:28

2 Answers 2

0

I don't know if this ok

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calendar}
        
\pgfkeys{/pgf/calendar/method one/.code={
    \newcount\n
    \foreach \d in \holiday{
        \pgfcalendardatetojulian{\d}{\n}
        \ifnum \pgfcalendarcurrentjulian = \n
            \global\pgfcalendarmatchestrue
        \fi
        }}}
            
\pgfkeys{/pgf/calendar/method two/.code={
    \newcount\first \pgfcalendardatetojulian{\holiday[0]}{\first} \advance \first -1
    \newcount\last \pgfcalendardatetojulian{\holiday[1]}{\last} \advance \last +1
    \ifnum \pgfcalendarcurrentjulian > \first
        \ifnum \pgfcalendarcurrentjulian < \last
            \global\pgfcalendarmatchestrue
    \fi\fi
    }}
        
\begin{document}
        
\def\holiday{2021-1-1, 2021-1-10}
        
\begin{tikzpicture}
\calendar[dates=2021-01-01 to 2021-01-last, style = week list]
%   if (method one) [red];    % this works
%    if (method two) [red];    % this doesn't
if (between =2021-1-1 and 2021-1-10) [red];
\end{tikzpicture}
        
\end{document}

enter image description here

0

There's no need for much .code in my opinion.

You can just define a style that takes the first and the second date of your list and forwards it to the /pgf/calendar/between key that is basically doing the same thing as your method two.

Here I defined a /pgf/calendar/parse between key that you give an once expanded \holiday.

You can also, similar to your own keys, define a holiday key that does just that:

/pgf/calendar/holiday/.style={parse between/.expand once=\holiday}

Then you only have to say if (holiday) […].

And if you want all holidays, to be red, either way, you can define a /tikz/holiday key as in the last example.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{calendar}
\pgfqkeys{/pgf/calendar}{
  parse between/.style args={#1,#2}{between=#1and#2}}
\newcommand*\holiday{2021-1-1, 2021-1-10}

\begin{document}
\begin{tikzpicture}
\calendar[dates=2021-01-01 to 2021-01-last, week list]
    if (parse between/.expand once=\holiday) [red];
\end{tikzpicture}

\begin{tikzpicture}[
  /pgf/calendar/holiday/.style={parse between/.expand once=\holiday}]
\calendar[dates=2021-01-01 to 2021-01-last, week list]
    if (holiday) [red];
\end{tikzpicture}

\begin{tikzpicture}[
  holiday/.style={if={(parse between/.expand once=\holiday)[red]}}]
\calendar[dates=2021-01-01 to 2021-01-last, week list]
    [holiday];
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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