我想构造一个带有两个参数(年份和月份)的计数器,例如\tt{year}{month}
。用法如下blah-blah \tt{2022}{6}, blah-blah \tt{2022}{6}
给出blah-blah 06/06, blah-blah 13/06
。如果月份已结束,则开始下一个月份。包 datetime 给出了相反的可能性:\dayofweekname{1}{6}{2022}
给出星期几的名称。
答案1
该解决方案使用的语法更类似于大卫卡莱尔建议。我们使用该pgfcalendar
包进行所有计算。
\documentclass{article}
\usepackage{pgfkeys,pgfcalendar}
\newcount\currentjday
\newcount\daycalcoffset
\newcommand\findFirstMonday[2]{%
\pgfcalendardatetojulian{#1-#2-1}{\currentjday}%
\pgfcalendarjuliantoweekday{\currentjday}{\daycalcoffset}%
\advance\currentjday by -\daycalcoffset\relax %find prev Monday
\getNextMonday
}
\newcommand\getNextMonday{%
\advance\currentjday by 7\relax
\pgfcalendarjuliantodate{\currentjday}{\theyear}{\themonth}{\theday}%
\theyear-\themonth-\theday
}
\begin{document}
\findFirstMonday{2022}{6}
\getNextMonday
\getNextMonday
\end{document}
答案2
对上述答案稍加修改即可适用于一周中的任何一天。
\documentclass{article}
\usepackage{pgfkeys,pgfcalendar}
\newcount\firstjday
\newcount\fistgday
\newcommand\fWd[3]{%
\pgfcalendardatetojulian{#1-#2-1}{\firstjday}%
\pgfcalendarjuliantoweekday{\firstjday}{\fistgday}%
\advance\firstjday by -\fistgday \relax %find prev Monday
\advance\firstjday by #3 \relax %find prev day #3 (#3=0 is Monday)
\nWd
}
\newcommand\nWd{%
\advance\firstjday by 7\relax
\pgfcalendarjuliantodate{\firstjday}{\theyear}{\themonth}{\theday}%
\theyear-\themonth-\theday
}
\begin{document}
\fWd{2022}{6}{0} \nWd\ \nWd\ \nWd
\end{document}
\fWd 有第三个参数,即星期几,0 表示星期一,1 表示星期二,依此类推。