我正在尝试使用和datetime2
包处理 Latex 中的日期(通过 Overleaf) pdfgantt
。
我已经创建了一个使用 增加日期的命令\DTMsavejulianday
。下面的代码显示它正确计算了日期,但在将计算出的日期传递到包中时却不尽如人意pdfgantt
。
在 overleaf 中,当我尝试使用该命令的结果时,\startDatePlusDays
它发出抱怨missing number, being treated as zero
。
简而言之,\DTMsavedate
创建我可以使用的日期,但\DTMsavejulianday
实际上却不能。我哪里做错了?
平均能量损失:
\documentclass[tikz]{standalone}
\usepackage[calc]{datetime2}
\usepackage{pgfgantt}
\begin{document}
\DTMsavedate{StartDate}{2023-10-02}
\DTMsavedate{EndDate}{2023-10-22}
% a command to add some days to a date
\newcount\daycount
\newcommand{\startDatePlusDays}[1]{%
\DTMsaveddateoffsettojulianday{StartDate}{#1}\daycount%
\DTMsavejulianday{newDate}{\number\daycount}%
\DTMusedate{newDate}%
}
\newcommand\startDate{\DTMusedate{StartDate}}
\newcommand\theEndDate{\DTMusedate{EndDate}}
\newcommand\anotherEndDate{\startDatePlusDays{20}}
%these look OK when printed
theEndDate is \theEndDate\\
anotherEndDate is \anotherEndDate\\
%So why is anotherEndDate being treated as zero?
\begin{ganttchart}[hgrid, vgrid, inline, time slot format=isodate]{\startDate}{\theEndDate}
\gantttitlecalendar{month=name, day}
\ganttnewline
\ganttbar{This works}{\startDate}{\theEndDate}\\
\ganttbar{This does not}{\startDate}{\anotherEndDate}\\
\end{ganttchart}
\end{document}
答案1
你的诊断不太正确: 没有问题\DTMsavejulianday
。 根本问题与几个宏的扩展有关\DTM...
。
例如,如果你将序言重写如下:
\newcount\daycount
\newcommand{\startDatePlusDays}[1]{%
\DTMsaveddateoffsettojulianday{StartDate}{#1}\daycount%
\DTMsavejulianday{newDate}{\number\daycount}%
%\DTMusedate{newDate}% <= NOTE: \startDatePlusDays now just saves to newDate
}
\newcommand\startDate{\DTMusedate{StartDate}}
\newcommand\theEndDate{\DTMusedate{EndDate}}
\startDatePlusDays{15} % <= sets newDate
\newcommand\anotherEndDate{\DTMusedate{newDate}} % <= uses newDate
那么代码就可以完美地编译。但是如果你定义\theEndDate
了
\newcommand\theEndDate{\DTMsavedate{EndDate}{2023-10-22}\DTMusedate{EndDate}}
您将会得到与之前完全相同的错误。
以下内容构成了一个可行的界面:
\documentclass[tikz]{standalone}
\usepackage[calc]{datetime2}
\usepackage{pgfgantt}
\begin{document}
\DTMsavedate{StartDate}{2023-10-02}
\DTMsavedate{EndDate}{2023-10-22}
% a command to add some days to a date and save it
\newcount\daycount
\newcommand{\saveStartDatePlusDays}[2]{%
\DTMsaveddateoffsettojulianday{StartDate}{#1}\daycount%
\DTMsavejulianday{#2}{\number\daycount}%
}
\newcommand\startDate{\DTMusedate{StartDate}}
\newcommand\theEndDate{\DTMusedate{EndDate}}
\newcommand\gantoffset[2]{%
\saveStartDatePlusDays{#2}{tempdate}%
\ganttbar{#1}{\startDate}{\DTMusedate{tempdate}}%
}
\begin{ganttchart}[hgrid, vgrid, inline, time slot format=isodate]{\startDate}{\theEndDate}
\gantttitlecalendar{month=name, day}
\ganttnewline
\ganttbar{This works}{\startDate}{\theEndDate}\\
\gantoffset{This also works}{15}\\
\gantoffset{This does too!}{13}
\end{ganttchart}
\end{document}