1

My MWE is based on the excellent template. The original template defines a \vacations command to store the dates of the vacation and the text to be printed; it defines some styles for each of these; and then loops over the calendar dates to apply these styles and print the text. I have tried to adapt the template, but the styles leak across nodes.

Maybe something wrong with my use of .append style?

MWE Expected output: Holidays have gray background and orange text color, other days have white background and black text color.

\documentclass[margin=1pt]{standalone}
\usepackage{tikz}
\usepackage{xparse}
\usetikzlibrary{calc,calendar}

% set current year \def\currentyear{\the\year}

% set vacation dates \newcommand*\vacationdata{% \currentyear-09-02/\currentyear-09-02/Holiday% }

% set all styles \tikzset{% every day/.style={ minimum height=0.8cm, text width=5cm, draw=gray }, every day name/.style={ font=\footnotesize }, every vacation/.style={ every day/.append style={ fill=gray!30, text=orange, } }, vacation name/.initial=, }% end of tikzset

\begin{document} \centering \begin{tikzpicture} \calendar[ dates=\currentyear-09-01 to \currentyear-09-03, name=cal, day code={% % print the calendar day number \node [name=\pgfcalendarsuggestedname, every day/.try] {\tikzdaytext}; % print the calendar day name \node [anchor=west] at ([xshift=0.5cm]\pgfcalendarsuggestedname.west) {\pgfcalendarweekdayshortname{\pgfcalendarcurrentweekday}}; % print the vacation name \pgfkeysifdefined{/tikz/vacation name}{% \node [every vacation/.try,text width=3cm,xshift=1cm] {\pgfkeysvalueof{/tikz/vacation name}}; }{}% }, execute before day scope={ % set vacation name \tikzset{every vacation/.try, vacation name=, loop over item/.code args={##1/##2/##3}{% \ifdate{between=##1 and ##2}{% \tikzset{every vacation/.try, vacation name/.expanded=##3}% }{}}, loop over item/.list/.expanded=\vacationdata}% }, execute at begin day scope={% \pgftransformyshift{-1*\pgfcalendarcurrentday cm} } ]; \end{tikzpicture} \end{document}

enter image description here

PatrickT
  • 2,923

1 Answers1

3

The main change is to test whether key /tikz/vacation name has an empty value, since it is always defined.

Not sure if the area having background color is the desired.

\documentclass[margin=1pt]{standalone}
\usepackage{tikz}
\usepackage{xparse}
\usetikzlibrary{calc,calendar}

% set current year \def\currentyear{\the\year}

% set vacation dates \newcommand*\vacationdata{% \currentyear-09-02/\currentyear-09-02/Holiday% }

% set all styles \tikzset{% every day/.style={ minimum height=0.8cm, text width=5cm, draw=gray }, every day name/.style={ font=\footnotesize }, every vacation/.style={ every day/.append style={ fill=gray!30, text=orange, } }, vacation name/.initial=, }% end of tikzset

\begin{document} \centering \begin{tikzpicture} \calendar[ dates=\currentyear-09-01 to \currentyear-09-03, name=cal, day code={% % print the calendar day number \node [name=\pgfcalendarsuggestedname, every day/.try] {\tikzdaytext}; % print the calendar day name \node [anchor=west] at ([xshift=0.5cm]\pgfcalendarsuggestedname.west) {\pgfcalendarweekdayshortname{\pgfcalendarcurrentweekday}}; % print the vacation name \pgfkeysgetvalue{/tikz/vacation name}{\mytemp}% \ifx\mytemp\empty \else \node [every vacation/.try, every day/.try, text width=3cm,xshift=1cm] {\pgfkeysvalueof{/tikz/vacation name}}; \fi }, execute before day scope={ % set vacation name \tikzset{vacation name=, loop over item/.code args={##1/##2/##3}{% \ifdate{between=##1 and ##2}{% \tikzset{vacation name/.expanded=##3}% }{}}, loop over item/.list/.expanded=\vacationdata}% }, execute at begin day scope={% \pgftransformyshift{-1*\pgfcalendarcurrentday cm}% } ]; \end{tikzpicture} \end{document}

enter image description here

muzimuzhi Z
  • 26,474
  • Oh, very clever, I would never have found this one! Thanks! – PatrickT Sep 08 '21 at 23:49
  • 1
    Btw, noted the baselines of "Thu" and "Holiday" are slightly mis-aligned because only y in Holiday has a descender. You can play with tikz options text height and text depth, see pgfmanual sec. 5.1 "Styling the Nodes". – muzimuzhi Z Sep 09 '21 at 00:14
  • +1: Comment on the last comment: Maybe \strut would help – Dr. Manuel Kuehner Sep 09 '21 at 01:17
  • I've been able to adapt the code to a bigger project! With 6 months on a single page, the misalignment you noted is not perceptible. Here is the full code I'm using for reference (don't hesitate to comment further). Thanks again!! https://pastebin.com/wBDcnzZ8 – PatrickT Sep 09 '21 at 03:53
  • 1
    For better text alignment, try the mid anchors (eg mid west) – Andrew Stacey Sep 09 '21 at 05:50