![Tikz 日历,日期数字交替着色](https://linux22.com/image/368135/Tikz%20%E6%97%A5%E5%8E%86%EF%BC%8C%E6%97%A5%E6%9C%9F%E6%95%B0%E5%AD%97%E4%BA%A4%E6%9B%BF%E7%9D%80%E8%89%B2.png)
我的代码主要改编自杰克的答案垂直和平日以及 Berteun Damman 的例子。
Berteun Damman 的这句话tiks@lib@cal@width=7
似乎没有什么区别,所以我把它注释掉了。
我的目标是使奇数日的日期数字为红色,其余日期数字为黑色。
虽然这问题解决了同样的问题,但我没能在那里找到答案。然而,史蒂芬·柯特维茨建议使用if (odd)
构造…但我还无法让它工作。
我尝试过各种变化 if (<date is odd>) [red]
但无法<date is odd>
正确表达。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calendar}
\makeatletter
\tikzstyle{week list}=[
execute before day scope={
\ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}
{% On first of month, except when first date in calendar.
\pgfmathsetlength{\pgf@x}{\tikz@lib@cal@month@xshift}
\pgftransformxshift{\pgf@x}
}
}{}
},
execute at begin day scope={%
\pgfmathsetlength\pgf@y{\tikz@lib@cal@yshift}%
\pgf@y=\pgfcalendarcurrentweekday\pgf@y%
\pgftransformyshift{-\pgf@y}%
},
execute after day scope={%
\ifdate{Sunday}{%
\pgfmathsetlength{\pgf@x}{\tikz@lib@cal@xshift}%
\pgftransformxshift{\pgf@x}
}{}%
},
% % This should be defined according to Berteun (I don't know why.)
% tikz@lib@cal@width=7
% ]
]
\tikzoption{day headings}{\tikzstyle{day heading}=[#1]}
\tikzstyle{day heading}=[]
\tikzstyle{day letter headings}=[
execute before day scope={ \ifdate{day of month=1}{%
\pgfmathsetlength\pgf@ya{\tikz@lib@cal@yshift}%
\pgfmathsetlength\pgf@xa{\tikz@lib@cal@xshift}%
\pgftransformxshift{\pgf@xa}
\foreach \d/\l in {0/Monday,1/Tuesday,2/Wednesday,3/Thursday,4/Friday,5/Saturday,6/Sunday} {
\pgf@ya=\d\pgf@ya%
\pgftransformxshift{-\pgf@xa}%
\pgftransformyshift{-\pgf@ya}%
\node[every day,day heading, anchor=base]{\l};%
}
}{}%
}%
]
\makeatother
\begin{document}
\tikz
\calendar
[name=cal,
dates=2018-02-01 to 2018-04-last,
week list,
day letter headings,
day heading/.style={red, xshift=-1em},
month label above centered,
month text = \%y- \%mt \%y-,
day xshift = 5ex,
day yshift = 5ex,
month yshift = 13ex
];
\end{document}
答案1
添加\ifodd\pgfcalendarcurrentday\relax \color{red} \fi
到execute at begin day scope
。月份的日期包含在内\pgfcalendarcurrentday
,要检查 tex 中的数字是否为奇数,可以使用\ifodd
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calendar}
\makeatletter
\tikzstyle{week list}=[
execute before day scope={
\ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}
{% On first of month, except when first date in calendar.
\pgfmathsetlength{\pgf@x}{\tikz@lib@cal@month@xshift}
\pgftransformxshift{\pgf@x}
}
}{}
},
execute at begin day scope={%
\ifodd\pgfcalendarcurrentday\relax \color{red} \fi %%% <-- added line
\pgfmathsetlength\pgf@y{\tikz@lib@cal@yshift}%
\pgf@y=\pgfcalendarcurrentweekday\pgf@y%
\pgftransformyshift{-\pgf@y}%
},
execute after day scope={%
\ifdate{Sunday}{%
\pgfmathsetlength{\pgf@x}{\tikz@lib@cal@xshift}%
\pgftransformxshift{\pgf@x}
}{}%
},
% % This should be defined according to Berteun (I don't know why.)
% tikz@lib@cal@width=7
% ]
]
\tikzoption{day headings}{\tikzstyle{day heading}=[#1]}
\tikzstyle{day heading}=[]
\tikzstyle{day letter headings}=[
execute before day scope={ \ifdate{day of month=1}{%
\pgfmathsetlength\pgf@ya{\tikz@lib@cal@yshift}%
\pgfmathsetlength\pgf@xa{\tikz@lib@cal@xshift}%
\pgftransformxshift{\pgf@xa}
\foreach \d/\l in {0/Monday,1/Tuesday,2/Wednesday,3/Thursday,4/Friday,5/Saturday,6/Sunday} {
\pgf@ya=\d\pgf@ya%
\pgftransformxshift{-\pgf@xa}%
\pgftransformyshift{-\pgf@ya}%
\node[every day,day heading, anchor=base]{\l};%
}
}{}%
}%
]
\makeatother
\begin{document}
\tikz
\calendar
[name=cal,
dates=2018-02-01 to 2018-04-last,
week list,
day letter headings,
day heading/.style={red, xshift=-1em},
month label above centered,
month text = \%y- \%mt \%y-,
day xshift = 5ex,
day yshift = 5ex,
month yshift = 13ex
];
\end{document}