如何测试传递的日期是否在日历上

如何测试传递的日期是否在日历上

我想知道如何测试传递的事件日期是否在\calbegindate和之间\calenddate。这两个值是根据我的代码中的某些选项动态设置的,因此很容易发生这种情况:其中一个尚未更新的事件的日期仍然在日历之外。这将产生奇怪的输出以及错误消息。

因此,在尝试将事件打印到日历之前,最好先测试一下该事件是否有日历上的日期,这当然会失败。我添加了一个注释掉的事件,该事件不在日历日期中,取消注释并在上面的事件中添加一个逗号以测试输出。


“最小”示例

(抱歉,但我不知道如何进一步简化代码,因此它并不是真正最小的)

% %%%%%%%%
% Preamble
% %%%%%%%%

\documentclass[10pt, a4paper, landscape]{article}
\usepackage[margin = .5cm, nofoot]{geometry}

% Tikz
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{calendar}

% Logic and Tools
\usepackage{xparse}

% Fonts
\RequirePackage{lmodern}
\renewcommand*\familydefault{\sfdefault}

% Color
\definecolor{definedcolor}{HTML}{00CC00}

% Calculation
\ExplSyntaxOn
    \DeclareExpandableDocumentCommand{\eval}{m}{\int_eval:n {#1}}
\ExplSyntaxOff

% Custom appearance
\newcommand{\practicalCourse}[2]{%
    \node [text=black, anchor = north west, text width = 3.3cm ] at ($(cal-#1.north west)+(3.5em, -0.2em)$) {\scriptsize{#2}};
}

% Variables
\newcommand{\currentyear}{\the\year}
\newcommand{\nextyear}{\eval{\currentyear + 1}}

\newcommand{\calbegindate}{\currentyear-10-01}
\newcommand{\calenddate}{\nextyear-03-31}

\newcommand{\practicalCourses}{%
    {Foo}/\currentyear-10-10,%
    {Foo}/\currentyear-10-11,%
    {Foo}/\currentyear-10-12,%
    {Foo}/\currentyear-10-17,%
    {Foo}/\currentyear-10-18,%
    {Foo}/\currentyear-10-24,%
    {Foo}/\currentyear-10-25,%
    {Foo}/\currentyear-10-26,%
    {Foo}/\currentyear-11-07,%
    {Foo}/\currentyear-11-08%
    % {Bar}/\currentyear-04-11%
}

% Define searchable object (\ifdate{PracticalCourse})
% Answer by cfr on StackExchange: https://tex.stackexchange.com/a/346318/117727
\ExplSyntaxOn
    \clist_new:N \g_practical_course_clist%
    \int_new:N \l_practical_course_int%
    \foreach \i/\j in \practicalCourses {%
        \pgfcalendardatetojulian{\j}{\l_practical_course_int}%
        \clist_gput_right:Nx \g_practical_course_clist {%
            \int_to_arabic:n { \l_practical_course_int }%
        }%
    }%
    \cs_new_protected_nopar:Nn \practical_course_test:n {%
        \int_set:Nn \l_tmpa_int {#1}%
            \clist_if_in:NVT \g_practical_course_clist \l_tmpa_int {%
            \pgfcalendarmatchestrue%
        }%
    }%
    \cs_generate_variant:Nn \practical_course_test:n {x}%
    \NewDocumentCommand \testpraktikum { m } {%
        \practical_course_test:x { #1 }%
    }%
\ExplSyntaxOff

\tikzset{
    /pgf/calendar/PracticalCourse/.code={%
        \testpraktikum{\pgfcalendarifdatejulian}%
    },
}


% %%%%%%%%%%%%%%
% Begin Document
% %%%%%%%%%%%%%%

\begin{document}
    \centering
    \begin{tikzpicture}[every day/.style={anchor = north}]
        \calendar[
            dates = \calbegindate to \calenddate,
            name = cal,
            day yshift = 3em,
            day code = {
                \node[name = \pgfcalendarsuggestedname, every day, minimum height = .53cm, text width = 4.4cm, draw = gray] {\tikzdaytext};
                \draw (-1.8cm, -.1ex) node [anchor = west, font=\footnotesize] {\pgfcalendarweekdayshortname{\pgfcalendarcurrentweekday}};
            },
            execute before day scope={
                \ifdate{day of month = 1} {
                    \pgftransformxshift{4.8cm}
                    \draw (0,0) node [minimum height = .53cm, text width = 4.4cm, fill = definedcolor, text = white, draw = definedcolor, text centered] {\textbf{\pgfcalendarmonthname{\pgfcalendarcurrentmonth}\strut}};
                }{}
                \ifdate{workday} {
                    \tikzset{every day/.style = {fill = white}}
                    \ifdate{PracticalCourse}{%
                        \tikzset{every day/.style = {fill = olive!30}}%
                    }{}
                }{}
                \ifdate{Saturday} {
                    \tikzset{every day/.style = {fill = definedcolor!10}}%
                }{}
                \ifdate{Sunday} {
                    \tikzset{every day/.style = {fill = definedcolor!20}}%
                }{}
            },
            execute at begin day scope = {
                \pgftransformyshift{-.53*\pgfcalendarcurrentday cm}
            }
        ];
        \foreach \subject/\eventdate in \practicalCourses {
            \practicalCourse{\eventdate}{\subject}
        }
    \end{tikzpicture}
\end{document}

答案1

您可以测试该节点是否存在:

\newcommand{\practicalCourse}[2]{%
    \pgfutil@ifundefined{pgf@sh@ns@cal-#1}
     {\typeout{Warning date #1 doesn't exist}}% or some other action 
     {\node [text=black, anchor = north west, text width = 3.3cm ] at ($(cal-#1.north west)+(3.5em, -0.2em)$) {\scriptsize{#2}};}
}

相关内容