我正在尝试编写一个宏,用于将日期时间转换为 pgfgrantt 包可以理解的格式。如下所示:
\newcommand{\htime}[2][0]{
\StrBefore{#2}{:}[\hours]
\StrBehind{#2}{:}[\minutes]
\pgfmathparse{\hours+\minutes/60+#1}
\pgfmathresult
}
\newcommand{\gantthbar}[3]{
\def\from{\htime[1]{#2}}
\def\to{\htime{#3}}
\ganttbar{#1}{\from}{\to}
}
% \gantthbar{test}{11:15}{12:00} -> \ganttbar{test}{12.25}{12.0}
编译失败并出现错误,我无法解决:
! Use of \\ganttbar doesn't match its definition.
\kernel@ifnextchar ...rved@d =#1\def \reserved@a {
#2}\def \reserved@b {#3}\f...
l.37 \gantthbar{test}{11:15}{12:00}
?
这个编译没有错误:
\newcommand{\gantthbar}[3]{
\def\from{\htime[1]{#2}}
\def\to{\htime{#3}}
\ganttbar{#1 \from \to}{12.25}{12.0}
}
感谢您的帮助。
阅读评论后我写了非常简单的工作示例:
\documentclass[a4paper, 12pt]{report}
\usepackage{xstring}
\usepackage{pgfgantt}
\newcommand{\gantthbar}[3]{
\StrBefore{#2}{:}[\hoursfrom]
\StrBehind{#2}{:}[\minutesfrom]
\StrBefore{#3}{:}[\hoursto]
\StrBehind{#3}{:}[\minutesto]
\pgfmathsetmacro\from{\hoursfrom + \minutesfrom / 60 + 1}
\pgfmathsetmacro\to{\hoursto + \minutesto / 60}
\ganttbar{#1}{\from}{\to}
}
\begin{document}
\begin{ganttchart}[vgrid]{24}
\gantthbar{test}{11:15}{12:00}
\end{ganttchart}
\end{document}