我的 MWE 基于优秀的模板。原始模板定义了一个\vacations
命令来存储假期日期和要打印的文本;它为每个日期定义了一些样式;然后循环遍历日历日期以应用这些样式并打印文本。我曾尝试调整模板,但样式会跨节点泄漏。
也许我的使用有问题.append style
?
MWE 预期输出:假期有灰色背景和橙色文本颜色,其他日子有白色背景和黑色文本颜色。
\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}
答案1
主要的变化是测试键是否/tikz/vacation name
具有空值,因为它始终被定义。
不确定具有背景颜色的区域是否是所需的。
\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}