我已经修改了给出的代码成本加运费在回答这个问题关于当前节点。
我想在日历的第一行和最后一行用破折号填充“空白”天数。示例代码中的前两个月应如下所示:
August
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 -- -- -- --
September
-- -- -- 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 -- --
我认为可能有一种直接的方法来实现这一点。[或者至少我应该能够使用每月第一天和最后一天所在的星期几来确定我需要哪些额外的节点,但我甚至不知道如何开始这样的事情。]有什么建议吗?
\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{calendar}
\begin{document}
\begin{tikzpicture}
\calendar (cal)
[%
week list,
dates=2016-08-01 to 2016-10-last,
day xshift=1.5em,
month xshift=1pt,
month label above left,
]
if (weekend) [text=black!25]
[%
execute at end day scope={%
\draw[red]
(cal-\%y0-\%m0-\%d0.north east) --
(cal-\%y0-\%m0-\%d0.south east);
}][%
execute at end day scope={%
\draw[green]
(cal-\%y0-\%m0-\%d0.south east) --
(cal-\%y0-\%m0-\%d0.south west);
}];
\end{tikzpicture}
\end{document}
[注:这是关于修改同一代码的两个问题之一。另一个是这里。
答案1
好了。这很简单——我添加了在月份的第一天和最后一天执行的代码来添加破折号。为了添加破折号,我有一个循环,\foreach
从星期一到当天的前一天进行迭代,在每一天添加破折号。这不会为部分月份添加破折号,如果你想让我更新它来做到这一点,我可以。
\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{calendar}
\makeatletter
% #1 -- an integer 0-6 representing the day of the week
% This helper macro shifts the position to the appropriate place to insert a node on the given weekday
\newcommand\dayshift[1]{%
\pgfmathsetlength\pgf@x{\tikz@lib@cal@xshift}%
\pgf@x = #1 \pgf@x
\pgftransformxshift {\pgf@x }%
}
\makeatother
\begin{document}
\newcount\tempcount
\begin{tikzpicture}
\calendar (cal)
[%
week list,
dates=2016-08-01 to 2016-11-3,
day xshift=1.5em,
month xshift=1pt,
month label above left,
execute before day scope={
\ifdate{day of month=1}{ % If we are the first day of the month
% no dashes need be added if the first day is a Monday
\ifnum\pgfcalendarcurrentweekday>0\relax
% Iterate from Monday to day before current day
\foreach \i in {0,...,\the\numexpr\pgfcalendarcurrentweekday-1}{
\dayshift{\i}% add the appropriate shift
\node[every day] {-{}-};% add dashes
}
\fi
}{}
\pgfcalendardatetojulian{\pgfcalendarcurrentyear-\pgfcalendarcurrentmonth-last}
{\tempcount}
\ifnum\pgfcalendarcurrentjulian=\tempcount\relax % If last day of the month
\ifnum\pgfcalendarcurrentweekday<6\relax % If we need to add any dashes
% Iterate from next day to Sunday
\foreach \i in {\the\numexpr\pgfcalendarcurrentweekday+1,...,6}{
\dayshift{\i}%
\node[every day] {-{}-};% add dashes
}
\fi
\fi
}
]
if (weekend) [text=black!25]
[%
execute at end day scope={%
\draw[red]
(cal-\%y0-\%m0-\%d0.north east) --
(cal-\%y0-\%m0-\%d0.south east);
}][%
execute at end day scope={%
\draw[green]
(cal-\%y0-\%m0-\%d0.south east) --
(cal-\%y0-\%m0-\%d0.south west);
}];
\end{tikzpicture}
\end{document}